Skip to content

Commit

Permalink
Further document order middleware are applied
Browse files Browse the repository at this point in the history
  • Loading branch information
klippx committed Nov 5, 2024
1 parent 8d1835e commit 6421d79
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/middleware/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,111 @@ describe('middleware', () => {
expect(spy).toHaveBeenNthCalledWith(6, { foo: 'mw1', ctx: 'mw3' })
})
})

describe('both', () => {
beforeEach(() => {
mockRequest({
method: 'post',
url: `${host}${path}`,
body: m.anything(),
headers: {},
response: {
body: { ok: true },
status: 200,
},
})
})

const createMiddleware =
(
prepareRequestSpy: jest.Mock,
responseSpy: jest.Mock,
beforeParams: Record<string, string>,
afterParams: Record<string, string>
): Middleware =>
() => ({
prepareRequest: async (next) => {
prepareRequestSpy(beforeParams)
const request = await next()
prepareRequestSpy(afterParams)
return request
},
response: async (next) => {
responseSpy(beforeParams)
const response = await next()
responseSpy(afterParams)
return response
},
})

it('invokes middleware and extends response context in given order', async () => {
const prepareRequestSpy = jest.fn()
const responseSpy = jest.fn()
const mw1 = createMiddleware(prepareRequestSpy, responseSpy, { foo: 'mw1' }, { bar: 'mw1' })
const mw2 = createMiddleware(prepareRequestSpy, responseSpy, { ctx: 'mw2' }, { fizz: 'mw2' })
const mw3 = createMiddleware(prepareRequestSpy, responseSpy, { ctx: 'mw3' }, { buzz: 'mw3' })
const client = forge({
clientId: 'testMw',
middleware: [mw1, mw2, mw3],
host,
resources: { Resource: { create: { method: 'post', path } } },
})
await client.Resource.create()

// order of prepareRequest
expect(prepareRequestSpy).toHaveBeenCalledTimes(6)
expect(prepareRequestSpy).toHaveBeenNthCalledWith(1, { ctx: 'mw3' })
expect(prepareRequestSpy).toHaveBeenNthCalledWith(2, { ctx: 'mw2' })
expect(prepareRequestSpy).toHaveBeenNthCalledWith(3, { foo: 'mw1' })

expect(prepareRequestSpy).toHaveBeenNthCalledWith(4, { bar: 'mw1' })
expect(prepareRequestSpy).toHaveBeenNthCalledWith(5, { fizz: 'mw2' })
expect(prepareRequestSpy).toHaveBeenNthCalledWith(6, { buzz: 'mw3' })

// order of response
expect(responseSpy).toHaveBeenCalledTimes(6)
expect(responseSpy).toHaveBeenNthCalledWith(1, { ctx: 'mw3' })
expect(responseSpy).toHaveBeenNthCalledWith(2, { ctx: 'mw2' })
expect(responseSpy).toHaveBeenNthCalledWith(3, { foo: 'mw1' })

expect(responseSpy).toHaveBeenNthCalledWith(4, { bar: 'mw1' })
expect(responseSpy).toHaveBeenNthCalledWith(5, { fizz: 'mw2' })
expect(responseSpy).toHaveBeenNthCalledWith(6, { buzz: 'mw3' })
})

it('invokes resource specific middleware first', async () => {
const prepareRequestSpy = jest.fn()
const responseSpy = jest.fn()
const mw1 = createMiddleware(prepareRequestSpy, responseSpy, { foo: 'mw1' }, { bar: 'mw1' })
const mw2 = createMiddleware(prepareRequestSpy, responseSpy, { ctx: 'mw2' }, { fizz: 'mw2' })
const mw3 = createMiddleware(prepareRequestSpy, responseSpy, { ctx: 'mw3' }, { buzz: 'mw3' })
const client = forge({
clientId: 'testMw',
middleware: [mw1, mw3],
host,
resources: { Resource: { post: { method: 'post', path, middleware: [mw2] } } },
})
await client.Resource.post()

// order of prepareRequest
expect(prepareRequestSpy).toHaveBeenCalledTimes(6)
expect(prepareRequestSpy).toHaveBeenNthCalledWith(1, { ctx: 'mw3' })
expect(prepareRequestSpy).toHaveBeenNthCalledWith(2, { foo: 'mw1' })
expect(prepareRequestSpy).toHaveBeenNthCalledWith(3, { ctx: 'mw2' })

expect(prepareRequestSpy).toHaveBeenNthCalledWith(4, { fizz: 'mw2' })
expect(prepareRequestSpy).toHaveBeenNthCalledWith(5, { bar: 'mw1' })
expect(prepareRequestSpy).toHaveBeenNthCalledWith(6, { buzz: 'mw3' })

// order of response
expect(responseSpy).toHaveBeenCalledTimes(6)
expect(responseSpy).toHaveBeenNthCalledWith(1, { ctx: 'mw3' })
expect(responseSpy).toHaveBeenNthCalledWith(2, { foo: 'mw1' })
expect(responseSpy).toHaveBeenNthCalledWith(3, { ctx: 'mw2' })

expect(responseSpy).toHaveBeenNthCalledWith(4, { fizz: 'mw2' })
expect(responseSpy).toHaveBeenNthCalledWith(5, { bar: 'mw1' })
expect(responseSpy).toHaveBeenNthCalledWith(6, { buzz: 'mw3' })
})
})
})

0 comments on commit 6421d79

Please sign in to comment.