Skip to content

Commit 39869b2

Browse files
tastypacketsEzekiel Keator
and
Ezekiel Keator
authored
feature: support pre-shared sessions (#3325)
Co-authored-by: Ezekiel Keator <[email protected]>
1 parent 7f54a24 commit 39869b2

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

lib/core/connect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ if (global.FinalizationRegistry && !(process.env.NODE_V8_COVERAGE || process.env
7373
}
7474
}
7575

76-
function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, ...opts }) {
76+
function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, session: customSession, ...opts }) {
7777
if (maxCachedSessions != null && (!Number.isInteger(maxCachedSessions) || maxCachedSessions < 0)) {
7878
throw new InvalidArgumentError('maxCachedSessions must be a positive integer or zero')
7979
}
@@ -91,7 +91,7 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, ...o
9191
servername = servername || options.servername || util.getServerName(host) || null
9292

9393
const sessionKey = servername || hostname
94-
const session = sessionCache.get(sessionKey) || null
94+
const session = customSession || sessionCache.get(sessionKey) || null
9595

9696
assert(sessionKey)
9797

test/connect-pre-shared-session.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict'
2+
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after, mock } = require('node:test')
5+
const { Client } = require('..')
6+
const { createServer } = require('node:https')
7+
const pem = require('https-pem')
8+
const tls = require('node:tls')
9+
10+
test('custom session passed to client will be used in tls connect call', async (t) => {
11+
t = tspl(t, { plan: 4 })
12+
13+
const mockConnect = mock.method(tls, 'connect')
14+
15+
const server = createServer(pem, (req, res) => {
16+
t.strictEqual('/', req.url)
17+
t.strictEqual('GET', req.method)
18+
res.setHeader('content-type', 'text/plain')
19+
res.end('hello')
20+
})
21+
after(() => server.close())
22+
23+
server.listen(0, async () => {
24+
const session = Buffer.from('test-session')
25+
26+
const client = new Client(`https://localhost:${server.address().port}`, {
27+
connect: {
28+
rejectUnauthorized: false,
29+
session
30+
}
31+
})
32+
after(() => client.close())
33+
34+
const { statusCode, headers, body } = await client.request({
35+
path: '/',
36+
method: 'GET'
37+
})
38+
39+
t.strictEqual(statusCode, 200)
40+
t.strictEqual(headers['content-type'], 'text/plain')
41+
42+
const responseText = await body.text()
43+
t.strictEqual('hello', responseText)
44+
45+
const connectSession = mockConnect.mock.calls[0].arguments[0].session
46+
t.strictEqual(connectSession, session)
47+
})
48+
49+
await t.completed
50+
})

0 commit comments

Comments
 (0)