|
| 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