Skip to content

Commit

Permalink
Support paths within base (#148)
Browse files Browse the repository at this point in the history
This use case is relied upon by fast-proxy and it was working with
the Node.js core client but not with Undici.
  • Loading branch information
mcollina authored Feb 26, 2021
1 parent f8dc007 commit 7fb25fb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ function buildRequest (opts) {
'unix+http:': { base: http, request: unixRequest },
'unix+https:': { base: https, request: unixRequest }
}
const baseUrl = opts.base
const http2Opts = getHttp2Opts(opts)
const httpOpts = getHttpOpts(opts)
const baseUrl = opts.base && new URL(opts.base).origin
const undiciOpts = opts.undici || {}
let http2Client
let undiciAgent
Expand Down
50 changes: 50 additions & 0 deletions test/core-with-path-in-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

const t = require('tap')
const Fastify = require('fastify')
const From = require('..')
const http = require('http')
const get = require('simple-get').concat

const instance = Fastify()

t.plan(11)
t.tearDown(instance.close.bind(instance))

const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'GET')
t.equal(req.url, '/hello')
t.equal(req.headers.connection, 'close')
res.statusCode = 205
res.setHeader('Content-Type', 'text/plain')
res.setHeader('x-my-header', 'hello!')
res.end('hello world')
})

instance.get('/', (request, reply) => {
reply.from('/hello')
})

t.tearDown(target.close.bind(target))

target.listen(0, (err) => {
t.error(err)

instance.register(From, {
base: `http://localhost:${target.address().port}/hello`,
http: true
})

instance.listen(0, (err) => {
t.error(err)

get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
t.error(err)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.headers['x-my-header'], 'hello!')
t.equal(res.statusCode, 205)
t.equal(data.toString(), 'hello world')
})
})
})
50 changes: 50 additions & 0 deletions test/undici-with-path-in-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

const t = require('tap')
const Fastify = require('fastify')
const From = require('..')
const http = require('http')
const get = require('simple-get').concat

const instance = Fastify()

t.plan(11)
t.tearDown(instance.close.bind(instance))

const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'GET')
t.equal(req.url, '/hello')
t.equal(req.headers.connection, 'keep-alive')
res.statusCode = 205
res.setHeader('Content-Type', 'text/plain')
res.setHeader('x-my-header', 'hello!')
res.end('hello world')
})

instance.get('/', (request, reply) => {
reply.from('/hello')
})

t.tearDown(target.close.bind(target))

target.listen(0, (err) => {
t.error(err)

instance.register(From, {
base: `http://localhost:${target.address().port}/hello`,
undici: true
})

instance.listen(0, (err) => {
t.error(err)

get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
t.error(err)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.headers['x-my-header'], 'hello!')
t.equal(res.statusCode, 205)
t.equal(data.toString(), 'hello world')
})
})
})

0 comments on commit 7fb25fb

Please sign in to comment.