Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions functions/http/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

node_modules
13 changes: 7 additions & 6 deletions functions/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ exports.helloContent = (req, res) => {
// [END functions_http_content]

// [START functions_http_method]
function handleGET(req, res) {
function handleGET (req, res) {
// Do something with the GET request
res.status(200).send('Hello World!');
}

function handlePUT(req, res) {
function handlePUT (req, res) {
// Do something with the PUT request
res.status(403).send('Forbidden!');
}
Expand Down Expand Up @@ -194,7 +194,8 @@ exports.uploadFile = (req, res) => {
// [END functions_http_form_data]

// [START functions_http_signed_url]
const storage = require('@google-cloud/storage')();
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

/**
* HTTP function that generates a signed URL
Expand All @@ -208,17 +209,17 @@ exports.getSignedUrl = (req, res) => {
// TODO(developer) check that the user is authorized to upload

// Get a reference to the destination file in GCS
const file = storage.bucket('my-bucket').file(req.body.filename);
const file = storage.bucket(req.body.bucket).file(req.body.filename);

// Create a temporary upload URL
const expiresAtMs = Date.now() + 300000; // Link expires in 5 minutes
const config = {
action: 'write',
expires: expiresAtMs,
contentType: req.body.contentType,
contentType: req.body.contentType
};

file.getSignedUrl(config, function(err, url) {
file.getSignedUrl(config, function (err, url) {
if (err) {
console.error(err);
res.status(500).end();
Expand Down
5 changes: 3 additions & 2 deletions functions/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"ava": "0.25.0",
"proxyquire": "2.1.0",
"sinon": "4.4.2"
"sinon": "4.4.2",
"uuid": "^3.3.2"
},
"dependencies": {
"@google-cloud/storage": "1.7.0",
"@google-cloud/storage": "2.3.1",
"busboy": "^0.2.14",
"escape-html": "^1.0.3",
"safe-buffer": "5.1.2"
Expand Down
46 changes: 35 additions & 11 deletions functions/http/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,39 @@ const Buffer = require('safe-buffer').Buffer;
const proxyquire = require(`proxyquire`).noCallThru();
const sinon = require(`sinon`);
const test = require(`ava`);
const uuid = require(`uuid`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

function getSample() {
function getSample () {
const requestPromise = sinon
.stub()
.returns(new Promise(resolve => resolve(`test`)));

return {
sample: proxyquire(`../`, {
'request-promise': requestPromise,
'request-promise': requestPromise
}),
mocks: {
requestPromise: requestPromise,
},
requestPromise: requestPromise
}
};
}

function getMocks() {
function getMocks () {
const req = {
headers: {},
get: function(header) {
get: function (header) {
return this.headers[header];
},
}
};
sinon.spy(req, `get`);

const corsPreflightReq = {
method: 'OPTIONS',
method: 'OPTIONS'
};

const corsMainReq = {
method: 'GET',
method: 'GET'
};

return {
Expand All @@ -62,8 +63,8 @@ function getMocks() {
send: sinon.stub().returnsThis(),
json: sinon.stub().returnsThis(),
end: sinon.stub().returnsThis(),
status: sinon.stub().returnsThis(),
},
status: sinon.stub().returnsThis()
}
};
}

Expand Down Expand Up @@ -222,3 +223,26 @@ test.serial(`http:cors: should respond to main request (auth)`, t => {

t.true(mocks.res.send.calledOnceWith(`Hello World!`));
});

test.serial(`http:getSignedUrl: should process example request`, async t => {
const mocks = getMocks();
const httpSample = getSample();

const reqMock = {
method: 'POST',
body: {
bucket: 'nodejs-docs-samples',
filename: `gcf-gcs-url-${uuid.v4}`,
contentType: 'application/octet-stream'
}
};

httpSample.sample.getSignedUrl(reqMock, mocks.res);

// Instead of modifying the sample to return a promise,
// use a delay here and keep the sample idiomatic
await new Promise(resolve => setTimeout(resolve, 300));

t.false(mocks.res.status.called);
t.true(mocks.res.send.calledOnce);
});