-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #480 from Zhubaoheng/develop_max_2024_dec_2
feat: Add test for binary data stream.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 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
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(); | ||
}, | ||
}); | ||
}); |