diff --git a/test/fixtures/test.txt b/test/fixtures/test.txt new file mode 100644 index 0000000..6769dd6 --- /dev/null +++ b/test/fixtures/test.txt @@ -0,0 +1 @@ +Hello world! \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 2788fe9..ee33bce 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -2,6 +2,8 @@ const http = require('http'); const zlib = require('zlib'); +const fs = require('fs'); +const path = require('path'); const assert = require('assert'); const socks = require('socksv5'); @@ -23,6 +25,15 @@ const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello world!'); }, 200); + } else if (req.url === '/stream') { + res.writeHead(200); + const buffers = []; + req.on('data', (chunk) => { + buffers.push(chunk); + }); + req.on('end', () => { + res.end(Buffer.concat(buffers).toString()); + }); } else if (req.url === '/compression') { res.writeHead(200, { 'content-encoding': 'gzip' @@ -52,7 +63,7 @@ const server = http.createServer((req, res) => { res.end(); return; } - res.write(`data: ${JSON.stringify({count: count})}\nevent: flow\nid: sse-test\nretry: 3\n\n`); + res.write(`data: ${JSON.stringify({count: count})}\nevent: flow\nid: sse-test\nretry: 3\n:heartbeat\n\n`); count++; }, 100); } else if (req.url === '/sse_with_no_spaces') { @@ -145,6 +156,16 @@ describe('httpx', () => { assert.deepStrictEqual(result, Buffer.from('Hello world!')); }); + it('should ok with stream', async function () { + var res = await make(server)('/stream', { + method: 'POST', + data: fs.createReadStream(path.join(__dirname, './fixtures/test.txt')) + }); + assert.strictEqual(res.statusCode, 200); + var result = await httpx.read(res); + assert.deepStrictEqual(result, Buffer.from('Hello world!')); + }); + it('compression should ok', async function () { var res = await make(server)('/compression'); assert.strictEqual(res.statusCode, 200);