Skip to content
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

Mocking URLs with msw/nock doesn't work with named imports. #584

Closed
2 tasks done
dhruvinmakwana19 opened this issue Oct 2, 2023 · 1 comment
Closed
2 tasks done

Comments

@dhruvinmakwana19
Copy link

dhruvinmakwana19 commented Oct 2, 2023

What happened?

I am writing integration tests to mock the auth mechanism in my application. To do this I am using mock-jwks which internally uses the MSW library to mock the responses of creating jwks in createRemoteJWKSet. The problem I am facing right now is since he module fetches the remote JWKs through the named import of http/https modules, MSW is not able to monkey patch the http/https modules. I have found possible issues relating to this problem and there seems to be a possible solution implemented here.

Related issues:
stripe/stripe-node#1844
nock/nock#2461

Version

4.11.0

Runtime

Browser

Runtime Details

Node - v16.20.2 with ESM

Code to reproduce

//test setup
test('Test with mocked jkwsUri', async () => {
  const jwksMock = createJWKSMock(
    'https://keycloak.somedomain.com',
    '/auth/realm/application/protocol/openid-connect/certs'
  )
  // We start our app.
  const server = createApp().listen()
  const request = supertest(server)
  jwksMock.start()
  const access_token = jwksMock.token({
    aud: 'private',
    iss: 'master',
  })
  const { status } = await request
    .get('/')
    .set('Authorization', `Bearer ${access_token}`)
  expect(status).toEqual(200)
})
// app


function auth() {
  const getKey = createRemoteJWKSet(new URL("https://keycloak.somedomain.com/auth/realm/application/protocol/openid-connect/certs"));

  return async function middleware(ctx, next) {

    const [type, accessToken] = ctx.request.get('Authorization').split(' ');

    try {
      const { payload } = await jwtVerify(accessToken, getKey, { issuer, audience });
    } catch (err) {
      ctx.throw(401);
    }
    return next();
  };
}

const createApp = () => {
  const app = new Koa()
  app.use(auth())
  const router = new Router()
  // This route is protected by the authentication middleware
  router.get('/', (ctx) => {
    ctx.body = 'Authenticated!'
  })
  app.use(router.middleware())
  return app
}
export default createApp

Required

  • I have searched the issues tracker and discussions for similar topics and couldn't find anything related.
  • I agree to follow this project's Code of Conduct
@panva
Copy link
Owner

panva commented Oct 2, 2023

@panva panva closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2023
@panva panva removed the triage label Oct 4, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jan 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants