Skip to content

Commit

Permalink
chore: use node-fetch instead of gaxios (#2024)
Browse files Browse the repository at this point in the history
  • Loading branch information
fhinkel authored Oct 28, 2020
1 parent b6f425d commit 92fce56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion datastore/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"devDependencies": {
"@google-cloud/functions-framework": "^1.1.1",
"child-process-promise": "^2.2.1",
"gaxios": "^4.0.0",
"mocha": "^8.0.0",
"node-fetch": "^2.6.1",
"proxyquire": "^2.1.0",
"sinon": "^9.0.0",
"uuid": "^8.0.0"
Expand Down
61 changes: 31 additions & 30 deletions datastore/functions/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const assert = require('assert');
const path = require('path');
const uuid = require('uuid');
const sinon = require('sinon');
const {request} = require('gaxios');
const fetch = require('node-fetch');
const isReachable = require('is-reachable');
const execPromise = require('child-process-promise').exec;
const {Datastore} = require('@google-cloud/datastore');
Expand Down Expand Up @@ -125,18 +125,18 @@ describe('functions/datastore', () => {
});

it('set: Saves an entity', async () => {
const response = await request({
url: `${BASE_URL}/set`,
const response = await fetch(`${BASE_URL}/set`,{
method: 'POST',
responseType: 'text',
data: {
body: JSON.stringify({
kind: KIND,
key: NAME,
value: VALUE,
},
}),
headers: {'Content-Type': 'application/json'},
});
assert.strictEqual(response.status, 200);
assert.ok(response.data.includes(`Entity ${KIND}/${NAME} saved`));
const body = await response.text();
assert.ok(body.includes(`Entity ${KIND}/${NAME} saved`));
});
});

Expand All @@ -158,36 +158,37 @@ describe('functions/datastore', () => {
});

it('get: Fails when entity does not exist', async () => {
const response = await request({
url: `${BASE_URL}/get`,
const response = await fetch(`${BASE_URL}/get`, {
method: 'POST',
data: {
body: JSON.stringify({
kind: KIND,
key: 'nonexistent',
},
responseType: 'text',
}),
headers: {'Content-Type': 'application/json'},
validateStatus: () => true,
});

assert.strictEqual(response.status, 500);
const body = await response.text();
assert.ok(
new RegExp(
/(Missing or insufficient permissions.)|(No entity found for key)/
).test(response.data)
).test(body)
);
});

it('get: Finds an entity', async () => {
const response = await request({
const response = await fetch(`${BASE_URL}/get`, {
method: 'POST',
url: `${BASE_URL}/get`,
data: {
body: JSON.stringify({
kind: KIND,
key: NAME,
},
}),
headers: {'Content-Type': 'application/json'},
});
assert.strictEqual(response.status, 200);
assert.deepStrictEqual(response.data, {
const body = await response.json();
assert.deepStrictEqual(body, {
description: 'Buy milk',
});
});
Expand Down Expand Up @@ -275,31 +276,31 @@ describe('functions/datastore', () => {
});

it(`del: Doesn't fail when entity does not exist`, async () => {
const response = await request({
const response = await fetch(`${BASE_URL}/del`,{
method: 'POST',
url: `${BASE_URL}/del`,
data: {
body: JSON.stringify({
kind: KIND,
key: 'nonexistent',
},
responseType: 'text',
}),
headers: {'Content-Type': 'application/json'},
});
assert.strictEqual(response.status, 200);
assert.strictEqual(response.data, `Entity ${KIND}/nonexistent deleted.`);
const body = await response.text();
assert.strictEqual(body, `Entity ${KIND}/nonexistent deleted.`);
});

it('del: Deletes an entity', async () => {
const response = await request({
const response = await fetch( `${BASE_URL}/del`,{
method: 'POST',
url: `${BASE_URL}/del`,
data: {
body: JSON.stringify({
kind: KIND,
key: NAME,
},
responseType: 'text',
}),
headers: {'Content-Type': 'application/json'},
});
assert.strictEqual(response.status, 200);
assert.strictEqual(response.data, `Entity ${KIND}/${NAME} deleted.`);
const body = await response.text();
assert.strictEqual(body, `Entity ${KIND}/${NAME} deleted.`);

const key = datastore.key([KIND, NAME]);
const [entity] = await datastore.get(key);
Expand Down

0 comments on commit 92fce56

Please sign in to comment.