Skip to content

Commit 0990440

Browse files
Replace nock with msw
1 parent 23aca64 commit 0990440

15 files changed

+1092
-224
lines changed

package-lock.json

+391-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@
5858
"chance": "^1.1.9",
5959
"chance-access-token": "^2.1.0",
6060
"date-fns": "^3.6.0",
61+
"diff": "^5.2.0",
6162
"doctoc": "^2.2.1",
6263
"eslint": "^8.26.0",
6364
"eslint-config-airbnb-base": "^15.0.0",
6465
"eslint-plugin-import": "^2.26.0",
65-
"nock": "^13.2.9"
66+
"msw": "^2.3.1"
6667
},
6768
"volta": {
6869
"node": "18.20.3"

test/_authorization-server-mock.js

+104-55
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22

3-
const nock = require('nock');
43
const Hoek = require('@hapi/hoek');
54
const Boom = require('@hapi/boom');
5+
const { HttpResponse } = require('msw');
6+
7+
const msw = require('./msw-matcher/_index');
68

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

1416
function createAuthorizationServer(authorizationServerUrl) {
1517
function tokenSuccessWithCustomPath(path, scopeOptions, params) {
16-
return nock(authorizationServerUrl, scopeOptions)
17-
.post(path, params)
18-
.reply(200, accessToken, {
19-
'Content-Type': 'application/json',
20-
});
18+
return msw
19+
.scope(authorizationServerUrl)
20+
.post(path)
21+
.matchBody(params)
22+
.matchHeaders(scopeOptions.reqheaders)
23+
.handler(() => HttpResponse.json(accessToken, {
24+
status: 200,
25+
}));
2126
}
2227

2328
function tokenSuccess(scopeOptions, params) {
24-
return nock(authorizationServerUrl, scopeOptions)
25-
.post('/oauth/token', params)
26-
.reply(200, accessToken, {
27-
'Content-Type': 'application/json',
28-
});
29+
return msw
30+
.scope(authorizationServerUrl)
31+
.post('/oauth/token')
32+
.matchBody(params)
33+
.matchHeaders(scopeOptions.reqheaders)
34+
.handler(() => HttpResponse.json(accessToken, {
35+
status: 200,
36+
}));
2937
}
3038

3139
function tokenError(scopeOptions, params) {
32-
return nock(authorizationServerUrl, scopeOptions)
33-
.post('/oauth/token', params)
34-
.reply(500, Boom.badImplementation(), {
35-
'Content-Type': 'application/json',
36-
});
40+
return msw
41+
.scope(authorizationServerUrl)
42+
.post('/oauth/token')
43+
.matchBody(params)
44+
.matchHeaders(scopeOptions.reqheaders)
45+
.handler(() => HttpResponse.json(Boom.badImplementation(), {
46+
status: 500,
47+
}));
3748
}
3849

3950
function tokenAuthorizationError(scopeOptions, params) {
40-
return nock(authorizationServerUrl, scopeOptions)
41-
.post('/oauth/token', params)
42-
.reply(401, Boom.unauthorized(), {
43-
'Content-Type': 'application/json',
44-
});
51+
return msw
52+
.scope(authorizationServerUrl)
53+
.post('/oauth/token')
54+
.matchBody(params)
55+
.matchHeaders(scopeOptions.reqheaders)
56+
.handler(() => HttpResponse.json(Boom.unauthorized(), {
57+
status: 401,
58+
}));
4559
}
4660

4761
function tokenRevokeSuccess(scopeOptions, params) {
48-
return nock(authorizationServerUrl, scopeOptions)
49-
.post('/oauth/revoke', params)
50-
.reply(240, null, {
51-
'Content-Type': 'application/json',
52-
});
62+
return msw
63+
.scope(authorizationServerUrl)
64+
.post('/oauth/revoke')
65+
.matchBody(params)
66+
.matchHeaders(scopeOptions.reqheaders)
67+
.handler(() => new HttpResponse(null, {
68+
status: 204,
69+
headers: {
70+
'Content-Type': 'application/json',
71+
},
72+
}));
5373
}
5474

5575
function tokenRevokeSuccessWithCustomPath(path, scopeOptions, params) {
56-
return nock(authorizationServerUrl, scopeOptions)
57-
.post(path, params)
58-
.reply(204, null, {
59-
'Content-Type': 'application/json',
60-
});
76+
return msw
77+
.scope(authorizationServerUrl)
78+
.post(path)
79+
.matchBody(params)
80+
.matchHeaders(scopeOptions.reqheaders)
81+
.handler(() => new HttpResponse(null, {
82+
status: 204,
83+
headers: {
84+
'Content-Type': 'application/json',
85+
},
86+
}));
6187
}
6288

6389
function tokenRevokeError(scopeOptions, params) {
64-
return nock(authorizationServerUrl, scopeOptions)
65-
.post('/oauth/revoke', params)
66-
.reply(500, Boom.badImplementation(), {
67-
'Content-Type': 'application/json',
68-
});
90+
return msw
91+
.scope(authorizationServerUrl)
92+
.post('/oauth/revoke')
93+
.matchBody(params)
94+
.matchHeaders(scopeOptions.reqheaders)
95+
.handler(() => HttpResponse.json(Boom.badImplementation(), {
96+
status: 500,
97+
}));
6998
}
7099

71100
function tokenRevokeAllSuccess(scopeOptions, accessTokenParams, refreshTokenParams) {
72-
return nock(authorizationServerUrl, scopeOptions)
73-
.post('/oauth/revoke', accessTokenParams)
74-
.reply(204, null, {
75-
'Content-Type': 'application/json',
76-
})
77-
.post('/oauth/revoke', refreshTokenParams)
78-
.reply(204, null, {
79-
'Content-Type': 'application/json',
80-
});
101+
return msw
102+
.scope(authorizationServerUrl)
103+
.post('/oauth/revoke')
104+
.matchBody(accessTokenParams)
105+
.matchHeaders(scopeOptions.reqheaders)
106+
.handler(() => new HttpResponse(null, {
107+
status: 204,
108+
headers: {
109+
'Content-Type': 'application/json',
110+
},
111+
}))
112+
.post('/oauth/revoke')
113+
.matchBody(refreshTokenParams)
114+
.matchHeaders(scopeOptions.reqheaders)
115+
.handler(() => new HttpResponse(null, {
116+
status: 204,
117+
headers: {
118+
'Content-Type': 'application/json',
119+
},
120+
}));
81121
}
82122

83123
function tokenSuccessWithNonJSONContent(scopeOptions, params) {
84-
return nock(authorizationServerUrl, scopeOptions)
85-
.post('/oauth/token', params)
86-
.reply(200, '<html>Sorry for not responding with a json response</html>', {
87-
'Content-Type': 'application/html',
88-
});
124+
return msw
125+
.scope(authorizationServerUrl)
126+
.post('/oauth/token')
127+
.matchBody(params)
128+
.matchHeaders(scopeOptions.reqheaders)
129+
.handler(() => new HttpResponse('<html>Sorry for not responding with a json response</html>', {
130+
status: 200,
131+
headers: {
132+
'Content-Type': 'text/html',
133+
},
134+
}));
89135
}
90136

91137
function tokenSuccessWithoutRefreshToken(scopeOptions, params) {
92-
return nock(authorizationServerUrl, scopeOptions)
93-
.post('/oauth/token', params)
94-
.reply(200, { ...accessToken, refresh_token: undefined }, {
95-
'Content-Type': 'application/json',
96-
});
138+
return msw
139+
.scope(authorizationServerUrl)
140+
.post('/oauth/token')
141+
.matchBody(params)
142+
.matchHeaders(scopeOptions.reqheaders)
143+
.handler(() => HttpResponse.json({ ...accessToken, refresh_token: undefined }, {
144+
status: 200,
145+
}));
97146
}
98147

99148
return {

0 commit comments

Comments
 (0)