1
- var request = require ( 'supertest' ) ;
2
- var express = require ( 'express' ) ;
3
- var apiMocker = require ( '../index' ) ;
4
- var fs = require ( 'fs' ) ;
5
- var app = express ( ) ;
1
+ const request = require ( 'supertest' ) ;
2
+ const express = require ( 'express' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+ const apiMocker = require ( '..' ) ;
6
5
7
- var deleteFolderRecursive = function ( path ) {
6
+ const app = express ( ) ;
7
+
8
+ const deleteFolderRecursive = function ( path ) {
8
9
if ( fs . existsSync ( path ) ) {
9
- fs . readdirSync ( path ) . forEach ( function ( file , index ) {
10
- var curPath = path + "/" + file ;
10
+ fs . readdirSync ( path ) . forEach ( ( file ) => {
11
+ const curPath = ` ${ path } / ${ file } ` ;
11
12
if ( fs . lstatSync ( curPath ) . isDirectory ( ) ) { // recurse
12
13
deleteFolderRecursive ( curPath ) ;
13
14
} else { // delete file
@@ -20,24 +21,24 @@ var deleteFolderRecursive = function(path) {
20
21
21
22
app . use ( '/api' , apiMocker ( 'test/mocks' ) ) ;
22
23
app . use ( '/v2' , apiMocker ( {
23
- target : 'test/mocks' ,
24
- nextOnNotFound : true ,
25
- verbose : true
24
+ target : 'test/mocks' ,
25
+ nextOnNotFound : true ,
26
+ verbose : true
26
27
} ) ) ;
27
- app . use ( '/v2' , function ( req , res ) {
28
- res . json ( {
29
- message : 'Fallback'
30
- } )
28
+ app . use ( '/v2' , ( req , res ) => {
29
+ res . json ( {
30
+ message : 'Fallback'
31
+ } ) ;
31
32
} ) ;
32
33
app . use ( apiMocker ( '/v3' , 'test/mocks' ) ) ;
33
34
app . use ( apiMocker ( '/v4' , {
34
- target : 'test/mocks'
35
+ target : 'test/mocks'
35
36
} ) ) ;
36
37
app . use ( '/notdefined' , apiMocker ( 'notdefined' ) ) ;
37
38
app . use ( apiMocker ( '/xml' , {
38
39
target : 'test/mocks' ,
39
40
type : 'xml' ,
40
- verbose : function ( msg ) {
41
+ verbose ( ) {
41
42
// sth with message
42
43
}
43
44
} ) ) ;
@@ -46,8 +47,8 @@ app.use(apiMocker('/dyn', {
46
47
type : 'auto'
47
48
} ) ) ;
48
49
49
- describe ( 'Simple configuration with baseUrl' , function ( ) {
50
- it ( 'responds for simple GET request' , function ( done ) {
50
+ describe ( 'Simple configuration with baseUrl' , ( ) => {
51
+ it ( 'responds for simple GET request' , ( done ) => {
51
52
request ( app )
52
53
. get ( '/api/users/1' )
53
54
. expect ( 'Content-Type' , / j s o n / )
@@ -57,7 +58,7 @@ describe('Simple configuration with baseUrl', function () {
57
58
} , done ) ;
58
59
} ) ;
59
60
60
- it ( 'responds for simple POST request' , function ( done ) {
61
+ it ( 'responds for simple POST request' , ( done ) => {
61
62
request ( app )
62
63
. post ( '/api/users/1' )
63
64
. expect ( 'Content-Type' , / j s o n / )
@@ -67,7 +68,7 @@ describe('Simple configuration with baseUrl', function () {
67
68
} , done ) ;
68
69
} ) ;
69
70
70
- it ( 'custom response will not cache' , function ( done ) {
71
+ it ( 'custom response will not cache' , ( done ) => {
71
72
fs . mkdirSync ( './test/mocks/users/2' ) ;
72
73
fs . writeFileSync ( './test/mocks/users/2/GET.js' , fs . readFileSync ( './test/mocks/users/__user_id__/GET_example1.js' ) ) ;
73
74
@@ -83,15 +84,15 @@ describe('Simple configuration with baseUrl', function () {
83
84
. post ( '/api/users/2' )
84
85
. expect ( {
85
86
version : 2
86
- } , function ( ) {
87
+ } , ( ) => {
87
88
done ( ) ;
88
89
deleteFolderRecursive ( './test/mocks/users/2' ) ;
89
90
} ) ;
90
91
} ) ;
91
92
} ) ;
92
93
93
- describe ( 'nextOnNotFound setting' , function ( ) {
94
- it ( 'returns correct response when mock is exits' , function ( done ) {
94
+ describe ( 'nextOnNotFound setting' , ( ) => {
95
+ it ( 'returns correct response when mock is exits' , ( done ) => {
95
96
request ( app )
96
97
. get ( '/v2/users/1' )
97
98
. expect ( 'Content-Type' , / j s o n / )
@@ -101,7 +102,7 @@ describe('nextOnNotFound setting', function () {
101
102
} , done ) ;
102
103
} ) ;
103
104
104
- it ( 'returns fallback when mock is not exits' , function ( done ) {
105
+ it ( 'returns fallback when mock is not exits' , ( done ) => {
105
106
request ( app )
106
107
. get ( '/v2/non-existing-resource' )
107
108
. expect ( 'Content-Type' , / j s o n / )
@@ -112,8 +113,8 @@ describe('nextOnNotFound setting', function () {
112
113
} ) ;
113
114
} ) ;
114
115
115
- describe ( 'Simple configuration without baseUrl' , function ( ) {
116
- it ( 'returns correct response' , function ( done ) {
116
+ describe ( 'Simple configuration without baseUrl' , ( ) => {
117
+ it ( 'returns correct response' , ( done ) => {
117
118
request ( app )
118
119
. get ( '/v3/users/1' )
119
120
. expect ( 'Content-Type' , / j s o n / )
@@ -124,8 +125,8 @@ describe('Simple configuration without baseUrl', function () {
124
125
} ) ;
125
126
} ) ;
126
127
127
- describe ( 'Configuration with object and without baseUrl' , function ( ) {
128
- it ( 'returns correct response' , function ( done ) {
128
+ describe ( 'Configuration with object and without baseUrl' , ( ) => {
129
+ it ( 'returns correct response' , ( done ) => {
129
130
request ( app )
130
131
. get ( '/v4/users/1' )
131
132
. expect ( 'Content-Type' , / j s o n / )
@@ -136,14 +137,14 @@ describe('Configuration with object and without baseUrl', function () {
136
137
} ) ;
137
138
} ) ;
138
139
139
- describe ( 'Wildcard feature' , function ( ) {
140
- it ( 'works properly when no mock exist for request' , function ( done ) {
140
+ describe ( 'Wildcard feature' , ( ) => {
141
+ it ( 'works properly when no mock exist for request' , ( done ) => {
141
142
request ( app )
142
143
. get ( '/notdefined/products/1' )
143
144
. expect ( 404 , done ) ;
144
145
} ) ;
145
146
146
- it ( 'wildcard mock works properly' , function ( done ) {
147
+ it ( 'wildcard mock works properly' , ( done ) => {
147
148
request ( app )
148
149
. get ( '/api/users/2812391232' )
149
150
. expect ( 200 )
@@ -154,7 +155,7 @@ describe('Wildcard feature', function () {
154
155
} , done ) ;
155
156
} ) ;
156
157
157
- it ( 'wildcard mock works properly with nested resources' , function ( done ) {
158
+ it ( 'wildcard mock works properly with nested resources' , ( done ) => {
158
159
request ( app )
159
160
. get ( '/api/users/1/nested' )
160
161
. expect ( 200 )
@@ -164,13 +165,13 @@ describe('Wildcard feature', function () {
164
165
} , done ) ;
165
166
} ) ;
166
167
167
- it ( 'wildcard json methods should work on any given method' , function ( done ) {
168
+ it ( 'wildcard json methods should work on any given method' , ( done ) => {
168
169
request ( app )
169
170
. get ( '/api/users/1/any-json-request' )
170
171
. expect ( 200 )
171
172
. expect ( {
172
173
method : 'ANY'
173
- } , function ( ) {
174
+ } , ( ) => {
174
175
request ( app )
175
176
. post ( '/api/users/1/any-json-request' )
176
177
. expect ( 200 )
@@ -180,13 +181,13 @@ describe('Wildcard feature', function () {
180
181
} ) ;
181
182
} ) ;
182
183
183
- it ( 'wildcard js methods should work on any given method' , function ( done ) {
184
+ it ( 'wildcard js methods should work on any given method' , ( done ) => {
184
185
request ( app )
185
186
. get ( '/api/users/1/any-js-request' )
186
187
. expect ( 200 )
187
188
. expect ( {
188
189
anyMethod : 'GET'
189
- } , function ( ) {
190
+ } , ( ) => {
190
191
request ( app )
191
192
. post ( '/api/users/1/any-js-request' )
192
193
. expect ( 200 )
@@ -198,59 +199,57 @@ describe('Wildcard feature', function () {
198
199
} ) ;
199
200
200
201
201
- describe ( 'Response type config' , function ( ) {
202
- it ( 'works properly with xml responses' , function ( done ) {
202
+ describe ( 'Response type config' , ( ) => {
203
+ it ( 'works properly with xml responses' , ( done ) => {
203
204
request ( app )
204
205
. get ( '/xml/users/1' )
205
206
. expect ( 'Content-Type' , / x m l / )
206
207
. expect ( 200 , done ) ;
207
208
} ) ;
208
209
209
- it ( 'works properly with auto type (xml)' , function ( done ) {
210
+ it ( 'works properly with auto type (xml)' , ( done ) => {
210
211
request ( app )
211
212
. get ( '/dyn/users/1' )
212
213
. set ( 'Accept' , 'application/xml' )
213
214
. expect ( 'Content-Type' , / x m l / )
214
215
. expect ( 200 , done ) ;
215
216
} ) ;
216
217
217
- it ( 'works properly with auto type (json)' , function ( done ) {
218
+ it ( 'works properly with auto type (json)' , ( done ) => {
218
219
request ( app )
219
220
. get ( '/dyn/users/1' )
220
221
. set ( 'Accept' , 'application/json' )
221
222
. expect ( 'Content-Type' , / j s o n / )
222
223
. expect ( 200 , done ) ;
223
224
} ) ;
224
225
225
- it ( 'works properly with auto type (xml not found)' , function ( done ) {
226
+ it ( 'works properly with auto type (xml not found)' , ( done ) => {
226
227
request ( app )
227
228
. post ( '/dyn/users/2' )
228
229
. set ( 'Accept' , 'application/xml' )
229
230
. expect ( 404 , done ) ;
230
-
231
231
} ) ;
232
232
} ) ;
233
233
234
- describe ( 'Handling request body' , function ( ) {
235
- it ( 'should work with request body json' , function ( done ) {
234
+ describe ( 'Handling request body' , ( ) => {
235
+ it ( 'should work with request body json' , ( done ) => {
236
236
request ( app )
237
237
. post ( '/api/users' )
238
238
. set ( 'Content-Type' , 'application/json' )
239
- . send ( { name : 'A name' } )
239
+ . send ( { name : 'A name' } )
240
240
. expect ( 201 )
241
241
. expect ( {
242
242
name : 'A name'
243
- } , done )
243
+ } , done ) ;
244
244
} ) ;
245
245
246
- it ( 'shouldnt break to capability of reading raw request body' , function ( done ) {
246
+ it ( 'shouldnt break to capability of reading raw request body' , ( done ) => {
247
247
request ( app )
248
248
. patch ( '/api/users' )
249
249
. send ( 'A text content' )
250
250
. expect ( 200 )
251
251
. expect ( {
252
252
requestString : 'A text content'
253
253
} , done ) ;
254
- } )
255
-
256
- } )
254
+ } ) ;
255
+ } ) ;
0 commit comments