@@ -49,40 +49,158 @@ describe('Integration | Server', function() {
49
49
expect ( numIntercepts ) . to . equal ( 2 ) ;
50
50
} ) ;
51
51
52
- it ( 'merges all configs' , async function ( ) {
53
- const { server } = this . polly ;
54
- let config ;
55
-
56
- server . any ( ) . configure ( { foo : 'foo' } ) ;
57
- server . any ( ) . configure ( { bar : 'bar' } ) ;
58
- server
59
- . get ( '/ping' )
60
- . configure ( { foo : 'baz' } )
61
- . intercept ( ( req , res ) => {
62
- config = req . config ;
63
- res . sendStatus ( 200 ) ;
64
- } ) ;
52
+ describe ( 'API' , function ( ) {
53
+ it ( '.configure() - merges all configs' , async function ( ) {
54
+ const { server } = this . polly ;
55
+ let config ;
65
56
66
- expect ( ( await fetch ( '/ping' ) ) . status ) . to . equal ( 200 ) ;
67
- expect ( config ) . to . include ( { foo : 'baz' , bar : 'bar' } ) ;
68
- } ) ;
57
+ server . any ( ) . configure ( { foo : 'foo' } ) ;
58
+ server . any ( ) . configure ( { bar : 'bar' } ) ;
59
+ server
60
+ . get ( '/ping' )
61
+ . configure ( { foo : 'baz' } )
62
+ . intercept ( ( req , res ) => {
63
+ config = req . config ;
64
+ res . sendStatus ( 200 ) ;
65
+ } ) ;
69
66
70
- it ( 'should throw when trying to override certain options' , async function ( ) {
71
- const { server } = this . polly ;
67
+ expect ( ( await fetch ( '/ping' ) ) . status ) . to . equal ( 200 ) ;
68
+ expect ( config ) . to . include ( { foo : 'baz' , bar : 'bar' } ) ;
69
+ } ) ;
70
+
71
+ it ( '.configure() - should throw when trying to override certain options' , async function ( ) {
72
+ const { server } = this . polly ;
73
+
74
+ // The following options cannot be overridden on a per request basis
75
+ [
76
+ 'mode' ,
77
+ 'adapters' ,
78
+ 'adapterOptions' ,
79
+ 'persister' ,
80
+ 'persisterOptions'
81
+ ] . forEach ( key =>
82
+ expect ( ( ) => server . any ( ) . configure ( { [ key ] : 'foo' } ) ) . to . throw (
83
+ Error ,
84
+ / I n v a l i d c o n f i g u r a t i o n o p t i o n /
85
+ )
86
+ ) ;
87
+ } ) ;
88
+
89
+ it ( '.recordingName()' , async function ( ) {
90
+ const { server } = this . polly ;
91
+ let recordingName ;
92
+
93
+ server
94
+ . get ( '/ping' )
95
+ . recordingName ( 'Override' )
96
+ . intercept ( ( req , res ) => {
97
+ recordingName = req . recordingName ;
98
+ res . sendStatus ( 200 ) ;
99
+ } ) ;
100
+
101
+ expect ( ( await fetch ( '/ping' ) ) . status ) . to . equal ( 200 ) ;
102
+ expect ( recordingName ) . to . equal ( 'Override' ) ;
103
+ } ) ;
104
+
105
+ it ( '.recordingName() - should reset when called with no arguments' , async function ( ) {
106
+ const { server } = this . polly ;
107
+ let recordingName ;
108
+
109
+ server . any ( ) . recordingName ( 'Override' ) ;
110
+
111
+ server
112
+ . get ( '/ping' )
113
+ . recordingName ( )
114
+ . intercept ( ( req , res ) => {
115
+ recordingName = req . recordingName ;
116
+ res . sendStatus ( 200 ) ;
117
+ } ) ;
118
+
119
+ expect ( ( await fetch ( '/ping' ) ) . status ) . to . equal ( 200 ) ;
120
+ expect ( recordingName ) . to . not . equal ( 'Override' ) ;
121
+ } ) ;
122
+
123
+ it ( '.filter()' , async function ( ) {
124
+ const { server } = this . polly ;
72
125
73
- // The following options cannot be overridden on a per request basis
74
- [
75
- 'mode' ,
76
- 'adapters' ,
77
- 'adapterOptions' ,
78
- 'persister' ,
79
- 'persisterOptions'
80
- ] . forEach ( key =>
81
- expect ( ( ) => server . any ( ) . configure ( { [ key ] : 'foo' } ) ) . to . throw (
126
+ server
127
+ . get ( '/ping' )
128
+ . filter ( req => req . query . num === '1' )
129
+ . intercept ( ( req , res ) => res . sendStatus ( 201 ) ) ;
130
+
131
+ server
132
+ . get ( '/ping' )
133
+ . filter ( req => req . query . num === '2' )
134
+ . intercept ( ( req , res ) => res . sendStatus ( 202 ) ) ;
135
+
136
+ expect ( ( await fetch ( '/ping?num=1' ) ) . status ) . to . equal ( 201 ) ;
137
+ expect ( ( await fetch ( '/ping?num=2' ) ) . status ) . to . equal ( 202 ) ;
138
+ } ) ;
139
+
140
+ it ( '.filter() + events' , async function ( ) {
141
+ const { server } = this . polly ;
142
+ let num ;
143
+
144
+ server
145
+ . get ( '/ping' )
146
+ . filter ( req => req . query . num === '1' )
147
+ . on ( 'request' , req => ( num = req . query . num ) )
148
+ . intercept ( ( req , res ) => res . sendStatus ( 201 ) ) ;
149
+
150
+ server
151
+ . get ( '/ping' )
152
+ . filter ( req => req . query . num === '2' )
153
+ . on ( 'request' , req => ( num = req . query . num ) )
154
+ . intercept ( ( req , res ) => res . sendStatus ( 202 ) ) ;
155
+
156
+ expect ( ( await fetch ( '/ping?num=1' ) ) . status ) . to . equal ( 201 ) ;
157
+ expect ( num ) . to . equal ( '1' ) ;
158
+ } ) ;
159
+
160
+ it ( '.filter() - multiple' , async function ( ) {
161
+ const { server } = this . polly ;
162
+
163
+ server
164
+ . get ( '/ping' )
165
+ . filter ( req => req . query . foo === 'foo' )
166
+ . filter ( req => req . query . bar === 'bar' )
167
+ . intercept ( ( req , res ) => res . sendStatus ( 201 ) ) ;
168
+
169
+ server
170
+ . get ( '/ping' )
171
+ . filter ( req => req . query . foo === 'foo' )
172
+ . filter ( req => req . query . baz === 'baz' )
173
+ . intercept ( ( req , res ) => res . sendStatus ( 202 ) ) ;
174
+
175
+ expect ( ( await fetch ( '/ping?foo=foo&bar=bar' ) ) . status ) . to . equal ( 201 ) ;
176
+ expect ( ( await fetch ( '/ping?foo=foo&baz=baz' ) ) . status ) . to . equal ( 202 ) ;
177
+ } ) ;
178
+
179
+ it ( '.filter() - can access params' , async function ( ) {
180
+ const { server } = this . polly ;
181
+ let id ;
182
+
183
+ server
184
+ . get ( '/ping/:id' )
185
+ . filter ( req => {
186
+ id = req . params . id ;
187
+
188
+ return true ;
189
+ } )
190
+ . intercept ( ( req , res ) => res . sendStatus ( 201 ) ) ;
191
+
192
+ expect ( ( await fetch ( '/ping/1' ) ) . status ) . to . equal ( 201 ) ;
193
+ expect ( id ) . to . equal ( '1' ) ;
194
+ } ) ;
195
+
196
+ it ( '.filter() - should throw when not passed a function' , async function ( ) {
197
+ const { server } = this . polly ;
198
+
199
+ expect ( ( ) => server . any ( ) . filter ( ) ) . to . throw (
82
200
Error ,
83
- / I n v a l i d c o n f i g u r a t i o n o p t i o n /
84
- )
85
- ) ;
201
+ / I n v a l i d f i l t e r c a l l b a c k p r o v i d e d /
202
+ ) ;
203
+ } ) ;
86
204
} ) ;
87
205
88
206
describe ( 'Events & Middleware' , function ( ) {
0 commit comments