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

feat: allow HEAD & OPTIONS for plaintext http

parent b7149207
......@@ -15,7 +15,7 @@ module.exports.redirect = async (options) => {
const app = connect()
app.use(fingerprint(options))
app.use(logger(options.log))
app.use(allowedMethods(['GET']))
app.use(allowedMethods(['GET', 'HEAD', 'OPTIONS']))
app.use(cdnLoopPrevention(options.via))
app.use(ACME_CHALLENGE, acmeChallenge(options))
app.use(redirectHttps(options))
......
const test = require('blue-tape')
const { Master } = require('..')
const { join } = require('path')
const { connect } = require('http2')
const { h1 } = require('./helpers/receive')
const { constants: {
HTTP_STATUS_OK,
HTTP_STATUS_BAD_REQUEST,
HTTP_STATUS_PERMANENT_REDIRECT,
HTTP_STATUS_METHOD_NOT_ALLOWED
} } = require('http2')
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: http, method: 'FOOBAR', status: HTTP_STATUS_BAD_REQUEST },
{ 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 },
{ url: https, method: 'FOOBAR', status: HTTP_STATUS_BAD_REQUEST }
]
let master
test('start server', async (t) => {
......@@ -11,45 +38,11 @@ test('start server', async (t) => {
await master.listen()
})
const HTTP_STATUS = {
OK: 200,
METHOD_NOT_ALLOWED: 405
}
const fixtures = [
{ method: 'GET', status: HTTP_STATUS.OK },
{ method: 'HEAD', status: HTTP_STATUS.OK },
{ method: 'OPTIONS', status: HTTP_STATUS.OK },
{ method: 'POST', status: HTTP_STATUS.METHOD_NOT_ALLOWED },
{ method: 'PUT', status: HTTP_STATUS.METHOD_NOT_ALLOWED },
{ method: 'PATCH', status: HTTP_STATUS.METHOD_NOT_ALLOWED },
{ method: 'FOOBAR', status: HTTP_STATUS.METHOD_NOT_ALLOWED }
]
let session
test('Connect to server', async (t) => {
session = connect(
'https://localhost:8443',
{ rejectUnauthorized: false }
)
session.on('socketError', t.fail)
session.on('error', t.fail)
})
for (const { method, status } of fixtures) {
test(`${method} method returns status code ${status}`, (t) => {
const stream = session.request({ ':method': method, ':path': '/' })
stream.on('response', (headers) => {
t.is(headers[':status'], status)
stream.on('close', () => t.end())
stream.on('end', () => stream.close())
stream.resume()
})
stream.on('error', t.end)
for (const { method, url, status } of fixtures) {
test(`${method} ${url} responds with ${status}`, async (t) => {
const response = await h1(url, { method })
t.is(response.status, status)
})
}
test('Disconnect from server', (t) => session.close(t.end))
test('stop server', async (t) => master.close())
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