Skip to content

Commit 50d9db4

Browse files
kieranskeithamus
authored andcommittedAug 23, 2019
feat: add charset assertion (#253)
1 parent 7f5f260 commit 50d9db4

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed
 

‎README.md

+10
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,16 @@ expect(req).to.be.html;
343343
expect(req).to.be.text;
344344
```
345345

346+
### .charset
347+
348+
349+
350+
Assert that a `Response` or `Request` object has a given charset.
351+
352+
```js
353+
expect(req).to.have.charset('utf-8');
354+
```
355+
346356
### .redirect
347357

348358

‎lib/http.js

+35
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = function (chai, _) {
2121
var qs = require('qs');
2222
var url = require('url');
2323
var Cookie = require('cookiejar');
24+
var charset = require("charset");
2425

2526
/*!
2627
* Aliases.
@@ -239,6 +240,40 @@ module.exports = function (chai, _) {
239240
.keys(contentTypes)
240241
.forEach(checkContentType);
241242

243+
/**
244+
* ### .charset
245+
*
246+
* Assert that a `Response` or `Request` object has a given charset.
247+
*
248+
* ```js
249+
* expect(req).to.have.charset('utf-8');
250+
* ```
251+
*
252+
* @name charset
253+
* @api public
254+
*/
255+
256+
Assertion.addMethod('charset', function (value) {
257+
value = value.toLowerCase();
258+
259+
var headers = this._obj.headers;
260+
var cs = charset(headers);
261+
262+
/*
263+
* Fix charset() treating "utf8" as a special case
264+
* See https://github.com/node-modules/charset/issues/12
265+
*/
266+
if (cs === "utf8") {
267+
cs = "utf-8";
268+
}
269+
270+
this.assert(
271+
cs != null && value === cs
272+
, 'expected content type to have ' + value + ' charset'
273+
, 'expected content type to not have ' + value + ' charset'
274+
)
275+
});
276+
242277
/**
243278
* ### .redirect
244279
*

‎package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"dependencies": {
5353
"@types/chai": "4",
5454
"@types/superagent": "^3.8.3",
55+
"charset": "^1.0.1",
5556
"cookiejar": "^2.1.1",
5657
"is-ip": "^2.0.0",
5758
"methods": "^1.1.2",

‎test/http.js

+29
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,33 @@ describe('assertions', function () {
403403
}).should.throw('expected cookie \'name2\' to have value \'value\' but got \'value2\'');
404404

405405
});
406+
407+
describe('#charset', function () {
408+
it("should match charset in content type", function() {
409+
var req = { headers: { 'content-type': [ 'text/plain; charset=utf-8' ] } };
410+
req.should.to.have.charset("utf-8");
411+
412+
(function () {
413+
req.should.not.have.charset("utf-8");
414+
}).should.throw('expected content type to not have utf-8 charset');
415+
});
416+
417+
it("should handle no content type", function() {
418+
var req = { headers: {} };
419+
req.should.not.have.charset("utf-8");
420+
421+
(function () {
422+
req.should.to.have.charset("utf-8");
423+
}).should.throw('expected content type to have utf-8 charset');
424+
});
425+
426+
it("should handle no charset in content type", function() {
427+
var req = { headers: { 'content-type': [ 'text/plain' ] } };
428+
req.should.not.have.charset("utf-8");
429+
430+
(function () {
431+
req.should.to.have.charset("utf-8");
432+
}).should.throw('expected content type to have utf-8 charset');
433+
});
434+
});
406435
});

‎types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ declare global {
3535

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

38+
charset(charset: string): Assertion;
39+
3840
headers: Assertion;
3941
json: Assertion;
4042
text: Assertion;

0 commit comments

Comments
 (0)
Please sign in to comment.