Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sample for handling CORS requests. #721

Merged
merged 5 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 54 additions & 0 deletions functions/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,57 @@ exports.getSignedUrl = (req, res) => {
}
};
// [END functions_http_signed_url]

// [START functions_http_cors]
/**
* HTTP function that supports CORS requests.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.corsEnabledFunction = (req, res) => {
// Set CORS headers for preflight requests
// Allows GETs from any origin with the Content-Type header
// and caches preflight response for 3600s

res.set('Access-Control-Allow-Origin', '*');

if (req.method === 'OPTIONS') {
// Send response to OPTIONS requests
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Max-Age', '3600');
res.status(204).send('');
} else {
// Set CORS headers for the main request
res.set('Access-Control-Allow-Origin', '*');
res.send('Hello World!');
}
};
// [END functions_http_cors]

// [START functions_http_cors_auth]
/**
* HTTP function that supports CORS requests with credentials.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.corsEnabledFunctionAuth = (req, res) => {
// Set CORS headers for preflight requests
// Allows GETs from origin https://mydomain.com with Authorization header

res.set('Access-Control-Allow-Origin', 'https://mydomain.com');
res.set('Access-Control-Allow-Credentials', 'true');

if (req.method === 'OPTIONS') {
// Send response to OPTIONS requests
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'Authorization');
res.set('Access-Control-Max-Age', '3600');
res.status(204).send('');
} else {
res.send('Hello World!');
}
};
// [END functions_http_cors_auth]
49 changes: 49 additions & 0 deletions functions/http/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,20 @@ function getMocks () {
};
sinon.spy(req, `get`);

const corsPreflightReq = {
method: 'OPTIONS'
};

const corsMainReq = {
method: 'GET'
};

return {
req: req,
corsPreflightReq: corsPreflightReq,
corsMainReq: corsMainReq,
res: {
set: sinon.stub().returnsThis(),
send: sinon.stub().returnsThis(),
json: sinon.stub().returnsThis(),
end: sinon.stub().returnsThis(),
Expand Down Expand Up @@ -181,3 +192,41 @@ test.serial(`http:helloContent: should handle other`, (t) => {
t.true(mocks.res.send.calledOnce);
t.deepEqual(mocks.res.send.firstCall.args[0], `Hello World!`);
});

test.serial(`http:cors: should respond to preflight request (no auth)`, (t) => {
const mocks = getMocks();
const httpSample = getSample();

httpSample.sample.corsEnabledFunction(mocks.corsPreflightReq, mocks.res);

t.true(mocks.res.status.calledOnceWith(204));
t.true(mocks.res.send.called);
});

test.serial(`http:cors: should respond to main request (no auth)`, (t) => {
const mocks = getMocks();
const httpSample = getSample();

httpSample.sample.corsEnabledFunction(mocks.corsMainReq, mocks.res);

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

test.serial(`http:cors: should respond to preflight request (auth)`, (t) => {
const mocks = getMocks();
const httpSample = getSample();

httpSample.sample.corsEnabledFunctionAuth(mocks.corsPreflightReq, mocks.res);

t.true(mocks.res.status.calledOnceWith(204));
t.true(mocks.res.send.calledOnce);
});

test.serial(`http:cors: should respond to main request (auth)`, (t) => {
const mocks = getMocks();
const httpSample = getSample();

httpSample.sample.corsEnabledFunctionAuth(mocks.corsMainReq, mocks.res);

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