| 1 | 'use strict' |
| 2 | |
| 3 | const { createServer } = require('node:http') |
| 4 | const { Agent, EventSource } = require('../..') |
| 5 | const { test } = require('node:test') |
| 6 | |
| 7 | test('EventSource allows setting custom dispatcher.', (t, done) => { |
| 8 | t.plan(1) |
| 9 | |
| 10 | const server = createServer({ joinDuplicateHeaders: true }, async (req, res) => { |
| 11 | res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) |
| 12 | t.assert.deepStrictEqual(req.headers['x-customer-header'], 'hello world') |
| 13 | |
| 14 | res.end() |
| 15 | done() |
| 16 | }) |
| 17 | |
| 18 | t.after(() => { |
| 19 | server.close() |
| 20 | }) |
| 21 | |
| 22 | server.listen(0, () => { |
| 23 | class CustomHeaderAgent extends Agent { |
| 24 | dispatch (opts) { |
| 25 | opts.headers['x-customer-header'] = 'hello world' |
| 26 | return super.dispatch(...arguments) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | const eventSourceInstance = new EventSource(`http://localhost:${server.address().port}`, { |
| 31 | dispatcher: new CustomHeaderAgent() |
| 32 | }) |
| 33 | t.after(() => { |
| 34 | eventSourceInstance.close() |
| 35 | }) |
| 36 | }) |
| 37 | }) |
| 38 | |
| 39 | test('EventSource allows setting custom dispatcher in EventSourceDict.', (t, done) => { |
| 40 | t.plan(1) |
| 41 | |
| 42 | const server = createServer({ joinDuplicateHeaders: true }, async (req, res) => { |
| 43 | res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) |
| 44 | t.assert.deepStrictEqual(req.headers['x-customer-header'], 'hello world') |
| 45 | |
| 46 | res.end() |
| 47 | |
| 48 | done() |
| 49 | }) |
| 50 | |
| 51 | t.after(() => { |
| 52 | server.close() |
| 53 | }) |
| 54 | |
| 55 | server.listen(0, () => { |
| 56 | class CustomHeaderAgent extends Agent { |
| 57 | dispatch (opts) { |
| 58 | opts.headers['x-customer-header'] = 'hello world' |
| 59 | return super.dispatch(...arguments) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | const eventSourceInstance = new EventSource(`http://localhost:${server.address().port}`, { |
| 64 | node: { |
| 65 | dispatcher: new CustomHeaderAgent() |
| 66 | } |
| 67 | }) |
| 68 | t.after(() => { |
| 69 | eventSourceInstance.close() |
| 70 | }) |
| 71 | }) |
| 72 | }) |