Skip to content

Commit

Permalink
Merge pull request #42 from bsdkurt/master
Browse files Browse the repository at this point in the history
Correct path for non-custom endpoints
  • Loading branch information
dougmoscrop committed Mar 26, 2018
2 parents d668f99 + cee134b commit 149f53f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/clean-up-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module.exports = function cleanupEvent(evt) {
const event = evt || {};

event.httpMethod = event.httpMethod || 'GET';
event.path = event.path || '/';
event.body = event.body || '';
event.headers = event.headers || {};
event.requestContext = event.requestContext || {};
event.requestContext.path = event.requestContext.path || '/';
event.requestContext.identity = event.requestContext.identity || {};

return event;
Expand Down
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = class ServerlessRequest extends http.IncomingMessage {
method: event.httpMethod,
headers: headers,
url: url.format({
pathname: event.path,
pathname: event.requestContext.path,
query: event.queryStringParameters
})
});
Expand Down
28 changes: 20 additions & 8 deletions test/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('express', () => {

return request(app, {
httpMethod: 'GET',
path: '/'
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(418);
Expand All @@ -37,7 +39,9 @@ describe('express', () => {

return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
body: 'hello, world',
headers: {
'Content-Type': 'text/plain',
Expand All @@ -58,7 +62,9 @@ describe('express', () => {

return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
body: JSON.stringify({
hello: 'world'
}),
Expand All @@ -79,7 +85,9 @@ describe('express', () => {

return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
queryStringParameters: {
foo: 'bar'
}
Expand All @@ -100,7 +108,9 @@ describe('express', () => {

return request(app, {
httpMethod: 'PUT',
path: '/',
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(201);
Expand All @@ -113,7 +123,9 @@ describe('express', () => {

return request(app, {
httpMethod: 'GET',
path: '/file.txt',
requestContext: {
path: '/file.txt',
}
})
.then(response => {
expect(response.statusCode).to.equal(200);
Expand All @@ -130,11 +142,11 @@ describe('express', () => {

return request(app, {
httpMethod: 'GET',
path: '/',
headers: {
authorization: 'Basic QWxhZGRpbjpPcGVuU2VzYW1l'
},
requestContext: {
path: '/',
identity: {
sourceIp: '1.3.3.7'
}
Expand All @@ -153,11 +165,11 @@ describe('express', () => {

return request(app, {
httpMethod: 'GET',
path: '/',
headers: {
authorization: 'Basic QWxhZGRpbjpPcGVuU2VzYW1l'
},
requestContext: {
path: '/',
identity: {
sourceIp: '1.3.3.7'
}
Expand Down
24 changes: 18 additions & 6 deletions test/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ describe('generic http listener', () => {

return request(app, {
httpMethod: 'GET',
path: '/'
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(418);
Expand All @@ -42,7 +44,9 @@ describe('generic http listener', () => {

return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
body: 'hello, world'
})
.then(response => {
Expand All @@ -62,7 +66,9 @@ describe('generic http listener', () => {

return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
queryStringParameters: {
foo: 'bar'
}
Expand All @@ -84,7 +90,9 @@ describe('generic http listener', () => {

return request(app, {
httpMethod: 'PUT',
path: '/',
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(200);
Expand All @@ -102,7 +110,9 @@ describe('generic http listener', () => {

return request(app, {
httpMethod: 'GET',
path: '/file.txt',
requestContext: {
path: '/file.txt'
}
})
.then(response => {
expect(response.statusCode).to.equal(200);
Expand All @@ -124,7 +134,9 @@ describe('generic http listener', () => {

return request(app, {
httpMethod: 'GET',
path: '/'
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(200);
Expand Down
72 changes: 54 additions & 18 deletions test/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ describe('koa', () => {

return request(app, {
httpMethod: 'GET',
path: '/'
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(418);
Expand All @@ -41,7 +43,9 @@ describe('koa', () => {

return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
queryStringParameters: {
x: 'y'
}
Expand All @@ -61,7 +65,9 @@ describe('koa', () => {

return request(app, {
httpMethod: 'GET',
path: '/'
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(201);
Expand All @@ -78,7 +84,9 @@ describe('koa', () => {

return request(app, {
httpMethod: 'GET',
path: '/'
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(200);
Expand All @@ -100,7 +108,9 @@ describe('koa', () => {

return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
headers: {
'X-Request-Id': 'abc'
}
Expand All @@ -117,7 +127,9 @@ describe('koa', () => {
});
return request(app, {
httpMethod: 'GET',
path: '/'
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(500);
Expand All @@ -131,7 +143,9 @@ it('auth middleware should set statusCode 401', () => {
});
return request(app, {
httpMethod: 'GET',
path: '/'
requestContext: {
path: '/'
}
})
.then(response => {
expect(response.statusCode).to.equal(401);
Expand All @@ -157,7 +171,9 @@ it('auth middleware should set statusCode 401', () => {
it('should get path information when it matches exactly', () => {
return request(app, {
httpMethod: 'GET',
path: '/foo'
requestContext: {
path: '/foo'
}
})
.then(response => {
expect(response.statusCode).to.equal(200);
Expand All @@ -168,7 +184,9 @@ it('auth middleware should set statusCode 401', () => {
it('should get path information when it matches with params', () => {
return request(app, {
httpMethod: 'GET',
path: '/foo/baz'
requestContext: {
path: '/foo/baz'
}
})
.then(response => {
expect(response.statusCode).to.equal(200);
Expand All @@ -179,7 +197,9 @@ it('auth middleware should set statusCode 401', () => {
it('should get method information', () => {
return request(app, {
httpMethod: 'POST',
path: '/foo'
requestContext: {
path: '/foo'
}
})
.then(response => {
expect(response.statusCode).to.equal(201);
Expand All @@ -190,7 +210,9 @@ it('auth middleware should set statusCode 401', () => {
it('should allow 404s', () => {
return request(app, {
httpMethod: 'POST',
path: '/missing'
requestContext: {
path: '/missing'
}
})
.then(response => {
expect(response.statusCode).to.equal(404);
Expand Down Expand Up @@ -222,7 +244,9 @@ it('auth middleware should set statusCode 401', () => {
it('should get when it matches', function() {
return request(app, {
httpMethod: 'GET',
path: '/'
requestContext: {
path: '/'
}
})
.then((response) => {
expect(response.statusCode).to.equal(200);
Expand All @@ -233,7 +257,9 @@ it('auth middleware should set statusCode 401', () => {
it('should 404 when route does not match', function() {
return request(app, {
httpMethod: 'GET',
path: '/missing'
requestContext: {
path: '/missing'
}
})
.then((response) => {
expect(response.statusCode).to.equal(404);
Expand Down Expand Up @@ -262,7 +288,9 @@ it('auth middleware should set statusCode 401', () => {
});
return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
headers: {
'Content-Type': 'application/json',
'Content-Length': body.length
Expand Down Expand Up @@ -292,7 +320,9 @@ it('auth middleware should set statusCode 401', () => {
.then((zipped) => {
return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
headers: {
'Content-Type': 'application/json',
'Content-Encoding': 'gzip',
Expand All @@ -317,7 +347,9 @@ it('auth middleware should set statusCode 401', () => {
});
return request(app, {
httpMethod: 'DELETE',
path: '/',
requestContext: {
path: '/'
},
headers: {
'Content-Type': 'application/json'
}
Expand All @@ -337,7 +369,9 @@ it('auth middleware should set statusCode 401', () => {
it('should serve a text file', () => {
return request(app, {
httpMethod: 'GET',
path: '/test/file.txt'
requestContext: {
path: '/test/file.txt'
}
})
.then((response) => {
expect(response.body).to.equal('this is a test\n');
Expand All @@ -359,7 +393,9 @@ it('auth middleware should set statusCode 401', () => {
it('should serve compressed text (base64 encoded)', () => {
return request(app, {
httpMethod: 'GET',
path: '/',
requestContext: {
path: '/'
},
headers: {
'accept-encoding': 'deflate'
}
Expand Down

0 comments on commit 149f53f

Please sign in to comment.