Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions lib/utils/http_request_handler/request_handler.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { describe, beforeEach, afterEach, beforeAll, afterAll, it, vi, expect } from 'vitest';

import http from 'http';
import nock from 'nock';
import zlib from 'zlib';
import { NodeRequestHandler } from './request_handler.node';
Expand Down Expand Up @@ -202,6 +203,79 @@ describe('NodeRequestHandler', () => {
scope.done();
});

describe('multi-byte unicode data', () => {
let server: http.Server;
let port: number;
let receivedBody: string;

beforeAll(async () => {
nock.enableNetConnect('localhost');
server = http.createServer((req, res) => {
const chunks: Buffer[] = [];
req.on('data', (chunk: Buffer) => chunks.push(chunk));
req.on('end', () => {
receivedBody = Buffer.concat(chunks).toString('utf8');
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ ok: true }));
});
});

await new Promise<void>((resolve) => {
server.listen(0, () => {
port = (server.address() as { port: number }).port;
resolve();
});
});
});

afterAll(async () => {
await new Promise<void>((resolve) => server.close(() => resolve()));
nock.disableNetConnect();
});

it('should correctly transmit ASCII data', async () => {
const data = '{"key":"value"}';

const { responsePromise } = nodeRequestHandler.makeRequest(
`http://localhost:${port}/test`,
{ 'content-type': 'application/json' },
'POST',
data,
);
await responsePromise;

expect(receivedBody).toBe(data);
});

it('should correctly transmit emoji data', async () => {
const data = JSON.stringify({ message: '🚀 launch' });

const { responsePromise } = nodeRequestHandler.makeRequest(
`http://localhost:${port}/test`,
{ 'content-type': 'application/json' },
'POST',
data,
);
await responsePromise;

expect(receivedBody).toBe(data);
});

it('should correctly transmit CJK character data', async () => {
const data = JSON.stringify({ greeting: '你好世界' });

const { responsePromise } = nodeRequestHandler.makeRequest(
`http://localhost:${port}/test`,
{ 'content-type': 'application/json' },
'POST',
data,
);
await responsePromise;

expect(receivedBody).toBe(data);
});
});

describe('timeout', () => {
beforeEach(() => {
vi.useFakeTimers();
Expand Down
7 changes: 2 additions & 5 deletions lib/utils/http_request_handler/request_handler.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,13 @@ export class NodeRequestHandler implements RequestHandler {
headers: {
...headers,
'accept-encoding': 'gzip,deflate',
'content-length': String(data?.length || 0)
'content-length': String(data ? Buffer.byteLength(data) : 0),
},
timeout: this.timeout,
});
const abortableRequest = this.getAbortableRequestFromRequest(request);

if (data) {
request.write(data);
}
request.end();
request.end(data);

return abortableRequest;
}
Expand Down
Loading