Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dns interceptor with Pool #3957

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,24 @@ class Client extends DispatcherBase {
this[kResume]()
})
}

request (opts, callback) {
const options = { ...opts }
if (options.origin) {
try {
const origin = util.parseOrigin(opts.origin).origin
if (origin !== this[kUrl].origin) {
throw new InvalidArgumentError('origin must equal constructor origin')
}
} catch (err) {
return typeof callback === 'function'
? queueMicrotask(() => callback(err))
: Promise.reject(err)
}
}
options.origin = this[kUrl].origin
return super.request(options, callback)
}
}

function onError (client, err) {
Expand Down
18 changes: 18 additions & 0 deletions lib/dispatcher/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ class Pool extends PoolBase {
return dispatcher
}
}

request (opts, callback) {
const options = { ...opts }
if (options.origin) {
try {
const origin = util.parseOrigin(opts.origin).origin
if (origin !== this[kUrl].origin) {
throw new InvalidArgumentError('origin must equal constructor origin')
}
} catch (err) {
return typeof callback === 'function'
? queueMicrotask(() => callback(err))
: Promise.reject(err)
}
}
options.origin = this[kUrl].origin
return super.request(options, callback)
}
}

module.exports = Pool
55 changes: 55 additions & 0 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,61 @@ describe('headers', () => {
})
})

describe('origin', () => {
let serverAddress
const server = createServer((req, res) => {
res.end('hello')
})

before(async () => {
server.listen(0)
await EE.once(server, 'listening')
serverAddress = `localhost:${server.address().port}`
})

after(() => server.close())

test('same origin in request options', async (t) => {
t = tspl(t, { plan: 1 })

const client = new Client(`http://${serverAddress}`)
after(() => client.destroy())

const response = await client.request({
origin: `http://${serverAddress}`,
path: '/',
method: 'GET'
})
t.strictEqual(await response.body.text(), 'hello')
})

test('invalid origin in request options', async (t) => {
t = tspl(t, { plan: 1 })

const client = new Client(`http://${serverAddress}`)
after(() => client.destroy())

t.rejects(client.request({
origin: 'localhost',
path: '/',
method: 'GET'
}), new TypeError('Invalid URL'))
})

test('different origin in request options', async (t) => {
t = tspl(t, { plan: 1 })

const client = new Client(`http://${serverAddress}`)
after(() => client.destroy())

t.rejects(client.request({
origin: 'http://localhost',
path: '/',
method: 'GET'
}), new InvalidArgumentError('origin must equal constructor origin'))
})
})

test('request long multibyte text', async (t) => {
t = tspl(t, { plan: 1 })

Expand Down
28 changes: 0 additions & 28 deletions test/interceptors/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const request = {
origin: 'localhost',
method: 'GET',
path: '/'
}
Expand Down Expand Up @@ -79,7 +78,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const requestA = {
origin: 'localhost',
method: 'GET',
path: '/',
headers: {
Expand All @@ -91,7 +89,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const requestB = {
origin: 'localhost',
method: 'GET',
path: '/',
headers: {
Expand Down Expand Up @@ -180,7 +177,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const request = {
origin: 'localhost',
method: 'GET',
path: '/'
}
Expand Down Expand Up @@ -265,7 +261,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const request = {
origin: 'localhost',
method: 'GET',
path: '/'
}
Expand Down Expand Up @@ -358,7 +353,6 @@ describe('Cache Interceptor', () => {
strictEqual(revalidationRequests, 0)

const request = {
origin: 'localhost',
path: '/',
method: 'GET',
headers: {
Expand Down Expand Up @@ -416,7 +410,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const request = {
origin: 'localhost',
method: 'GET',
path: '/'
}
Expand Down Expand Up @@ -463,7 +456,6 @@ describe('Cache Interceptor', () => {

for (const method of ['POST', 'PUT', 'PATCH', 'DELETE']) {
await client.request({
origin: 'localhost',
method,
path: '/'
})
Expand Down Expand Up @@ -511,7 +503,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const request = {
origin: 'localhost',
method: 'GET',
path: '/'
}
Expand Down Expand Up @@ -557,7 +548,6 @@ describe('Cache Interceptor', () => {
// Should hit the origin
{
const res = await client.request({
origin: 'localhost',
path: '/',
method: 'GET'
})
Expand All @@ -568,7 +558,6 @@ describe('Cache Interceptor', () => {
// Should hit the cache
{
const res = await client.request({
origin: 'localhost',
path: '/',
method: 'GET'
})
Expand Down Expand Up @@ -614,7 +603,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const request = {
origin: 'localhost',
method: 'GET',
path: '/'
}
Expand Down Expand Up @@ -689,7 +677,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const request = {
origin: 'localhost',
method: 'GET',
path: '/'
}
Expand Down Expand Up @@ -758,7 +745,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const request = {
origin: 'localhost',
method: 'GET',
path: '/'
}
Expand Down Expand Up @@ -820,7 +806,6 @@ describe('Cache Interceptor', () => {
* @type {import('../../types/dispatcher').default.RequestOptions}
*/
const request = {
origin: 'localhost',
method: 'GET',
path: '/'
}
Expand Down Expand Up @@ -881,7 +866,6 @@ describe('Cache Interceptor', () => {

// Send initial request. This should reach the origin
await client.request({
origin: 'localhost',
method: 'GET',
path: '/',
headers: {
Expand All @@ -893,7 +877,6 @@ describe('Cache Interceptor', () => {

// Send second request, a validation request should be sent
await client.request({
origin: 'localhost',
method: 'GET',
path: '/',
headers: {
Expand All @@ -905,7 +888,6 @@ describe('Cache Interceptor', () => {

// Send third request w/o no-cache, this should be handled by the cache
await client.request({
origin: 'localhost',
method: 'GET',
path: '/'
})
Expand Down Expand Up @@ -935,7 +917,6 @@ describe('Cache Interceptor', () => {
await once(server, 'listening')

await client.request({
origin: 'localhost',
method: 'GET',
path: '/',
headers: {
Expand Down Expand Up @@ -964,15 +945,13 @@ describe('Cache Interceptor', () => {

// Send initial request. This should reach the origin
await client.request({
origin: 'localhost',
method: 'GET',
path: '/'
})
equal(requestsToOrigin, 1)

// Send second request, this shouldn't reach the origin
await client.request({
origin: 'localhost',
method: 'GET',
path: '/',
headers: {
Expand All @@ -984,7 +963,6 @@ describe('Cache Interceptor', () => {
// Send third request to an uncached resource, this should return a 504
{
const res = await client.request({
origin: 'localhost',
method: 'GET',
path: '/bla',
headers: {
Expand All @@ -997,7 +975,6 @@ describe('Cache Interceptor', () => {
// Send fourth request to an uncached resource w/ a , this should return a 504
{
const res = await client.request({
origin: 'localhost',
method: 'GET',
path: '/asd123',
headers: {
Expand Down Expand Up @@ -1044,7 +1021,6 @@ describe('Cache Interceptor', () => {
// Send first request. This will hit the origin and succeed
{
const response = await client.request({
origin: 'localhost',
path: '/',
method: 'GET'
})
Expand All @@ -1057,7 +1033,6 @@ describe('Cache Interceptor', () => {
// cache and succeed
{
const response = await client.request({
origin: 'localhost',
path: '/',
method: 'GET'
})
Expand All @@ -1072,7 +1047,6 @@ describe('Cache Interceptor', () => {
// fail but the response should still be served from cache.
{
const response = await client.request({
origin: 'localhost',
path: '/',
method: 'GET',
headers: {
Expand All @@ -1088,7 +1062,6 @@ describe('Cache Interceptor', () => {
// should get the error here.
{
const response = await client.request({
origin: 'localhost',
path: '/',
method: 'GET'
})
Expand All @@ -1102,7 +1075,6 @@ describe('Cache Interceptor', () => {
// should see the error.
{
const response = await client.request({
origin: 'localhost',
path: '/',
method: 'GET',
headers: {
Expand Down
Loading
Loading