-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly support POSTs with streams (#20)
* add post test Signed-off-by: Matteo Collina <[email protected]> * Correctly support POSTs with streams Signed-off-by: Matteo Collina <[email protected]> --------- Signed-off-by: Matteo Collina <[email protected]>
- Loading branch information
Showing
5 changed files
with
238 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const { Readable } = require('node:stream') | ||
const { deepStrictEqual, strictEqual, rejects } = require('node:assert') | ||
const { join } = require('node:path') | ||
const { Worker } = require('node:worker_threads') | ||
const { createThreadInterceptor } = require('../') | ||
const { Agent, request } = require('undici') | ||
|
||
test('POST', async (t) => { | ||
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js')) | ||
t.after(() => worker.terminate()) | ||
|
||
const interceptor = createThreadInterceptor({ | ||
domain: '.local', | ||
}) | ||
interceptor.route('myserver', worker) | ||
|
||
const agent = new Agent().compose(interceptor) | ||
|
||
const { statusCode, body } = await request('http://myserver.local/echo-body', { | ||
dispatcher: agent, | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify({ hello: 'world' }), | ||
}) | ||
|
||
strictEqual(statusCode, 200) | ||
deepStrictEqual(await body.json(), { hello: 'world' }) | ||
}) | ||
|
||
test('POST with Stream', async (t) => { | ||
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js')) | ||
t.after(() => worker.terminate()) | ||
|
||
const interceptor = createThreadInterceptor({ | ||
domain: '.local', | ||
}) | ||
interceptor.route('myserver', worker) | ||
|
||
const agent = new Agent().compose(interceptor) | ||
|
||
const { statusCode, body } = await request('http://myserver.local/echo-body', { | ||
dispatcher: agent, | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: Readable.from(JSON.stringify({ hello: 'world' })), | ||
}) | ||
|
||
strictEqual(statusCode, 200) | ||
deepStrictEqual(await body.json(), { hello: 'world' }) | ||
}) | ||
|
||
test('POST with Stream that errors', async (t) => { | ||
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js')) | ||
t.after(() => worker.terminate()) | ||
|
||
const interceptor = createThreadInterceptor({ | ||
domain: '.local', | ||
}) | ||
interceptor.route('myserver', worker) | ||
|
||
const agent = new Agent().compose(interceptor) | ||
|
||
await rejects(request('http://myserver.local/echo-body', { | ||
dispatcher: agent, | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: new Readable({ | ||
read () { | ||
this.destroy(new Error('kaboom')) | ||
}, | ||
}), | ||
})) | ||
}) | ||
|
||
test('POST with buffer stream', async (t) => { | ||
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js')) | ||
t.after(() => worker.terminate()) | ||
|
||
const interceptor = createThreadInterceptor({ | ||
domain: '.local', | ||
}) | ||
interceptor.route('myserver', worker) | ||
|
||
const agent = new Agent().compose(interceptor) | ||
|
||
const { statusCode, body } = await request('http://myserver.local/echo-body', { | ||
dispatcher: agent, | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: Readable.from(Buffer.from(JSON.stringify({ hello: 'world' }))), | ||
}) | ||
|
||
strictEqual(statusCode, 200) | ||
deepStrictEqual(await body.json(), { hello: 'world' }) | ||
}) | ||
|
||
test('POST errors with streams of objects', async (t) => { | ||
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js')) | ||
t.after(() => worker.terminate()) | ||
|
||
const interceptor = createThreadInterceptor({ | ||
domain: '.local', | ||
}) | ||
interceptor.route('myserver', worker) | ||
|
||
const agent = new Agent().compose(interceptor) | ||
|
||
await rejects(request('http://myserver.local/echo-body', { | ||
dispatcher: agent, | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: Readable.from([{ hello: 'world' }]), | ||
})) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const { rejects } = require('node:assert') | ||
const { join } = require('node:path') | ||
const { Worker } = require('node:worker_threads') | ||
const { once } = require('node:events') | ||
const { createThreadInterceptor } = require('../') | ||
const { Agent, request } = require('undici') | ||
|
||
test('timeout', async (t) => { | ||
const empty = new Worker(join(__dirname, 'fixtures', 'empty.js')) | ||
|
||
const interceptor = createThreadInterceptor({ | ||
domain: '.local', | ||
timeout: 1000, | ||
}) | ||
interceptor.route('myserver', empty) | ||
|
||
const agent = new Agent().compose(interceptor) | ||
|
||
await rejects(request('http://myserver.local', { | ||
dispatcher: agent, | ||
}), new Error('Timeout while waiting from a response from myserver.local')) | ||
|
||
empty.postMessage('close') | ||
await once(empty, 'exit') | ||
}) | ||
|
||
test('timeout set to a boolean', async (t) => { | ||
const empty = new Worker(join(__dirname, 'fixtures', 'empty.js')) | ||
|
||
const interceptor = createThreadInterceptor({ | ||
domain: '.local', | ||
timeout: true, | ||
}) | ||
interceptor.route('myserver', empty) | ||
|
||
const agent = new Agent().compose(interceptor) | ||
|
||
await rejects(request('http://myserver.local', { | ||
dispatcher: agent, | ||
}), new Error('Timeout while waiting from a response from myserver.local')) | ||
|
||
empty.postMessage('close') | ||
await once(empty, 'exit') | ||
}) |