-
Notifications
You must be signed in to change notification settings - Fork 126
fix(sendFile): surface resolved path in "No such file" error; clarify no auto-versioning (#257) #330
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
Open
naorpeled
wants to merge
2
commits into
jeremydaly:main
Choose a base branch
from
naorpeled:fix/257-no-versioning-clarify-sendfile-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(sendFile): surface resolved path in "No such file" error; clarify no auto-versioning (#257) #330
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| 'use strict'; | ||
|
|
||
| // Regression coverage for GitHub issue #257 ("Is it possible to disable versioning?"). | ||
| // | ||
| // lambda-api does NOT perform any automatic version detection or stripping. A | ||
| // version-like path segment (e.g. `v1`) is matched literally and exposed on | ||
| // `req.params` just like any other parameter value. These tests lock that in so | ||
| // the behavior can never silently regress. | ||
|
|
||
| // Init API instance (no base -- matching the reporter's setup) | ||
| const api = require('../index')(); | ||
|
|
||
| // Sample events for the other supported interfaces. Route matching must not | ||
| // diverge across API Gateway v1 (`path`), API Gateway v2 (`rawPath`) and ALB | ||
| // (`path` + `requestContext.elb`), so the same version-like segment is asserted | ||
| // against each shape below. Deep-cloned per test so overrides don't leak. | ||
| const sampleV2 = require('./sample-event-apigateway-v2.json'); | ||
| const sampleAlb = require('./sample-event-alb1.json'); | ||
| const clone = (obj) => JSON.parse(JSON.stringify(obj)); | ||
|
|
||
| // NOTE: Set test to true | ||
| api._test = true; | ||
|
|
||
| let event = { | ||
| httpMethod: 'get', | ||
| path: '/', | ||
| body: {}, | ||
| multiValueHeaders: { | ||
| 'Content-Type': ['application/json'] | ||
| } | ||
| } | ||
|
|
||
| /******************************************************************************/ | ||
| /*** DEFINE TEST ROUTES ***/ | ||
| /******************************************************************************/ | ||
| api.get('/:v/schema.json', function(req,res) { | ||
| res.status(200).json({ method: 'get', status: 'ok', v: req.params.v }) | ||
| }) | ||
|
|
||
| /******************************************************************************/ | ||
| /*** BEGIN TESTS ***/ | ||
| /******************************************************************************/ | ||
|
|
||
| describe('Versioning (issue #257) Tests:', function() { | ||
|
|
||
| it('Version-like segment is matched literally: /v1/schema.json', async function() { | ||
| let _event = Object.assign({},event,{ path: '/v1/schema.json' }) | ||
| let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) | ||
| expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok","v":"v1"}', isBase64Encoded: false }) | ||
| }) // end it | ||
|
|
||
| it('API Gateway v2 (rawPath) matches the version-like segment literally: /v1/schema.json', async function() { | ||
| let _event = clone(sampleV2) | ||
| _event.rawPath = '/v1/schema.json' | ||
| _event.requestContext.http.method = 'GET' | ||
| _event.requestContext.http.path = '/v1/schema.json' | ||
| let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) | ||
| expect(result.statusCode).toBe(200) | ||
| expect(JSON.parse(result.body).v).toBe('v1') | ||
| }) // end it | ||
|
|
||
| it('ALB event (path) matches the version-like segment literally: /v1/schema.json', async function() { | ||
| let _event = clone(sampleAlb) | ||
| _event.httpMethod = 'GET' | ||
| _event.path = '/v1/schema.json' | ||
| _event.isBase64Encoded = false | ||
| let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) | ||
| expect(result.statusCode).toBe(200) | ||
| expect(JSON.parse(result.body).v).toBe('v1') | ||
| }) // end it | ||
|
|
||
| it('Non-version first segment is matched the same way: /abc/schema.json', async function() { | ||
| let _event = Object.assign({},event,{ path: '/abc/schema.json' }) | ||
| let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) | ||
| expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok","v":"abc"}', isBase64Encoded: false }) | ||
| }) // end it | ||
|
|
||
| it('No version segment is consumed -- extra segment still 404s: /v1/x/schema.json', async function() { | ||
| let _event = Object.assign({},event,{ path: '/v1/x/schema.json' }) | ||
| let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) | ||
| expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 404, body: '{"error":"Route not found"}', isBase64Encoded: false }) | ||
| }) // end it | ||
|
|
||
| it("Setting base to a version collides with a leading :param (documented caveat)", async function() { | ||
| // With `base: 'v1'`, the literal `v1` base segment consumes the first path | ||
| // segment, so `/v1/schema.json` matches `v1` + `:v` (schema.json) and never | ||
| // reaches the handler -- yielding a 405. Prefer a base like `/api` over a | ||
| // version, or use `register({ prefix })` for versioned route groups. | ||
| const basedApi = require('../index')({ base: 'v1' }) | ||
| basedApi._test = true; | ||
| basedApi.get('/:v/schema.json', function(req,res) { | ||
| res.status(200).json({ v: req.params.v }) | ||
| }) | ||
|
|
||
| let _event = Object.assign({},event,{ path: '/v1/schema.json' }) | ||
| let result = await new Promise(r => basedApi.run(_event,{},(e,res) => { r(res) })) | ||
| expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false }) | ||
| }) // end it | ||
|
|
||
| }) // end VERSIONING tests | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.