Skip to content

Commit

Permalink
feat: encode an expected buffer the same way when matching as the client
Browse files Browse the repository at this point in the history
  • Loading branch information
bbridges authored and gr2m committed Aug 3, 2018
1 parent 907be86 commit c584f7a
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/match_body.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var deepEqual = require('deep-equal');
var qs = require('qs');
var _ = require('lodash')
var common = require('./common');

module.exports =
function matchBody(spec, body) {
Expand All @@ -15,6 +16,14 @@ function matchBody(spec, body) {
body = body.toString();
}

if (Buffer.isBuffer(spec)) {
if (common.isBinaryBuffer(spec)) {
spec = spec.toString('hex');
} else {
spec = spec.toString('utf8');
}
}

var contentType = (
options.headers &&
(options.headers['Content-Type'] || options.headers['content-type']) ||
Expand Down
106 changes: 106 additions & 0 deletions tests/test_body_match.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,109 @@ test('urlencoded form posts are matched with regexp', function(t) {
t.end();
});
});

test('match utf-8 buffer body with utf-8 buffer', function(t) {

nock('http://encodingsareus.com')
.post('/', Buffer.from('hello'))
.reply(200);

mikealRequest({
url: 'http://encodingsareus.com/',
method: 'post',
encoding: null,
body: Buffer.from('hello')
}, function(err, res) {
if (err) throw err;
assert.equal(res.statusCode, 200);
t.end();
});
});

test('doesn\'t match utf-8 buffer body with mismatching utf-8 buffer', function(t) {

nock('http://encodingsareus.com')
.post('/', Buffer.from('goodbye'))
.reply(200);

mikealRequest({
url: 'http://encodingsareus.com/',
method: 'post',
encoding: null,
body: Buffer.from('hello')
}, function(err) {
assert.ok(err);
t.end();
});
});

test('match binary buffer body with binary buffer', function(t) {

nock('http://encodingsareus.com')
.post('/', Buffer.from([0xff, 0xff, 0xff]))
.reply(200);

mikealRequest({
url: 'http://encodingsareus.com/',
method: 'post',
encoding: null,
body: Buffer.from([0xff, 0xff, 0xff])
}, function(err, res) {
if (err) throw err;
assert.equal(res.statusCode, 200);
t.end();
});
});

test('doesn\'t match binary buffer body with mismatching binary buffer', function(t) {

nock('http://encodingsareus.com')
.post('/', Buffer.from([0xff, 0xff, 0xfa]))
.reply(200);

mikealRequest({
url: 'http://encodingsareus.com/',
method: 'post',
encoding: null,
body: Buffer.from([0xff, 0xff, 0xff])
}, function(err) {
assert.ok(err);
t.end();
});
});

// for the next two tests, keeping the same urls causes them to interfere with another.

test('doesn\'t match binary buffer body with mismatching utf-8 buffer', function(t) {

nock('http://encodingsareus-1.com')
.post('/', Buffer.from([0xff, 0xff, 0xff]))
.reply(200);

mikealRequest({
url: 'http://encodingsareus-1.com/',
method: 'post',
encoding: null,
body: Buffer.from('hello')
}, function(err) {
assert.ok(err);
t.end();
});
});

test('doesn\'t match utf-8 buffer body with mismatching binary buffer', function(t) {

nock('http://encodingsareus-2.com')
.post('/', Buffer.from('hello'))
.reply(200);

mikealRequest({
url: 'http://encodingsareus-2.com/',
method: 'post',
encoding: null,
body: Buffer.from([0xff, 0xff, 0xff])
}, function(err) {
assert.ok(err);
t.end();
});
});

0 comments on commit c584f7a

Please sign in to comment.