Skip to content

Commit

Permalink
examples: add binary payload example (#2016)
Browse files Browse the repository at this point in the history
idea taken from #2004
  • Loading branch information
mguentner committed Aug 9, 2020
1 parent 3f97d2d commit cee17f0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/binary-reply.js
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.'
)
})
})

0 comments on commit cee17f0

Please sign in to comment.