11'use strict' ;
22
3- const nock = require ( 'nock' ) ;
43const Hoek = require ( '@hapi/hoek' ) ;
54const Boom = require ( '@hapi/boom' ) ;
5+ const { HttpResponse } = require ( 'msw' ) ;
6+
7+ const msw = require ( './msw-matcher/_index' ) ;
68
79const accessToken = {
810 access_token : '5683E74C-7514-4426-B64F-CF0C24223F69' ,
@@ -13,79 +15,123 @@ const accessToken = {
1315
1416function 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 return {
0 commit comments