Verified Commit 5d8d95ec authored by Sebastiaan Deckers's avatar Sebastiaan Deckers 馃悜

fix: parse query string for custom headers

parent 920c1c52
......@@ -23,6 +23,7 @@ function checkInvalidHeaderChar (val) {
module.exports.buildHeaders = function buildHeaders (
sourceFile,
urlPath,
supportedEncodings,
fileIndex,
options
......@@ -56,7 +57,7 @@ module.exports.buildHeaders = function buildHeaders (
for (const { uri, fields } of options.headers) {
if (uri) {
const predicate = new UriTemplate(uri)
const parts = predicate.fromUri(sourceFile.pathname)
const parts = predicate.fromUri(urlPath)
const replacer = (match, key) => parts[key]
if (parts !== undefined) {
for (const [name, value] of Object.entries(fields)) {
......
......@@ -34,6 +34,7 @@ async function serveDependencies (request, response) {
pushQueue.defer((callback) => {
const { headers, resolved } = buildHeaders(
dependency,
dependency.pathname,
supportedEncodings,
fileIndex,
request.options
......
......@@ -9,6 +9,7 @@ module.exports.serveResponse = () => {
return async function serveResponse (request, response, next) {
const { resolved, headers } = buildHeaders(
request.resolved,
request.url,
new Negotiator(request).encodings(),
request.fileIndex,
request.options
......
......@@ -29,6 +29,12 @@ test('start server', async (t) => {
'mixed-case': 'bar',
'MIXED-CASE': 'bla'
}
},
{
uri: '/stuff.txt{?query,}',
fields: {
'parse-query-string': 'query parameter is {query}'
}
}
]
}]
......@@ -55,4 +61,13 @@ test('Mixed-case equivalent headers are concatenated', async (t) => {
t.notOk(response.headers.get('MIXED-CASE'))
})
test('Parse the query string and use values in field templates', async (t) => {
const url = 'https://example.net:8443/stuff.txt?foo=bar&query=yep&then=end'
const response = await h2(url)
t.is(
response.headers.get('parse-query-string'),
'query parameter is yep'
)
})
test('stop server', async (t) => master.close())
......@@ -60,8 +60,9 @@ function receiveHttp2 (url, options = {}) {
callback(null, '127.0.0.1', 4)
}
}, options))
const { pathname, search } = new URL(url)
const headers = Object.assign(
{ ':path': new URL(url).pathname },
{ ':path': pathname + search },
options.headers
)
const request = session.request(headers)
......
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