@@ -6,7 +6,7 @@ const httpRequest = require('../lib/cloud-code/httpRequest'),
6
6
express = require ( 'express' ) ;
7
7
8
8
const port = 13371 ;
9
- const httpRequestServer = ' http://localhost:' + port ;
9
+ const httpRequestServer = ` http://localhost:${ port } ` ;
10
10
11
11
function startServer ( done ) {
12
12
const app = express ( ) ;
@@ -51,167 +51,136 @@ describe('httpRequest', () => {
51
51
server . close ( done ) ;
52
52
} ) ;
53
53
54
- it ( 'should do /hello' , done => {
55
- httpRequest ( {
56
- url : httpRequestServer + '/hello' ,
57
- } ) . then ( function ( httpResponse ) {
58
- expect ( httpResponse . status ) . toBe ( 200 ) ;
59
- expect ( httpResponse . buffer ) . toEqual ( new Buffer ( '{"response":"OK"}' ) ) ;
60
- expect ( httpResponse . text ) . toEqual ( '{"response":"OK"}' ) ;
61
- expect ( httpResponse . data . response ) . toEqual ( 'OK' ) ;
62
- done ( ) ;
63
- } , done . fail ) ;
54
+ it ( 'should do /hello' , async ( ) => {
55
+ const httpResponse = await httpRequest ( {
56
+ url : `${ httpRequestServer } /hello` ,
57
+ } ) ;
58
+
59
+ expect ( httpResponse . status ) . toBe ( 200 ) ;
60
+ expect ( httpResponse . buffer ) . toEqual ( Buffer . from ( '{"response":"OK"}' ) ) ;
61
+ expect ( httpResponse . text ) . toEqual ( '{"response":"OK"}' ) ;
62
+ expect ( httpResponse . data . response ) . toEqual ( 'OK' ) ;
64
63
} ) ;
65
64
66
- it ( 'should do not follow redirects by default' , done => {
67
- httpRequest ( {
68
- url : httpRequestServer + '/301' ,
69
- } ) . then ( function ( httpResponse ) {
70
- expect ( httpResponse . status ) . toBe ( 301 ) ;
71
- done ( ) ;
72
- } , done . fail ) ;
65
+ it ( 'should do not follow redirects by default' , async ( ) => {
66
+ const httpResponse = await httpRequest ( {
67
+ url : `${ httpRequestServer } /301` ,
68
+ } ) ;
69
+
70
+ expect ( httpResponse . status ) . toBe ( 301 ) ;
73
71
} ) ;
74
72
75
- it ( 'should follow redirects when set' , done => {
76
- httpRequest ( {
77
- url : httpRequestServer + ' /301' ,
73
+ it ( 'should follow redirects when set' , async ( ) => {
74
+ const httpResponse = await httpRequest ( {
75
+ url : ` ${ httpRequestServer } /301` ,
78
76
followRedirects : true ,
79
- } ) . then ( function ( httpResponse ) {
80
- expect ( httpResponse . status ) . toBe ( 200 ) ;
81
- expect ( httpResponse . buffer ) . toEqual ( new Buffer ( '{"response":"OK"}' ) ) ;
82
- expect ( httpResponse . text ) . toEqual ( '{"response":"OK"}' ) ;
83
- expect ( httpResponse . data . response ) . toEqual ( 'OK' ) ;
84
- done ( ) ;
85
- } , done . fail ) ;
77
+ } ) ;
78
+
79
+ expect ( httpResponse . status ) . toBe ( 200 ) ;
80
+ expect ( httpResponse . buffer ) . toEqual ( Buffer . from ( '{"response":"OK"}' ) ) ;
81
+ expect ( httpResponse . text ) . toEqual ( '{"response":"OK"}' ) ;
82
+ expect ( httpResponse . data . response ) . toEqual ( 'OK' ) ;
86
83
} ) ;
87
84
88
- it ( 'should fail on 404' , done => {
89
- let calls = 0 ;
90
- httpRequest ( {
91
- url : httpRequestServer + '/404' ,
92
- } ) . then (
93
- function ( ) {
94
- calls ++ ;
95
- fail ( 'should not succeed' ) ;
96
- done ( ) ;
97
- } ,
98
- function ( httpResponse ) {
99
- calls ++ ;
100
- expect ( calls ) . toBe ( 1 ) ;
101
- expect ( httpResponse . status ) . toBe ( 404 ) ;
102
- expect ( httpResponse . buffer ) . toEqual ( new Buffer ( 'NO' ) ) ;
103
- expect ( httpResponse . text ) . toEqual ( 'NO' ) ;
104
- expect ( httpResponse . data ) . toBe ( undefined ) ;
105
- done ( ) ;
106
- }
85
+ it ( 'should fail on 404' , async ( ) => {
86
+ await expectAsync (
87
+ httpRequest ( {
88
+ url : `${ httpRequestServer } /404` ,
89
+ } )
90
+ ) . toBeRejectedWith (
91
+ jasmine . objectContaining ( {
92
+ status : 404 ,
93
+ buffer : Buffer . from ( 'NO' ) ,
94
+ text : 'NO' ,
95
+ data : undefined ,
96
+ } )
107
97
) ;
108
98
} ) ;
109
99
110
- it ( 'should post on echo' , done => {
111
- httpRequest ( {
100
+ it ( 'should post on echo' , async ( ) => {
101
+ const httpResponse = await httpRequest ( {
112
102
method : 'POST' ,
113
- url : httpRequestServer + ' /echo' ,
103
+ url : ` ${ httpRequestServer } /echo` ,
114
104
body : {
115
105
foo : 'bar' ,
116
106
} ,
117
107
headers : {
118
108
'Content-Type' : 'application/json' ,
119
109
} ,
120
- } ) . then (
121
- function ( httpResponse ) {
122
- expect ( httpResponse . status ) . toBe ( 200 ) ;
123
- expect ( httpResponse . data ) . toEqual ( { foo : 'bar' } ) ;
124
- done ( ) ;
125
- } ,
126
- function ( ) {
127
- fail ( 'should not fail' ) ;
128
- done ( ) ;
129
- }
130
- ) ;
110
+ } ) ;
111
+
112
+ expect ( httpResponse . status ) . toBe ( 200 ) ;
113
+ expect ( httpResponse . data ) . toEqual ( { foo : 'bar' } ) ;
131
114
} ) ;
132
115
133
- it ( 'should encode a query string body by default' , done => {
116
+ it ( 'should encode a query string body by default' , ( ) => {
134
117
const options = {
135
118
body : { foo : 'bar' } ,
136
119
} ;
137
120
const result = httpRequest . encodeBody ( options ) ;
121
+
138
122
expect ( result . body ) . toEqual ( 'foo=bar' ) ;
139
123
expect ( result . headers [ 'Content-Type' ] ) . toEqual ( 'application/x-www-form-urlencoded' ) ;
140
- done ( ) ;
141
124
} ) ;
142
125
143
- it ( 'should encode a JSON body' , done => {
126
+ it ( 'should encode a JSON body' , ( ) => {
144
127
const options = {
145
128
body : { foo : 'bar' } ,
146
129
headers : { 'Content-Type' : 'application/json' } ,
147
130
} ;
148
131
const result = httpRequest . encodeBody ( options ) ;
132
+
149
133
expect ( result . body ) . toEqual ( '{"foo":"bar"}' ) ;
150
- done ( ) ;
151
134
} ) ;
152
- it ( 'should encode a www-form body' , done => {
135
+
136
+ it ( 'should encode a www-form body' , ( ) => {
153
137
const options = {
154
138
body : { foo : 'bar' , bar : 'baz' } ,
155
139
headers : { 'cOntent-tYpe' : 'application/x-www-form-urlencoded' } ,
156
140
} ;
157
141
const result = httpRequest . encodeBody ( options ) ;
142
+
158
143
expect ( result . body ) . toEqual ( 'foo=bar&bar=baz' ) ;
159
- done ( ) ;
160
144
} ) ;
161
- it ( 'should not encode a wrong content type' , done => {
145
+
146
+ it ( 'should not encode a wrong content type' , ( ) => {
162
147
const options = {
163
148
body : { foo : 'bar' , bar : 'baz' } ,
164
149
headers : { 'cOntent-tYpe' : 'mime/jpeg' } ,
165
150
} ;
166
151
const result = httpRequest . encodeBody ( options ) ;
152
+
167
153
expect ( result . body ) . toEqual ( { foo : 'bar' , bar : 'baz' } ) ;
168
- done ( ) ;
169
154
} ) ;
170
155
171
- it ( 'should fail gracefully' , done => {
172
- httpRequest ( {
173
- url : 'http://not a good url' ,
174
- } ) . then ( done . fail , function ( error ) {
175
- expect ( error ) . not . toBeUndefined ( ) ;
176
- expect ( error ) . not . toBeNull ( ) ;
177
- done ( ) ;
178
- } ) ;
156
+ it ( 'should fail gracefully' , async ( ) => {
157
+ await expectAsync (
158
+ httpRequest ( {
159
+ url : 'http://not a good url' ,
160
+ } )
161
+ ) . toBeRejected ( ) ;
179
162
} ) ;
180
163
181
- it ( 'should params object to query string' , done => {
182
- httpRequest ( {
183
- url : httpRequestServer + ' /qs' ,
164
+ it ( 'should params object to query string' , async ( ) => {
165
+ const httpResponse = await httpRequest ( {
166
+ url : ` ${ httpRequestServer } /qs` ,
184
167
params : {
185
168
foo : 'bar' ,
186
169
} ,
187
- } ) . then (
188
- function ( httpResponse ) {
189
- expect ( httpResponse . status ) . toBe ( 200 ) ;
190
- expect ( httpResponse . data ) . toEqual ( { foo : 'bar' } ) ;
191
- done ( ) ;
192
- } ,
193
- function ( ) {
194
- fail ( 'should not fail' ) ;
195
- done ( ) ;
196
- }
197
- ) ;
170
+ } ) ;
171
+
172
+ expect ( httpResponse . status ) . toBe ( 200 ) ;
173
+ expect ( httpResponse . data ) . toEqual ( { foo : 'bar' } ) ;
198
174
} ) ;
199
175
200
- it ( 'should params string to query string' , done => {
201
- httpRequest ( {
202
- url : httpRequestServer + ' /qs' ,
176
+ it ( 'should params string to query string' , async ( ) => {
177
+ const httpResponse = await httpRequest ( {
178
+ url : ` ${ httpRequestServer } /qs` ,
203
179
params : 'foo=bar&foo2=bar2' ,
204
- } ) . then (
205
- function ( httpResponse ) {
206
- expect ( httpResponse . status ) . toBe ( 200 ) ;
207
- expect ( httpResponse . data ) . toEqual ( { foo : 'bar' , foo2 : 'bar2' } ) ;
208
- done ( ) ;
209
- } ,
210
- function ( ) {
211
- fail ( 'should not fail' ) ;
212
- done ( ) ;
213
- }
214
- ) ;
180
+ } ) ;
181
+
182
+ expect ( httpResponse . status ) . toBe ( 200 ) ;
183
+ expect ( httpResponse . data ) . toEqual ( { foo : 'bar' , foo2 : 'bar2' } ) ;
215
184
} ) ;
216
185
217
186
it ( 'should not crash with undefined body' , ( ) => {
@@ -230,6 +199,7 @@ describe('httpRequest', () => {
230
199
231
200
const serialized = JSON . stringify ( httpResponse ) ;
232
201
const result = JSON . parse ( serialized ) ;
202
+
233
203
expect ( result . text ) . toBe ( 'hello' ) ;
234
204
expect ( result . data ) . toBe ( undefined ) ;
235
205
expect ( result . body ) . toBe ( undefined ) ;
@@ -251,43 +221,47 @@ describe('httpRequest', () => {
251
221
} ) ;
252
222
253
223
it ( 'serialized httpResponse correctly with body buffer string' , ( ) => {
254
- const httpResponse = new HTTPResponse ( { } , new Buffer ( 'hello' ) ) ;
224
+ const httpResponse = new HTTPResponse ( { } , Buffer . from ( 'hello' ) ) ;
255
225
expect ( httpResponse . text ) . toBe ( 'hello' ) ;
256
226
expect ( httpResponse . data ) . toBe ( undefined ) ;
257
227
258
228
const serialized = JSON . stringify ( httpResponse ) ;
259
229
const result = JSON . parse ( serialized ) ;
230
+
260
231
expect ( result . text ) . toBe ( 'hello' ) ;
261
232
expect ( result . data ) . toBe ( undefined ) ;
262
233
} ) ;
263
234
264
235
it ( 'serialized httpResponse correctly with body buffer JSON Object' , ( ) => {
265
236
const json = '{"foo":"bar"}' ;
266
- const httpResponse = new HTTPResponse ( { } , new Buffer ( json ) ) ;
237
+ const httpResponse = new HTTPResponse ( { } , Buffer . from ( json ) ) ;
267
238
const serialized = JSON . stringify ( httpResponse ) ;
268
239
const result = JSON . parse ( serialized ) ;
240
+
269
241
expect ( result . text ) . toEqual ( '{"foo":"bar"}' ) ;
270
242
expect ( result . data ) . toEqual ( { foo : 'bar' } ) ;
271
243
} ) ;
272
244
273
245
it ( 'serialized httpResponse with Parse._encode should be allright' , ( ) => {
274
246
const json = '{"foo":"bar"}' ;
275
- const httpResponse = new HTTPResponse ( { } , new Buffer ( json ) ) ;
247
+ const httpResponse = new HTTPResponse ( { } , Buffer . from ( json ) ) ;
276
248
const encoded = Parse . _encode ( httpResponse ) ;
277
249
let foundData ,
278
250
foundText ,
279
251
foundBody = false ;
252
+
280
253
for ( const key in encoded ) {
281
- if ( key == 'data' ) {
254
+ if ( key === 'data' ) {
282
255
foundData = true ;
283
256
}
284
- if ( key == 'text' ) {
257
+ if ( key === 'text' ) {
285
258
foundText = true ;
286
259
}
287
- if ( key == 'body' ) {
260
+ if ( key === 'body' ) {
288
261
foundBody = true ;
289
262
}
290
263
}
264
+
291
265
expect ( foundData ) . toBe ( true ) ;
292
266
expect ( foundText ) . toBe ( true ) ;
293
267
expect ( foundBody ) . toBe ( false ) ;
0 commit comments