Skip to content

Commit

Permalink
Replace nock with msw
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansamines committed Jun 29, 2024
1 parent 7624c0a commit b3ebe5b
Show file tree
Hide file tree
Showing 15 changed files with 1,080 additions and 217 deletions.
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
146 changes: 96 additions & 50 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,79 +15,123 @@ 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',
},
}));
}

return {
Expand Down
Loading

0 comments on commit b3ebe5b

Please sign in to comment.