-
-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add binary payload example (#2016)
idea taken from #2004
- Loading branch information
Showing
1 changed file
with
32 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,32 @@ | ||
const http = require('http') | ||
const fs = require('fs') | ||
|
||
const nock = require('../') | ||
|
||
const readFile = function () { | ||
return fs.readFileSync(`${__dirname}/../tests/assets/reply_file_2.txt.gz`) | ||
} | ||
|
||
nock('http://binary.com').get('/binary').reply(200, readFile(), { | ||
'content-type': 'application/octet-stream', | ||
'content-length': readFile().length, | ||
'content-disposition': 'attachment; filename=reply_file_2.tar.gz', | ||
}) | ||
|
||
http.get('http://binary.com/binary', function (res) { | ||
const data = [] | ||
res.on('data', function (chunk) { | ||
// try this | ||
// chunk[0] = 1; | ||
// chunk[1] = 1; | ||
data.push(chunk) | ||
}) | ||
res.on('end', function () { | ||
const buffer = Buffer.concat(data) | ||
console.log( | ||
Buffer.compare(readFile(), buffer) === 0 | ||
? 'Received the file.' | ||
: 'Received something else.' | ||
) | ||
}) | ||
}) |