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

Replace nock with msw #447

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
424 changes: 391 additions & 33 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@
"chance": "^1.1.9",
"chance-access-token": "^2.1.0",
"date-fns": "^3.6.0",
"diff": "^5.2.0",
"doctoc": "^2.2.1",
"eslint": "^8.26.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.26.0",
"nock": "^13.2.9"
"msw": "^2.3.1"
},
"volta": {
"node": "18.20.3"
Expand Down
159 changes: 104 additions & 55 deletions test/_authorization-server-mock.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const nock = require('nock');
const Hoek = require('@hapi/hoek');
const Boom = require('@hapi/boom');
const { HttpResponse } = require('msw');

const msw = require('./msw-matcher/_index');

const accessToken = {
access_token: '5683E74C-7514-4426-B64F-CF0C24223F69',
Expand All @@ -13,87 +15,134 @@ const accessToken = {

function createAuthorizationServer(authorizationServerUrl) {
function tokenSuccessWithCustomPath(path, scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post(path, params)
.reply(200, accessToken, {
'Content-Type': 'application/json',
});
return msw
.scope(authorizationServerUrl)
.post(path)
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(accessToken, {
status: 200,
}));
}

function tokenSuccess(scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post('/oauth/token', params)
.reply(200, accessToken, {
'Content-Type': 'application/json',
});
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(accessToken, {
status: 200,
}));
}

function tokenError(scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post('/oauth/token', params)
.reply(500, Boom.badImplementation(), {
'Content-Type': 'application/json',
});
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(Boom.badImplementation(), {
status: 500,
}));
}

function tokenAuthorizationError(scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post('/oauth/token', params)
.reply(401, Boom.unauthorized(), {
'Content-Type': 'application/json',
});
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(Boom.unauthorized(), {
status: 401,
}));
}

function tokenRevokeSuccess(scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post('/oauth/revoke', params)
.reply(240, null, {
'Content-Type': 'application/json',
});
return msw
.scope(authorizationServerUrl)
.post('/oauth/revoke')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse(null, {
status: 204,
headers: {
'Content-Type': 'application/json',
},
}));
}

function tokenRevokeSuccessWithCustomPath(path, scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post(path, params)
.reply(204, null, {
'Content-Type': 'application/json',
});
return msw
.scope(authorizationServerUrl)
.post(path)
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse(null, {
status: 204,
headers: {
'Content-Type': 'application/json',
},
}));
}

function tokenRevokeError(scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post('/oauth/revoke', params)
.reply(500, Boom.badImplementation(), {
'Content-Type': 'application/json',
});
return msw
.scope(authorizationServerUrl)
.post('/oauth/revoke')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json(Boom.badImplementation(), {
status: 500,
}));
}

function tokenRevokeAllSuccess(scopeOptions, accessTokenParams, refreshTokenParams) {
return nock(authorizationServerUrl, scopeOptions)
.post('/oauth/revoke', accessTokenParams)
.reply(204, null, {
'Content-Type': 'application/json',
})
.post('/oauth/revoke', refreshTokenParams)
.reply(204, null, {
'Content-Type': 'application/json',
});
return msw
.scope(authorizationServerUrl)
.post('/oauth/revoke')
.matchBody(accessTokenParams)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse(null, {
status: 204,
headers: {
'Content-Type': 'application/json',
},
}))
.post('/oauth/revoke')
.matchBody(refreshTokenParams)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse(null, {
status: 204,
headers: {
'Content-Type': 'application/json',
},
}));
}

function tokenSuccessWithNonJSONContent(scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post('/oauth/token', params)
.reply(200, '<html>Sorry for not responding with a json response</html>', {
'Content-Type': 'application/html',
});
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => new HttpResponse('<html>Sorry for not responding with a json response</html>', {
status: 200,
headers: {
'Content-Type': 'text/html',
},
}));
}

function tokenSuccessWithoutRefreshToken(scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post('/oauth/token', params)
.reply(200, { ...accessToken, refresh_token: undefined }, {
'Content-Type': 'application/json',
});
return msw
.scope(authorizationServerUrl)
.post('/oauth/token')
.matchBody(params)
.matchHeaders(scopeOptions.reqheaders)
.handler(() => HttpResponse.json({ ...accessToken, refresh_token: undefined }, {
status: 200,
}));
}

return {
Expand Down
Loading
Loading