Skip to content
This repository was archived by the owner on Aug 10, 2023. It is now read-only.

Commit 671ce10

Browse files
committed
tests
1 parent 4062923 commit 671ce10

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

test/mocha.opts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require test/test_setup.js

test/test.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
describe('binary formatter', function () {
4+
describe('binary converter', function() {
5+
it('should convert strings to binary buffer', function () {
6+
expect({
7+
body: '1234',
8+
}).to.be.formattedOut(new Buffer([0x31, 0x32, 0x33, 0x34]));
9+
});
10+
11+
it('should keep the same if body is already buffer', function () {
12+
expect({
13+
body: new Buffer([0x31, 0x32, 0x33, 0x34]),
14+
}).to.be.formattedOut(new Buffer([0x31, 0x32, 0x33, 0x34]));
15+
});
16+
});
17+
18+
describe('Content-Length', function() {
19+
it('should set Content-Length to 4 if body is "1234"', function() {
20+
expect({
21+
body: '1234'
22+
}).to.have.contentLength(4);
23+
});
24+
});
25+
26+
describe('body', function() {
27+
describe('error', function() {
28+
it('should response code 500', function() {
29+
expect({
30+
body: new Error('error')
31+
}).to.has.responseCode(500);
32+
});
33+
34+
it('should response code 404', function() {
35+
var error = new Error('error');
36+
error.statusCode = 404;
37+
error.body = 'errorBody';
38+
expect({
39+
body: error
40+
}).to.has.responseCode(404);
41+
});
42+
});
43+
});
44+
});
45+

test/test_setup.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
global.chai = require('chai');
4+
global.expect = chai.expect;
5+
6+
var formatter = require('../.');
7+
8+
global.chai.use(function(_chai, utils) {
9+
function applyFormatter(assertionThis, responseMock) {
10+
var assertionObject = utils.flag(assertionThis, 'object');
11+
assertionObject.response = responseMock || {
12+
setHeader: function() {}
13+
};
14+
assertionObject.request = assertionObject.request || {query: {}};
15+
return formatter(assertionObject.request,
16+
assertionObject.response,
17+
assertionObject.body);
18+
}
19+
20+
utils.addMethod(chai.Assertion.prototype, 'formattedOut',
21+
function (output) {
22+
new chai.Assertion(applyFormatter(this)).to.be.eql(output);
23+
});
24+
25+
utils.addMethod(chai.Assertion.prototype, 'contentLength',
26+
function (expectedLength) {
27+
var contentLength;
28+
applyFormatter(this, {
29+
setHeader: function(name, value) {
30+
contentLength = value;
31+
}
32+
});
33+
new chai.Assertion(contentLength).to.be.eql(expectedLength);
34+
});
35+
36+
utils.addMethod(chai.Assertion.prototype, 'responseCode',
37+
function (expectedCode) {
38+
var response = {
39+
statusCode: 200,
40+
setHeader: function() {}
41+
};
42+
applyFormatter(this, response);
43+
new chai.Assertion(response.statusCode).to.be.eql(expectedCode);
44+
});
45+
});
46+

0 commit comments

Comments
 (0)