Skip to content

Commit

Permalink
Merge pull request #480 from Zhubaoheng/develop_max_2024_dec_2
Browse files Browse the repository at this point in the history
feat: Add test for binary data stream.
  • Loading branch information
mike442144 authored Dec 17, 2024
2 parents c9210c9 + 13b6a6e commit af4b14c
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/binaryDataStream.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import test from 'ava';
import Crawler from '../dist/index.js';
import nock from 'nock';
import { testCb } from "./lib/avaTestCb.js";


const binaryData = Buffer.from('Hello, World!', 'utf-8');

test.beforeEach(t => {
nock('http://example.com')
.get('/binary-data')
.reply(200, binaryData, {
'Content-Type': 'application/octet-stream',
});

t.context.crawler = new Crawler({
encoding: null,
callback: (err, res, done) => {
if (err) {
console.error(err.stack);
return done(err);
}

const buffers = [];
res.body.on('data', chunk => buffers.push(chunk));
res.body.on('end', () => {
const result = Buffer.concat(buffers);
t.is(result.toString(), 'Hello, World!', 'The binary stream should match the expected content');
done();
});
},
});
});


testCb(test, 'should correctly handle and process a binary data stream', async t => {
t.context.crawler.send({
url: 'http://example.com/binary-data',
callback: (error, res) => {
t.is(error, null);
t.is(res.statusCode, 200);
t.end();
},
});
});

0 comments on commit af4b14c

Please sign in to comment.