Skip to content

Add matcher to check for charset. #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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