-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support msw v2 and custom server implementations #223
Changes from all commits
7600fa7
15f31f3
bd47f81
fa14dc4
3eb376e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,22 +1,24 @@ | ||||||
import { JwtPayload } from 'jsonwebtoken' | ||||||
import { createJWKS, createKeyPair, signJwt } from './tools.js' | ||||||
import { setupServer } from 'msw/node' | ||||||
import { rest } from 'msw' | ||||||
import { HttpResponse, http, RequestHandlerOptions, HttpHandler } from 'msw' | ||||||
|
||||||
const createJWKSMock = ( | ||||||
jwksBase: string, | ||||||
jwksPath = '/.well-known/jwks.json' | ||||||
) => { | ||||||
): JWKSMock => { | ||||||
const keypair = createKeyPair() | ||||||
const JWKS = createJWKS({ | ||||||
...keypair, | ||||||
jwksOrigin: jwksBase, | ||||||
}) | ||||||
const server = setupServer( | ||||||
rest.get(new URL(jwksPath, jwksBase).href, (_, res, ctx) => | ||||||
res(ctx.status(200), ctx.json(JWKS)) | ||||||
const jwksHandler = (options?: RequestHandlerOptions) => | ||||||
http.get( | ||||||
new URL(jwksPath, jwksBase).href, | ||||||
() => HttpResponse.json(JWKS), | ||||||
options | ||||||
) | ||||||
) | ||||||
const server = setupServer(jwksHandler()) | ||||||
|
||||||
const kid = () => JWKS.keys[0].kid | ||||||
|
||||||
|
@@ -35,9 +37,16 @@ const createJWKSMock = ( | |||||
stop, | ||||||
kid, | ||||||
token, | ||||||
jwksHandler, | ||||||
} | ||||||
} | ||||||
|
||||||
export type JWKSMock = ReturnType<typeof createJWKSMock> | ||||||
export type JWKSMock = { | ||||||
start: () => void | ||||||
stop: () => void | ||||||
kid: () => string | ||||||
token: (token: JwtPayload) => string | ||||||
jwksHandler: (options?: RequestHandlerOptions) => HttpHandler | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think it should be somehow clear from the name that this is for |
||||||
} | ||||||
|
||||||
export default createJWKSMock |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the wrong file. We should demonstrate the usage with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right sorry - that was the only test I saw that tested the mocking functionality. I didn't see the one outside of src. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why allow for providing options here? The only thing one could do it with it is to make a "one time handler" which I find kinda pointless. If the support for injecting options would be dropped, this would not need to be a function any more but could be a property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I started with it as a property but was trying to make it flexible - e.g. might want to ensure only one request is ever made. I don't currently use that option though so might be overkill. I also saw
kid
was a function so I kept a similar interface.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, you are right. Another unnecessary function. Possibly I was concerned about people writing to these variables. But I guess that is also a bit paranoid. I will provide the values for now but keep the
kid
as it is to not break the interface.