Skip to content

Commit 4c7b3a9

Browse files
committed
Makes trailing slash redirect only apply to GET requests
1 parent cddc8a3 commit 4c7b3a9

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

lib/gateway.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Gateway extends EventEmitter {
2525
this.name = cfg.name || DEFAULT_NAME;
2626
this.maxRequestSizeMB = cfg.maxRequestSizeMB || DEFAULT_MAX_REQUEST_SIZE_MB;
2727
this.supportedMethods = {'GET': true, 'POST': true, 'OPTIONS': true, 'HEAD': true};
28+
this.trailingSlashRedirectMethods = {'GET': true};
2829
this.supportedLogTypes = {'global': '***', 'info': ':::', 'error': '<!>', 'result': '>>>'};
2930
this.defaultLogType = 'info';
3031
this.supportedBgModes = background.modes;
@@ -587,7 +588,12 @@ class Gateway extends EventEmitter {
587588
this.log(req, `Background Function Initiated`);
588589
}
589590

590-
if (req.headers['user-agent'] && !pathname.endsWith('/') && pathname.split('/').pop().indexOf('.') === -1) {
591+
if (
592+
this.trailingSlashRedirectMethods[req.method] &&
593+
req.headers['user-agent'] &&
594+
!pathname.endsWith('/') &&
595+
pathname.split('/').pop().indexOf('.') === -1
596+
) {
591597
this.log(req, `Redirect`);
592598
if (pathinfo.length === 2) {
593599
pathinfo[0] = pathname + '/';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "functionscript",
3-
"version": "1.7.1",
3+
"version": "1.8.0",
44
"description": "A language and specification for turning JavaScript functions into typed HTTP APIs",
55
"author": "Keith Horwood <[email protected]>",
66
"main": "index.js",

tests/gateway/tests.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module.exports = (expect) => {
7979
});
8080
});
8181

82-
it('Should return 302 redirect when missing trailing / with user agent', done => {
82+
it('Should return 302 redirect on GET request when missing trailing / with user agent', done => {
8383
request('GET', {'user-agent': 'testing'}, '/my_function', '', (err, res, result) => {
8484

8585
expect(err).to.not.exist;
@@ -94,7 +94,7 @@ module.exports = (expect) => {
9494
});
9595
});
9696

97-
it('Should not return 302 redirect when missing trailing / without user agent', done => {
97+
it('Should not return 302 redirect on a GET request when missing trailing / without user agent', done => {
9898
request('GET', {}, '/my_function', '', (err, res, result) => {
9999

100100
expect(err).to.not.exist;
@@ -107,6 +107,19 @@ module.exports = (expect) => {
107107
});
108108
});
109109

110+
it('Should not return 302 redirect for POST request with trailing slash with user agent', done => {
111+
request('POST', {'user-agent': 'testing'}, '/my_function', '', (err, res, result) => {
112+
113+
expect(err).to.not.exist;
114+
expect(res.statusCode).to.not.equal(302);
115+
expect(res.headers).to.haveOwnProperty('access-control-allow-origin');
116+
expect(res.headers).to.haveOwnProperty('access-control-allow-headers');
117+
expect(res.headers).to.haveOwnProperty('access-control-expose-headers');
118+
done();
119+
120+
});
121+
});
122+
110123
it('Should give 200 OK and property headers for OPTIONS', done => {
111124
request('OPTIONS', {}, '/my_function/', {}, (err, res, result) => {
112125

@@ -695,8 +708,8 @@ module.exports = (expect) => {
695708
});
696709
});
697710

698-
it('Should return 302 redirect with correct url when running a background function missing a slash before :bg and at end of url', done => {
699-
request('POST', {'user-agent': 'testing'}, '/bg:bg', '', (err, res, result) => {
711+
it('Should return 302 redirect on a GET request with correct url when running a background function missing a slash before :bg and at end of url', done => {
712+
request('GET', {'user-agent': 'testing'}, '/bg:bg', '', (err, res, result) => {
700713

701714
expect(err).to.not.exist;
702715
expect(res.statusCode).to.equal(302);
@@ -710,8 +723,8 @@ module.exports = (expect) => {
710723
});
711724
});
712725

713-
it('Should return 302 redirect with correct url when running a background function missing a slash before :bg but with slash at end of url', done => {
714-
request('POST', {'user-agent': 'testing'}, '/bg:bg/', '', (err, res, result) => {
726+
it('Should return 302 redirect on a GET request with correct url when running a background function missing a slash before :bg but with slash at end of url', done => {
727+
request('GET', {'user-agent': 'testing'}, '/bg:bg/', '', (err, res, result) => {
715728

716729
expect(err).to.not.exist;
717730
expect(res.statusCode).to.equal(302);
@@ -725,8 +738,8 @@ module.exports = (expect) => {
725738
});
726739
});
727740

728-
it('Should return 302 redirect with correct url when running a background function missing a slash before :bg and at end of url with a query', done => {
729-
request('POST', {'user-agent': 'testing'}, '/bg:bg?test=param', '', (err, res, result) => {
741+
it('Should return 302 redirect on a GET request with correct url when running a background function missing a slash before :bg and at end of url with a query', done => {
742+
request('GET', {'user-agent': 'testing'}, '/bg:bg?test=param', '', (err, res, result) => {
730743

731744
expect(err).to.not.exist;
732745
expect(res.statusCode).to.equal(302);
@@ -740,8 +753,8 @@ module.exports = (expect) => {
740753
});
741754
});
742755

743-
it('Should return 302 redirect with correct url when running a background function missing a slash before :bg but with slash at end of url with a query', done => {
744-
request('POST', {'user-agent': 'testing'}, '/bg:bg/?test=param', '', (err, res, result) => {
756+
it('Should return 302 redirect on a GET request with correct url when running a background function missing a slash before :bg but with slash at end of url with a query', done => {
757+
request('GET', {'user-agent': 'testing'}, '/bg:bg/?test=param', '', (err, res, result) => {
745758

746759
expect(err).to.not.exist;
747760
expect(res.statusCode).to.equal(302);

0 commit comments

Comments
 (0)