Skip to content

Commit

Permalink
test: improve test cases for send stream
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Feb 22, 2024
1 parent 2f64ff7 commit 730bc71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions test/fixtures/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!
23 changes: 22 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 730bc71

Please sign in to comment.