diff --git a/datastore/functions/package.json b/datastore/functions/package.json index a69d759567..867a19cd4a 100644 --- a/datastore/functions/package.json +++ b/datastore/functions/package.json @@ -1,6 +1,5 @@ { "name": "nodejs-docs-samples-functions-datastore", - "version": "0.0.1", "private": true, "license": "Apache-2.0", "author": "Google Inc.", @@ -16,15 +15,14 @@ }, "dependencies": { "@google-cloud/datastore": "^5.0.0", - "supertest": "^4.0.0" + "is-reachable": "^4.0.0" }, "devDependencies": { "@google-cloud/functions-framework": "^1.1.1", "child-process-promise": "^2.2.1", + "gaxios": "^2.3.1", "mocha": "^7.0.0", "proxyquire": "^2.1.0", - "request": "^2.88.0", - "requestretry": "^4.0.0", "sinon": "^9.0.0", "uuid": "^3.3.2" } diff --git a/datastore/functions/test/index.test.js b/datastore/functions/test/index.test.js index c0520fc287..5d773ededb 100644 --- a/datastore/functions/test/index.test.js +++ b/datastore/functions/test/index.test.js @@ -15,24 +15,19 @@ 'use strict'; const assert = require('assert'); +const path = require('path'); +const uuid = require('uuid'); +const sinon = require('sinon'); +const {request} = require('gaxios'); +const isReachable = require('is-reachable'); +const execPromise = require('child-process-promise').exec; const {Datastore} = require('@google-cloud/datastore'); + const datastore = new Datastore(); const program = require('../'); -const uuid = require('uuid'); -const path = require('path'); -const execPromise = require('child-process-promise').exec; -const sinon = require('sinon'); const FF_TIMEOUT = 3000; - -let requestRetry = require('requestretry'); -requestRetry = requestRetry.defaults({ - retryDelay: 500, - retryStrategy: requestRetry.RetryStrategies.NetworkError, -}); - const cwd = path.join(__dirname, '..'); - const NAME = 'sampletask1'; const KIND = `Task-${uuid.v4()}`; const VALUE = { @@ -54,17 +49,27 @@ const handleLinuxFailures = async proc => { } }; +// Wait for the HTTP server to start listening +const waitForReady = async baseUrl => { + let ready = false; + while (!ready) { + await new Promise(r => setTimeout(r, 500)); + ready = await isReachable(baseUrl); + } +}; + describe('functions/datastore', () => { describe('set', () => { let ffProc; const PORT = 8080; const BASE_URL = `http://localhost:${PORT}`; - before(() => { + before(async () => { ffProc = execPromise( `functions-framework --target=set --signature-type=http --port=${PORT}`, {timeout: FF_TIMEOUT, shell: true, cwd} ); + await waitForReady(BASE_URL); }); after(async () => { @@ -96,9 +101,7 @@ describe('functions/datastore', () => { status: sinon.stub().returnsThis(), send: sinon.stub(), }; - await program.set(req, res); - assert.ok(res.status.calledWith(500)); assert.ok(res.send.calledWith(errorMsg('Key'))); }); @@ -122,19 +125,18 @@ describe('functions/datastore', () => { }); it('set: Saves an entity', async () => { - const response = await requestRetry({ + const response = await request({ url: `${BASE_URL}/set`, method: 'POST', - body: { + responseType: 'text', + data: { kind: KIND, key: NAME, value: VALUE, }, - json: true, }); - - assert.strictEqual(response.statusCode, 200); - assert.ok(response.body.includes(`Entity ${KIND}/${NAME} saved`)); + assert.strictEqual(response.status, 200); + assert.ok(response.data.includes(`Entity ${KIND}/${NAME} saved`)); }); }); @@ -143,11 +145,12 @@ describe('functions/datastore', () => { const PORT = 8081; const BASE_URL = `http://localhost:${PORT}`; - before(() => { + before(async () => { ffProc = execPromise( `functions-framework --target=get --signature-type=http --port=${PORT}`, {timeout: FF_TIMEOUT, shell: true, cwd} ); + await waitForReady(BASE_URL); }); after(async () => { @@ -155,37 +158,36 @@ describe('functions/datastore', () => { }); it('get: Fails when entity does not exist', async () => { - const response = await requestRetry({ + const response = await request({ url: `${BASE_URL}/get`, method: 'POST', - body: { + data: { kind: KIND, key: 'nonexistent', }, - json: true, + responseType: 'text', + validateStatus: () => true, }); - assert.strictEqual(response.statusCode, 500); + assert.strictEqual(response.status, 500); assert.ok( new RegExp( /(Missing or insufficient permissions.)|(No entity found for key)/ - ).test(response.body) + ).test(response.data) ); }); it('get: Finds an entity', async () => { - const response = await requestRetry({ + const response = await request({ method: 'POST', url: `${BASE_URL}/get`, - body: { + data: { kind: KIND, key: NAME, }, - json: true, }); - - assert.strictEqual(response.statusCode, 200); - assert.deepStrictEqual(response.body, { + assert.strictEqual(response.status, 200); + assert.deepStrictEqual(response.data, { description: 'Buy milk', }); }); @@ -228,11 +230,12 @@ describe('functions/datastore', () => { const PORT = 8082; const BASE_URL = `http://localhost:${PORT}`; - before(() => { + before(async () => { ffProc = execPromise( `functions-framework --target=del --signature-type=http --port=${PORT}`, {timeout: FF_TIMEOUT, shell: true, cwd} ); + await waitForReady(BASE_URL); }); after(async () => { @@ -272,32 +275,31 @@ describe('functions/datastore', () => { }); it(`del: Doesn't fail when entity does not exist`, async () => { - const response = await requestRetry({ + const response = await request({ method: 'POST', url: `${BASE_URL}/del`, - body: { + data: { kind: KIND, key: 'nonexistent', }, - json: true, + responseType: 'text', }); - - assert.strictEqual(response.statusCode, 200); - assert.strictEqual(response.body, `Entity ${KIND}/nonexistent deleted.`); + assert.strictEqual(response.status, 200); + assert.strictEqual(response.data, `Entity ${KIND}/nonexistent deleted.`); }); it('del: Deletes an entity', async () => { - const response = await requestRetry({ + const response = await request({ method: 'POST', url: `${BASE_URL}/del`, - body: { + data: { kind: KIND, key: NAME, }, - json: true, + responseType: 'text', }); - assert.strictEqual(response.statusCode, 200); - assert.strictEqual(response.body, `Entity ${KIND}/${NAME} deleted.`); + assert.strictEqual(response.status, 200); + assert.strictEqual(response.data, `Entity ${KIND}/${NAME} deleted.`); const key = datastore.key([KIND, NAME]); const [entity] = await datastore.get(key);