1
1
'use strict' ;
2
2
3
- const nock = require ( 'nock' ) ;
4
3
const Hoek = require ( '@hapi/hoek' ) ;
5
4
const Boom = require ( '@hapi/boom' ) ;
5
+ const { HttpResponse } = require ( 'msw' ) ;
6
+
7
+ const msw = require ( './msw-matcher/_index' ) ;
6
8
7
9
const accessToken = {
8
10
access_token : '5683E74C-7514-4426-B64F-CF0C24223F69' ,
@@ -13,87 +15,134 @@ const accessToken = {
13
15
14
16
function createAuthorizationServer ( authorizationServerUrl ) {
15
17
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
+ } ) ) ;
21
26
}
22
27
23
28
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
+ } ) ) ;
29
37
}
30
38
31
39
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
+ } ) ) ;
37
48
}
38
49
39
50
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
+ } ) ) ;
45
59
}
46
60
47
61
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
+ } ) ) ;
53
73
}
54
74
55
75
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
+ } ) ) ;
61
87
}
62
88
63
89
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
+ } ) ) ;
69
98
}
70
99
71
100
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
+ } ) ) ;
81
121
}
82
122
83
123
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
+ } ) ) ;
89
135
}
90
136
91
137
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
+ } ) ) ;
97
146
}
98
147
99
148
return {
0 commit comments