should-http
Advanced tools
Comparing version 0.0.4 to 0.1.0
@@ -0,6 +1,13 @@ | ||
0.1.0 / 2017-02-06 | ||
================== | ||
* `.xml()` assertion to test res Content-Type to be application/xml | ||
* `.contentType(type, charset)` now used for every content type assertions | ||
* formatter for http.IncomingMessage | ||
0.0.3 / 2015-05-21 | ||
================== | ||
* .status will show only body and statusCode | ||
* `.status()` will show only body and statusCode | ||
0.0.2 / 2014-05-29 | ||
@@ -14,2 +21,2 @@ ================== | ||
* Move this thing to separate repo | ||
* Move this thing to separate repo |
93
http.js
@@ -1,10 +0,41 @@ | ||
/*! | ||
* Should | ||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca> | ||
/* | ||
* should-http | ||
* Copyright(c) 2010-2013 TJ Holowaychuk <tj@vision-media.ca> | ||
* Copyright(c) 2013-2016 Denis Bardadym <bardadymchik@gmail.com> | ||
* MIT Licensed | ||
*/ | ||
var contentType = require('content-type'); | ||
var http = require('http'); | ||
module.exports = function(should, Assertion) { | ||
var i = should.format; | ||
var t = should.modules.type; | ||
var format = should.modules.format; | ||
var NODE_HTTP_INCOMMING_MESSAGE = new t.Type(t.OBJECT, 'node-http-incomming-message'); | ||
t.checker.addBeforeFirstMatch({}, function(obj) { | ||
if (obj instanceof http.IncomingMessage) { | ||
return NODE_HTTP_INCOMMING_MESSAGE; | ||
} | ||
}); | ||
var FIELDS = { | ||
headers: true, | ||
httpVersion: true, | ||
method: true, | ||
statusCode: true, | ||
url: true, | ||
body: true | ||
}; | ||
format.Formatter.addType(NODE_HTTP_INCOMMING_MESSAGE, function(value) { | ||
return format.formatPlainObject.call(this, value, { | ||
filterKey: function(key) { | ||
return key in FIELDS; | ||
} | ||
}); | ||
}); | ||
/** | ||
@@ -24,8 +55,11 @@ * Asserts given object has property headers which contain `field` and optional `val`. Will work well with node Request/Response etc. | ||
Assertion.add('header', function(field, val) { | ||
this.have.property('headers'); | ||
this.params = { obj: '{ ..., headers: ' + i(this.obj) + ', ... }', operator: 'to have header ' + i(field) + (val !== undefined ? (':' + i(val)) : '') }; | ||
if (val !== undefined) { | ||
this.have.property(field.toLowerCase(), val); | ||
var obj = this.obj; | ||
var assert = should(obj).have.property('headers'); | ||
this.params = { operator: 'to have header ' + i(field) + (val !== undefined ? (':' + i(val)) : '') }; | ||
if (val != null) { | ||
assert.have.property(field.toLowerCase(), val); | ||
} else { | ||
this.have.property(field.toLowerCase()); | ||
assert.have.property(field.toLowerCase()); | ||
} | ||
@@ -48,5 +82,4 @@ }); | ||
var obj = this.obj; | ||
var copy = { body: obj.body, statusCode: obj.statusCode }; | ||
copy.should.have.property('statusCode', code); | ||
obj.should.have.property('statusCode', code); | ||
}); | ||
@@ -66,3 +99,3 @@ | ||
Assertion.add('json', function() { | ||
this.have.header('content-type').match(/application\/json/i); | ||
this.have.contentType('application/json'); | ||
}); | ||
@@ -82,5 +115,39 @@ | ||
Assertion.add('html', function() { | ||
this.have.header('content-type').match(/text\/html/i); | ||
this.have.contentType('text/html'); | ||
}); | ||
/** | ||
* Shortcut for .should.header('content-type', 'application/xml') | ||
* | ||
* @name xml | ||
* @memberOf Assertion | ||
* @category assertion http | ||
* @module should-http | ||
* @example | ||
* | ||
* res.should.be.xml(); | ||
*/ | ||
Assertion.add('xml', function() { | ||
this.have.contentType('application/xml'); | ||
}); | ||
/** | ||
* Check if response have header content-type with given type and charset | ||
* | ||
* @name contentType | ||
* @memberOf Assertion | ||
* @category assertion http | ||
* @module should-http | ||
* @param {string} type | ||
* @param {string} [charset] | ||
*/ | ||
Assertion.add('contentType', function(type, charset) { | ||
this.have.header('content-type'); | ||
var ct = contentType.parse(this.obj); //changed by previous assertion | ||
should(ct.type).match(type); | ||
if(charset != null) { | ||
should(ct.parameters).have.property('charset').which.match(charset); | ||
} | ||
}); | ||
}; | ||
{ | ||
"name": "should-http", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Http requests, response assertions for should.js", | ||
@@ -31,3 +31,6 @@ "main": "index.js", | ||
"should": ">= 4.x" | ||
}, | ||
"dependencies": { | ||
"content-type": "^1.0.2" | ||
} | ||
} |
@@ -46,2 +46,30 @@ var should = require('should'); | ||
it('test .xml', function(){ | ||
var req = { | ||
headers: { | ||
'content-type': 'application/xml' | ||
} | ||
}; | ||
req.should.be.xml(); | ||
req = { | ||
headers: { | ||
'content-type': 'application/xml; charset=utf-8' | ||
} | ||
}; | ||
req.should.be.xml(); | ||
req = { | ||
headers: { | ||
'content-type': 'text/html' | ||
} | ||
}; | ||
req.should.not.be.xml(); | ||
({}).should.not.be.xml(); | ||
}); | ||
it('test .status', function() { | ||
@@ -48,0 +76,0 @@ ({ statusCode: 300 }).should.have.not.status(200); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9699
10
235
2
2
+ Addedcontent-type@^1.0.2
+ Addedcontent-type@1.0.5(transitive)