mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
* fix: only override hostname if none was provided Copy the behaviour or Wreck.request from 7.4. This is a regression for setting the Host header value. * Refactor variable name * [skip ci] Fix comment
This commit is contained in:
parent
617c874e2a
commit
fda06ee06a
2 changed files with 64 additions and 8 deletions
|
@ -30,11 +30,6 @@ describe(`Console's send request`, () => {
|
|||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
stub = sandbox.stub(http, 'request').callsFake(() => {
|
||||
fakeRequest = {
|
||||
abort: sinon.stub(),
|
||||
on() {},
|
||||
once() {},
|
||||
} as any;
|
||||
return fakeRequest;
|
||||
});
|
||||
});
|
||||
|
@ -45,6 +40,11 @@ describe(`Console's send request`, () => {
|
|||
});
|
||||
|
||||
it('correctly implements timeout and abort mechanism', async () => {
|
||||
fakeRequest = {
|
||||
abort: sinon.stub(),
|
||||
on() {},
|
||||
once() {},
|
||||
} as any;
|
||||
try {
|
||||
await proxyRequest({
|
||||
agent: null as any,
|
||||
|
@ -60,4 +60,55 @@ describe(`Console's send request`, () => {
|
|||
expect((fakeRequest.abort as sinon.SinonStub).calledOnce).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('correctly sets the "host" header entry', async () => {
|
||||
fakeRequest = {
|
||||
abort: sinon.stub(),
|
||||
on() {},
|
||||
once(event: string, fn: any) {
|
||||
if (event === 'response') {
|
||||
return fn('done');
|
||||
}
|
||||
},
|
||||
} as any;
|
||||
|
||||
// Don't set a host header this time
|
||||
const result1 = await proxyRequest({
|
||||
agent: null as any,
|
||||
headers: {},
|
||||
method: 'get',
|
||||
payload: null as any,
|
||||
timeout: 30000,
|
||||
uri: new URL('http://noone.nowhere.none'),
|
||||
});
|
||||
|
||||
expect(result1).toEqual('done');
|
||||
|
||||
const [httpRequestOptions1] = stub.firstCall.args;
|
||||
|
||||
expect((httpRequestOptions1 as any).headers).toEqual({
|
||||
'content-type': 'application/json',
|
||||
host: 'noone.nowhere.none', // Defaults to the provided host name
|
||||
'transfer-encoding': 'chunked',
|
||||
});
|
||||
|
||||
// Set a host header
|
||||
const result2 = await proxyRequest({
|
||||
agent: null as any,
|
||||
headers: { Host: 'myhost' },
|
||||
method: 'get',
|
||||
payload: null as any,
|
||||
timeout: 30000,
|
||||
uri: new URL('http://noone.nowhere.none'),
|
||||
});
|
||||
|
||||
expect(result2).toEqual('done');
|
||||
|
||||
const [httpRequestOptions2] = stub.secondCall.args;
|
||||
expect((httpRequestOptions2 as any).headers).toEqual({
|
||||
'content-type': 'application/json',
|
||||
Host: 'myhost', // Uses provided host name
|
||||
'transfer-encoding': 'chunked',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -57,19 +57,24 @@ export const proxyRequest = ({
|
|||
reject = rej;
|
||||
});
|
||||
|
||||
const finalUserHeaders = { ...headers };
|
||||
const hasHostHeader = Object.keys(finalUserHeaders).some(key => key.toLowerCase() === 'host');
|
||||
if (!hasHostHeader) {
|
||||
finalUserHeaders.host = hostname;
|
||||
}
|
||||
|
||||
const req = client.request({
|
||||
method: method.toUpperCase(),
|
||||
// We support overriding this on a per request basis to support legacy proxy config. See ./proxy_config.
|
||||
rejectUnauthorized: typeof rejectUnauthorized === 'boolean' ? rejectUnauthorized : undefined,
|
||||
host: hostname,
|
||||
port: port === '' ? undefined : Number(port),
|
||||
port: port === '' ? undefined : parseInt(port, 10),
|
||||
protocol,
|
||||
path: `${pathname}${search || ''}`,
|
||||
headers: {
|
||||
...headers,
|
||||
...finalUserHeaders,
|
||||
'content-type': 'application/json',
|
||||
'transfer-encoding': 'chunked',
|
||||
host: hostname,
|
||||
},
|
||||
agent,
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue