Commit 05481965 authored by Sebastiaan Deckers's avatar Sebastiaan Deckers 馃悜

fix: never set pseudo-header in response.writeHead

parent 83fa0ab0
......@@ -21,9 +21,6 @@ module.exports.serveResponse = () => {
request.once('error', next)
response.once('error', next)
response.stream.once('error', next)
if (statusCode !== 200) {
headers[':status'] = statusCode
}
if (request.method === 'HEAD') {
try {
response.setHeader(
......@@ -36,6 +33,7 @@ module.exports.serveResponse = () => {
response.writeHead(statusCode, headers)
response.end()
} else {
headers[':status'] = statusCode
response.stream.respondWithFile(
resolved.absolute,
Object.assign(response.getHeaders(), headers),
......
......@@ -62,7 +62,10 @@ function receiveHttp2 (url, options = {}) {
}, options))
const { pathname, search } = new URL(url)
const headers = Object.assign(
{ ':path': pathname + search },
{
':path': pathname + search,
':method': options.method || 'GET'
},
options.headers
)
const request = session.request(headers)
......
const test = require('blue-tape')
const { Master } = require('..')
const { join } = require('path')
const { h1 } = require('./helpers/receive')
const { h1, h2 } = require('./helpers/receive')
const { constants: {
HTTP_STATUS_OK,
HTTP_STATUS_BAD_REQUEST,
......@@ -13,19 +13,26 @@ const http = 'http://localhost:8080/'
const https = 'https://localhost:8443/'
const fixtures = [
{ url: http, method: 'GET', status: HTTP_STATUS_PERMANENT_REDIRECT },
{ url: http, method: 'HEAD', status: HTTP_STATUS_PERMANENT_REDIRECT },
{ url: http, method: 'OPTIONS', status: HTTP_STATUS_PERMANENT_REDIRECT },
{ url: http, method: 'POST', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ url: http, method: 'PUT', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ url: http, method: 'PATCH', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ url: https, method: 'GET', status: HTTP_STATUS_OK },
{ url: https, method: 'HEAD', status: HTTP_STATUS_OK },
{ url: https, method: 'OPTIONS', status: HTTP_STATUS_OK },
{ url: https, method: 'POST', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ url: https, method: 'PUT', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ url: https, method: 'PATCH', status: HTTP_STATUS_METHOD_NOT_ALLOWED }
{ agent: h1, url: http, method: 'GET', status: HTTP_STATUS_PERMANENT_REDIRECT },
{ agent: h1, url: http, method: 'HEAD', status: HTTP_STATUS_PERMANENT_REDIRECT },
{ agent: h1, url: http, method: 'OPTIONS', status: HTTP_STATUS_PERMANENT_REDIRECT },
{ agent: h1, url: http, method: 'POST', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ agent: h1, url: http, method: 'PUT', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ agent: h1, url: http, method: 'PATCH', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ agent: h1, url: https, method: 'GET', status: HTTP_STATUS_OK },
{ agent: h1, url: https, method: 'HEAD', status: HTTP_STATUS_OK },
{ agent: h1, url: https, method: 'OPTIONS', status: HTTP_STATUS_OK },
{ agent: h1, url: https, method: 'POST', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ agent: h1, url: https, method: 'PUT', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ agent: h1, url: https, method: 'PATCH', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ agent: h2, url: https, method: 'GET', status: HTTP_STATUS_OK },
{ agent: h2, url: https, method: 'HEAD', status: HTTP_STATUS_OK },
{ agent: h2, url: https, method: 'OPTIONS', status: HTTP_STATUS_OK },
{ agent: h2, url: https, method: 'POST', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ agent: h2, url: https, method: 'PUT', status: HTTP_STATUS_METHOD_NOT_ALLOWED },
{ agent: h2, url: https, method: 'PATCH', status: HTTP_STATUS_METHOD_NOT_ALLOWED }
]
let master
......@@ -36,9 +43,11 @@ test('start server', async (t) => {
await master.listen()
})
for (const { method, url, status } of fixtures) {
test(`${method} ${url} responds with ${status}`, async (t) => {
const response = await h1(url, { method })
for (const { agent, method, url, status } of fixtures) {
const protocols = new Map([[h1, 'HTTP/1'], [h2, 'HTTP/2']])
const protocol = protocols.get(agent)
test(`${protocol} ${method} ${url}`, async (t) => {
const response = await agent(url, { method })
t.is(response.status, status)
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment