Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Sep 26, 2019
1 parent 2a5d262 commit 6c790d4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
10 changes: 5 additions & 5 deletions source/agent.js
Expand Up @@ -107,7 +107,7 @@ class Agent extends EventEmitter {
this.tlsSessionCache = new QuickLRU({maxSize: maxCachedTlsSessions});
}

normalizeAuthority(authority, servername) {
static normalizeAuthority(authority, servername) {
if (typeof authority === 'string') {
authority = new URL(authority);
}
Expand All @@ -122,12 +122,12 @@ class Agent extends EventEmitter {
return `https://${host}:${port}`;
}

normalizeOptions(options) {
static normalizeOptions(options) {
let normalized = '';

if (options) {
for (const key of nameKeys) {
if (Reflect.has(options, key)) {
if (options[key]) {
normalized += `:${options[key]}`;
}
}
Expand Down Expand Up @@ -163,8 +163,8 @@ class Agent extends EventEmitter {

const listeners = Array.isArray(callback) ? callback : [detached];

const normalizedOptions = this.normalizeOptions(options);
const normalizedAuthority = this.normalizeAuthority(authority, options && options.servername);
const normalizedOptions = Agent.normalizeOptions(options);
const normalizedAuthority = Agent.normalizeAuthority(authority, options && options.servername);

if (Reflect.has(this.freeSessions, normalizedOptions)) {
const freeSessions = getSessions(this.freeSessions, normalizedOptions, normalizedAuthority);
Expand Down
6 changes: 5 additions & 1 deletion source/client-request.js
Expand Up @@ -100,7 +100,11 @@ class ClientRequest extends Writable {
options.path = options.socketPath;

this[kOptions] = options;
this[kAuthority] = options.authority || options; // TODO: can we totally get rid of parsing host, port etc. on our own?
this[kAuthority] = Agent.normalizeAuthority(options, options.servername);

if (!Reflect.has(this[kHeaders], ':authority')) {
this[kHeaders][':authority'] = this[kAuthority].slice(8);
}

if (this.agent && options.preconnect !== false) {
this.agent.getSession(this[kAuthority], options).catch(() => {});
Expand Down
8 changes: 4 additions & 4 deletions test/agent.js
Expand Up @@ -478,15 +478,15 @@ if (isCompatible) {
await agent.getSession(server.url);
await setImmediateAsync();

t.true(is.buffer(agent.tlsSessionCache.get(`${agent.normalizeAuthority(server.url)}:`).session));
t.true(is.buffer(agent.tlsSessionCache.get(`${Agent.normalizeAuthority(server.url)}:`).session));
});

test('reuses a TLS session', wrapper, async (t, server) => {
const agent = new Agent();
const session = await agent.getSession(server.url);
await setImmediateAsync();

const tlsSession = agent.tlsSessionCache.get(`${agent.normalizeAuthority(server.url)}:`).session;
const tlsSession = agent.tlsSessionCache.get(`${Agent.normalizeAuthority(server.url)}:`).session;

session.close();
await pEvent(session, 'close');
Expand All @@ -503,12 +503,12 @@ if (isCompatible) {
const session = await agent.getSession(server.url);
await setImmediateAsync();

t.true(is.buffer(agent.tlsSessionCache.get(`${agent.normalizeAuthority(server.url)}:`).session));
t.true(is.buffer(agent.tlsSessionCache.get(`${Agent.normalizeAuthority(server.url)}:`).session));

session.destroy(new Error('Ouch.'));
await pEvent(session, 'close', {rejectionEvents: []});

t.true(is.undefined(agent.tlsSessionCache.get(`${agent.normalizeAuthority(server.url)}:`)));
t.true(is.undefined(agent.tlsSessionCache.get(`${Agent.normalizeAuthority(server.url)}:`)));
});
}

Expand Down
35 changes: 21 additions & 14 deletions test/request.js
Expand Up @@ -150,20 +150,6 @@ if (isCompatible) {
t.true((/self signed certificate/).test(error.message) || error.message === 'unable to verify the first certificate');
});

test('`authority` option', async t => {
const localServer = await createServer();
await localServer.listen();

const request = makeRequest({...localServer.options, authority: localServer.options});
request.end();

const response = await pEvent(request, 'response');
const data = JSON.parse(await getStream(response));
await localServer.close();

t.is(data.headers[':authority'], `${localServer.options.hostname}:${localServer.options.port}`);
});

test('`tlsSession` option', wrapper, async (t, server) => {
const request = makeRequest(server.url, {tlsSession: 'not a buffer', agent: false});
request.end();
Expand Down Expand Up @@ -574,6 +560,27 @@ if (isCompatible) {
t.true(called);
});

test('sets proper `:authority` header', wrapper, async (t, server) => {
server.on('session', session => {
session.origin('https://example.com');
});

server.get('/', (request, response) => {
response.end(request.headers[':authority']);
});

const agent = new Agent();
const session = await agent.getSession(server.url);
await new Promise(resolve => setImmediate(resolve));
console.log(session.originSet);

const request = makeRequest('https://example.com', {agent}).end();
const response = await pEvent(request, 'response');
const body = await getStream(response);

t.is(body, 'example.com');
});

if (process.platform !== 'win32') {
const socketPath = tempy.file({extension: 'socket'});

Expand Down

0 comments on commit 6c790d4

Please sign in to comment.