1515'use strict' ;
1616
1717const assert = require ( 'assert' ) ;
18+ const path = require ( 'path' ) ;
19+ const uuid = require ( 'uuid' ) ;
20+ const sinon = require ( 'sinon' ) ;
21+ const { request} = require ( 'gaxios' ) ;
22+ const isReachable = require ( 'is-reachable' ) ;
23+ const execPromise = require ( 'child-process-promise' ) . exec ;
1824const { Datastore} = require ( '@google-cloud/datastore' ) ;
25+
1926const datastore = new Datastore ( ) ;
2027const program = require ( '../' ) ;
21- const uuid = require ( 'uuid' ) ;
22- const path = require ( 'path' ) ;
23- const execPromise = require ( 'child-process-promise' ) . exec ;
24- const sinon = require ( 'sinon' ) ;
2528
2629const FF_TIMEOUT = 3000 ;
27-
28- let requestRetry = require ( 'requestretry' ) ;
29- requestRetry = requestRetry . defaults ( {
30- retryDelay : 500 ,
31- retryStrategy : requestRetry . RetryStrategies . NetworkError ,
32- } ) ;
33-
3430const cwd = path . join ( __dirname , '..' ) ;
35-
3631const NAME = 'sampletask1' ;
3732const KIND = `Task-${ uuid . v4 ( ) } ` ;
3833const VALUE = {
@@ -54,17 +49,27 @@ const handleLinuxFailures = async proc => {
5449 }
5550} ;
5651
52+ // Wait for the HTTP server to start listening
53+ const waitForReady = async baseUrl => {
54+ let ready = false ;
55+ while ( ! ready ) {
56+ await new Promise ( r => setTimeout ( r , 500 ) ) ;
57+ ready = await isReachable ( baseUrl ) ;
58+ }
59+ } ;
60+
5761describe ( 'functions/datastore' , ( ) => {
5862 describe ( 'set' , ( ) => {
5963 let ffProc ;
6064 const PORT = 8080 ;
6165 const BASE_URL = `http://localhost:${ PORT } ` ;
6266
63- before ( ( ) => {
67+ before ( async ( ) => {
6468 ffProc = execPromise (
6569 `functions-framework --target=set --signature-type=http --port=${ PORT } ` ,
6670 { timeout : FF_TIMEOUT , shell : true , cwd}
6771 ) ;
72+ await waitForReady ( BASE_URL ) ;
6873 } ) ;
6974
7075 after ( async ( ) => {
@@ -96,9 +101,7 @@ describe('functions/datastore', () => {
96101 status : sinon . stub ( ) . returnsThis ( ) ,
97102 send : sinon . stub ( ) ,
98103 } ;
99-
100104 await program . set ( req , res ) ;
101-
102105 assert . ok ( res . status . calledWith ( 500 ) ) ;
103106 assert . ok ( res . send . calledWith ( errorMsg ( 'Key' ) ) ) ;
104107 } ) ;
@@ -122,19 +125,18 @@ describe('functions/datastore', () => {
122125 } ) ;
123126
124127 it ( 'set: Saves an entity' , async ( ) => {
125- const response = await requestRetry ( {
128+ const response = await request ( {
126129 url : `${ BASE_URL } /set` ,
127130 method : 'POST' ,
128- body : {
131+ responseType : 'text' ,
132+ data : {
129133 kind : KIND ,
130134 key : NAME ,
131135 value : VALUE ,
132136 } ,
133- json : true ,
134137 } ) ;
135-
136- assert . strictEqual ( response . statusCode , 200 ) ;
137- assert . ok ( response . body . includes ( `Entity ${ KIND } /${ NAME } saved` ) ) ;
138+ assert . strictEqual ( response . status , 200 ) ;
139+ assert . ok ( response . data . includes ( `Entity ${ KIND } /${ NAME } saved` ) ) ;
138140 } ) ;
139141 } ) ;
140142
@@ -143,49 +145,49 @@ describe('functions/datastore', () => {
143145 const PORT = 8081 ;
144146 const BASE_URL = `http://localhost:${ PORT } ` ;
145147
146- before ( ( ) => {
148+ before ( async ( ) => {
147149 ffProc = execPromise (
148150 `functions-framework --target=get --signature-type=http --port=${ PORT } ` ,
149151 { timeout : FF_TIMEOUT , shell : true , cwd}
150152 ) ;
153+ await waitForReady ( BASE_URL ) ;
151154 } ) ;
152155
153156 after ( async ( ) => {
154157 await handleLinuxFailures ( ffProc ) ;
155158 } ) ;
156159
157160 it ( 'get: Fails when entity does not exist' , async ( ) => {
158- const response = await requestRetry ( {
161+ const response = await request ( {
159162 url : `${ BASE_URL } /get` ,
160163 method : 'POST' ,
161- body : {
164+ data : {
162165 kind : KIND ,
163166 key : 'nonexistent' ,
164167 } ,
165- json : true ,
168+ responseType : 'text' ,
169+ validateStatus : ( ) => true ,
166170 } ) ;
167171
168- assert . strictEqual ( response . statusCode , 500 ) ;
172+ assert . strictEqual ( response . status , 500 ) ;
169173 assert . ok (
170174 new RegExp (
171175 / ( M i s s i n g o r i n s u f f i c i e n t p e r m i s s i o n s .) | ( N o e n t i t y f o u n d f o r k e y ) /
172- ) . test ( response . body )
176+ ) . test ( response . data )
173177 ) ;
174178 } ) ;
175179
176180 it ( 'get: Finds an entity' , async ( ) => {
177- const response = await requestRetry ( {
181+ const response = await request ( {
178182 method : 'POST' ,
179183 url : `${ BASE_URL } /get` ,
180- body : {
184+ data : {
181185 kind : KIND ,
182186 key : NAME ,
183187 } ,
184- json : true ,
185188 } ) ;
186-
187- assert . strictEqual ( response . statusCode , 200 ) ;
188- assert . deepStrictEqual ( response . body , {
189+ assert . strictEqual ( response . status , 200 ) ;
190+ assert . deepStrictEqual ( response . data , {
189191 description : 'Buy milk' ,
190192 } ) ;
191193 } ) ;
@@ -228,11 +230,12 @@ describe('functions/datastore', () => {
228230 const PORT = 8082 ;
229231 const BASE_URL = `http://localhost:${ PORT } ` ;
230232
231- before ( ( ) => {
233+ before ( async ( ) => {
232234 ffProc = execPromise (
233235 `functions-framework --target=del --signature-type=http --port=${ PORT } ` ,
234236 { timeout : FF_TIMEOUT , shell : true , cwd}
235237 ) ;
238+ await waitForReady ( BASE_URL ) ;
236239 } ) ;
237240
238241 after ( async ( ) => {
@@ -272,32 +275,31 @@ describe('functions/datastore', () => {
272275 } ) ;
273276
274277 it ( `del: Doesn't fail when entity does not exist` , async ( ) => {
275- const response = await requestRetry ( {
278+ const response = await request ( {
276279 method : 'POST' ,
277280 url : `${ BASE_URL } /del` ,
278- body : {
281+ data : {
279282 kind : KIND ,
280283 key : 'nonexistent' ,
281284 } ,
282- json : true ,
285+ responseType : 'text' ,
283286 } ) ;
284-
285- assert . strictEqual ( response . statusCode , 200 ) ;
286- assert . strictEqual ( response . body , `Entity ${ KIND } /nonexistent deleted.` ) ;
287+ assert . strictEqual ( response . status , 200 ) ;
288+ assert . strictEqual ( response . data , `Entity ${ KIND } /nonexistent deleted.` ) ;
287289 } ) ;
288290
289291 it ( 'del: Deletes an entity' , async ( ) => {
290- const response = await requestRetry ( {
292+ const response = await request ( {
291293 method : 'POST' ,
292294 url : `${ BASE_URL } /del` ,
293- body : {
295+ data : {
294296 kind : KIND ,
295297 key : NAME ,
296298 } ,
297- json : true ,
299+ responseType : 'text' ,
298300 } ) ;
299- assert . strictEqual ( response . statusCode , 200 ) ;
300- assert . strictEqual ( response . body , `Entity ${ KIND } /${ NAME } deleted.` ) ;
301+ assert . strictEqual ( response . status , 200 ) ;
302+ assert . strictEqual ( response . data , `Entity ${ KIND } /${ NAME } deleted.` ) ;
301303
302304 const key = datastore . key ( [ KIND , NAME ] ) ;
303305 const [ entity ] = await datastore . get ( key ) ;
0 commit comments