Skip to content

Commit

Permalink
feat: add charset assertion (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
kierans authored and keithamus committed Aug 23, 2019
1 parent 7f5f260 commit 50d9db4
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -343,6 +343,16 @@ expect(req).to.be.html;
expect(req).to.be.text;
```

### .charset



Assert that a `Response` or `Request` object has a given charset.

```js
expect(req).to.have.charset('utf-8');
```

### .redirect


Expand Down
35 changes: 35 additions & 0 deletions lib/http.js
Expand Up @@ -21,6 +21,7 @@ module.exports = function (chai, _) {
var qs = require('qs');
var url = require('url');
var Cookie = require('cookiejar');
var charset = require("charset");

/*!
* Aliases.
Expand Down Expand Up @@ -239,6 +240,40 @@ module.exports = function (chai, _) {
.keys(contentTypes)
.forEach(checkContentType);

/**
* ### .charset
*
* Assert that a `Response` or `Request` object has a given charset.
*
* ```js
* expect(req).to.have.charset('utf-8');
* ```
*
* @name charset
* @api public
*/

Assertion.addMethod('charset', function (value) {
value = value.toLowerCase();

var headers = this._obj.headers;
var cs = charset(headers);

/*
* Fix charset() treating "utf8" as a special case
* See https://github.com/node-modules/charset/issues/12
*/
if (cs === "utf8") {
cs = "utf-8";
}

this.assert(
cs != null && value === cs
, 'expected content type to have ' + value + ' charset'
, 'expected content type to not have ' + value + ' charset'
)
});

/**
* ### .redirect
*
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -52,6 +52,7 @@
"dependencies": {
"@types/chai": "4",
"@types/superagent": "^3.8.3",
"charset": "^1.0.1",
"cookiejar": "^2.1.1",
"is-ip": "^2.0.0",
"methods": "^1.1.2",
Expand Down
29 changes: 29 additions & 0 deletions test/http.js
Expand Up @@ -403,4 +403,33 @@ describe('assertions', function () {
}).should.throw('expected cookie \'name2\' to have value \'value\' but got \'value2\'');

});

describe('#charset', function () {
it("should match charset in content type", function() {
var req = { headers: { 'content-type': [ 'text/plain; charset=utf-8' ] } };
req.should.to.have.charset("utf-8");

(function () {
req.should.not.have.charset("utf-8");
}).should.throw('expected content type to not have utf-8 charset');
});

it("should handle no content type", function() {
var req = { headers: {} };
req.should.not.have.charset("utf-8");

(function () {
req.should.to.have.charset("utf-8");
}).should.throw('expected content type to have utf-8 charset');
});

it("should handle no charset in content type", function() {
var req = { headers: { 'content-type': [ 'text/plain' ] } };
req.should.not.have.charset("utf-8");

(function () {
req.should.to.have.charset("utf-8");
}).should.throw('expected content type to have utf-8 charset');
});
});
});
2 changes: 2 additions & 0 deletions types/index.d.ts
Expand Up @@ -35,6 +35,8 @@ declare global {

header(key: string, value?: string | RegExp): Assertion;

charset(charset: string): Assertion;

headers: Assertion;
json: Assertion;
text: Assertion;
Expand Down

0 comments on commit 50d9db4

Please sign in to comment.