@@ -6,7 +6,7 @@ const httpRequest = require('../lib/cloud-code/httpRequest'),
66 express = require ( 'express' ) ;
77
88const port = 13371 ;
9- const httpRequestServer = ' http://localhost:' + port ;
9+ const httpRequestServer = ` http://localhost:${ port } ` ;
1010
1111function startServer ( done ) {
1212 const app = express ( ) ;
@@ -51,167 +51,136 @@ describe('httpRequest', () => {
5151 server . close ( done ) ;
5252 } ) ;
5353
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' ) ;
6463 } ) ;
6564
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 ) ;
7371 } ) ;
7472
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` ,
7876 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' ) ;
8683 } ) ;
8784
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+ } )
10797 ) ;
10898 } ) ;
10999
110- it ( 'should post on echo' , done => {
111- httpRequest ( {
100+ it ( 'should post on echo' , async ( ) => {
101+ const httpResponse = await httpRequest ( {
112102 method : 'POST' ,
113- url : httpRequestServer + ' /echo' ,
103+ url : ` ${ httpRequestServer } /echo` ,
114104 body : {
115105 foo : 'bar' ,
116106 } ,
117107 headers : {
118108 'Content-Type' : 'application/json' ,
119109 } ,
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' } ) ;
131114 } ) ;
132115
133- it ( 'should encode a query string body by default' , done => {
116+ it ( 'should encode a query string body by default' , ( ) => {
134117 const options = {
135118 body : { foo : 'bar' } ,
136119 } ;
137120 const result = httpRequest . encodeBody ( options ) ;
121+
138122 expect ( result . body ) . toEqual ( 'foo=bar' ) ;
139123 expect ( result . headers [ 'Content-Type' ] ) . toEqual ( 'application/x-www-form-urlencoded' ) ;
140- done ( ) ;
141124 } ) ;
142125
143- it ( 'should encode a JSON body' , done => {
126+ it ( 'should encode a JSON body' , ( ) => {
144127 const options = {
145128 body : { foo : 'bar' } ,
146129 headers : { 'Content-Type' : 'application/json' } ,
147130 } ;
148131 const result = httpRequest . encodeBody ( options ) ;
132+
149133 expect ( result . body ) . toEqual ( '{"foo":"bar"}' ) ;
150- done ( ) ;
151134 } ) ;
152- it ( 'should encode a www-form body' , done => {
135+
136+ it ( 'should encode a www-form body' , ( ) => {
153137 const options = {
154138 body : { foo : 'bar' , bar : 'baz' } ,
155139 headers : { 'cOntent-tYpe' : 'application/x-www-form-urlencoded' } ,
156140 } ;
157141 const result = httpRequest . encodeBody ( options ) ;
142+
158143 expect ( result . body ) . toEqual ( 'foo=bar&bar=baz' ) ;
159- done ( ) ;
160144 } ) ;
161- it ( 'should not encode a wrong content type' , done => {
145+
146+ it ( 'should not encode a wrong content type' , ( ) => {
162147 const options = {
163148 body : { foo : 'bar' , bar : 'baz' } ,
164149 headers : { 'cOntent-tYpe' : 'mime/jpeg' } ,
165150 } ;
166151 const result = httpRequest . encodeBody ( options ) ;
152+
167153 expect ( result . body ) . toEqual ( { foo : 'bar' , bar : 'baz' } ) ;
168- done ( ) ;
169154 } ) ;
170155
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 ( ) ;
179162 } ) ;
180163
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` ,
184167 params : {
185168 foo : 'bar' ,
186169 } ,
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' } ) ;
198174 } ) ;
199175
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` ,
203179 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' } ) ;
215184 } ) ;
216185
217186 it ( 'should not crash with undefined body' , ( ) => {
@@ -230,6 +199,7 @@ describe('httpRequest', () => {
230199
231200 const serialized = JSON . stringify ( httpResponse ) ;
232201 const result = JSON . parse ( serialized ) ;
202+
233203 expect ( result . text ) . toBe ( 'hello' ) ;
234204 expect ( result . data ) . toBe ( undefined ) ;
235205 expect ( result . body ) . toBe ( undefined ) ;
@@ -251,43 +221,47 @@ describe('httpRequest', () => {
251221 } ) ;
252222
253223 it ( 'serialized httpResponse correctly with body buffer string' , ( ) => {
254- const httpResponse = new HTTPResponse ( { } , new Buffer ( 'hello' ) ) ;
224+ const httpResponse = new HTTPResponse ( { } , Buffer . from ( 'hello' ) ) ;
255225 expect ( httpResponse . text ) . toBe ( 'hello' ) ;
256226 expect ( httpResponse . data ) . toBe ( undefined ) ;
257227
258228 const serialized = JSON . stringify ( httpResponse ) ;
259229 const result = JSON . parse ( serialized ) ;
230+
260231 expect ( result . text ) . toBe ( 'hello' ) ;
261232 expect ( result . data ) . toBe ( undefined ) ;
262233 } ) ;
263234
264235 it ( 'serialized httpResponse correctly with body buffer JSON Object' , ( ) => {
265236 const json = '{"foo":"bar"}' ;
266- const httpResponse = new HTTPResponse ( { } , new Buffer ( json ) ) ;
237+ const httpResponse = new HTTPResponse ( { } , Buffer . from ( json ) ) ;
267238 const serialized = JSON . stringify ( httpResponse ) ;
268239 const result = JSON . parse ( serialized ) ;
240+
269241 expect ( result . text ) . toEqual ( '{"foo":"bar"}' ) ;
270242 expect ( result . data ) . toEqual ( { foo : 'bar' } ) ;
271243 } ) ;
272244
273245 it ( 'serialized httpResponse with Parse._encode should be allright' , ( ) => {
274246 const json = '{"foo":"bar"}' ;
275- const httpResponse = new HTTPResponse ( { } , new Buffer ( json ) ) ;
247+ const httpResponse = new HTTPResponse ( { } , Buffer . from ( json ) ) ;
276248 const encoded = Parse . _encode ( httpResponse ) ;
277249 let foundData ,
278250 foundText ,
279251 foundBody = false ;
252+
280253 for ( const key in encoded ) {
281- if ( key == 'data' ) {
254+ if ( key === 'data' ) {
282255 foundData = true ;
283256 }
284- if ( key == 'text' ) {
257+ if ( key === 'text' ) {
285258 foundText = true ;
286259 }
287- if ( key == 'body' ) {
260+ if ( key === 'body' ) {
288261 foundBody = true ;
289262 }
290263 }
264+
291265 expect ( foundData ) . toBe ( true ) ;
292266 expect ( foundText ) . toBe ( true ) ;
293267 expect ( foundBody ) . toBe ( false ) ;
0 commit comments