Skip to content

Commit

Permalink
fix: path access with trailing slash (#1606)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk authored Oct 31, 2022
1 parent 4a2df15 commit 73b95bc
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/events/http/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export default class HttpServer {
const serverOptions = {
host,
port: httpPort,
router: {
stripTrailingSlash: true,
},
state: enforceSecureCookies
? {
isHttpOnly: true,
Expand Down
3 changes: 3 additions & 0 deletions src/events/websocket/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export default class HttpServer {
const serverOptions = {
host,
port: websocketPort,
router: {
stripTrailingSlash: true,
},
// https support
...(httpsProtocol != null && {
tls: await this.#loadCerts(httpsProtocol),
Expand Down
117 changes: 113 additions & 4 deletions tests/endToEnd/optionParameters/optionParameters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('noPrependStageInUrl option', function desc() {

afterEach(() => teardown())

describe('when --noPrependStageInUrl is used, and the stage is not in the url', () => {
describe('when --noPrependStageInUrl is used', () => {
it('it should return a payload', async () => {
const url = new URL('/hello', BASE_URL)
const response = await fetch(url)
Expand All @@ -26,10 +26,28 @@ describe('noPrependStageInUrl option', function desc() {
foo: 'bar',
})
})
})

describe('when --noPrependStageInUrl is used, and the stage is not in the url', () => {
it('noPrependStageInUrl 2', async () => {
it('it should return a payload with no path', async () => {
const url = new URL('/', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, {
foo: 'bar',
})
})

it('it should return a payload when accessed with trailing slash', async () => {
const url = new URL('/hello/', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, {
foo: 'bar',
})
})

it('when --noPrependStageInUrl is used it should return a 404', async () => {
const url = new URL('/dev/hello', BASE_URL)
const response = await fetch(url)
const json = await response.json()
Expand Down Expand Up @@ -59,5 +77,96 @@ describe('prefix option', function desc() {
foo: 'bar',
})
})

it('the prefixed path should return a payload when accessed with trailing slash', async () => {
const url = new URL('/someprefix/dev/hello/', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, {
foo: 'bar',
})
})

it('the prefixed path should return a payload with no path', async () => {
const url = new URL('/someprefix/dev', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, {
foo: 'bar',
})
})

it('the prefixed path should return a payload with no path and trailing slash', async () => {
const url = new URL('/someprefix/dev/', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, {
foo: 'bar',
})
})
})
})

describe('noPrependStageInUrl option with prefix option', function desc() {
beforeEach(() =>
setup({
args: ['--noPrependStageInUrl', '--prefix', 'someprefix'],
servicePath: resolve(__dirname, 'src'),
}),
)

afterEach(() => teardown())

describe('when --noPrependStageInUrl and --prefix is used', () => {
it('it should return a payload', async () => {
const url = new URL('/someprefix/hello', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, {
foo: 'bar',
})
})

it('it should return a payload with no path', async () => {
const url = new URL('/someprefix', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, {
foo: 'bar',
})
})

it('it should return a payload with no path and trailing slash', async () => {
const url = new URL('/someprefix/', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, {
foo: 'bar',
})
})

it('it should return a payload when accessed with trailing slash', async () => {
const url = new URL('/someprefix/hello/', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, {
foo: 'bar',
})
})

it('when --noPrependStageInUrl is used it should return a 404', async () => {
const url = new URL('/someprefix/dev/hello', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.equal(json.statusCode, 404)
})
})
})
7 changes: 7 additions & 0 deletions tests/endToEnd/optionParameters/src/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ functions:
method: get
path: /hello
handler: handler.hello

foobar:
events:
- http:
method: get
path: '/'
handler: handler.hello

0 comments on commit 73b95bc

Please sign in to comment.