From 13e7def3d6664b9951dfab68509f63ae2eb4c104 Mon Sep 17 00:00:00 2001 From: Kieran Simpson Date: Wed, 26 Jun 2019 21:33:49 +1000 Subject: [PATCH] feat: add charset assertion --- README.md | 10 ++++++++++ lib/http.js | 35 +++++++++++++++++++++++++++++++++++ package-lock.json | 5 +++++ package.json | 1 + test/http.js | 29 +++++++++++++++++++++++++++++ types/index.d.ts | 2 ++ 6 files changed, 82 insertions(+) diff --git a/README.md b/README.md index 8df12ee..e6a919f 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,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 diff --git a/lib/http.js b/lib/http.js index 4dc8cc5..95e3a2e 100644 --- a/lib/http.js +++ b/lib/http.js @@ -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. @@ -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 * diff --git a/package-lock.json b/package-lock.json index 351921b..710569d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1712,6 +1712,11 @@ "supports-color": "^4.0.0" } }, + "charset": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/charset/-/charset-1.0.1.tgz", + "integrity": "sha512-6dVyOOYjpfFcL1Y4qChrAoQLRHvj2ziyhcm0QJlhOcAhykL/k1kTUPbeo+87MNRTRdk2OIIsIXbuF3x2wi5EXg==" + }, "check-error": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz", diff --git a/package.json b/package.json index 5c2dc9c..cde8788 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/http.js b/test/http.js index 76ba716..0002f9d 100644 --- a/test/http.js +++ b/test/http.js @@ -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'); + }); + }); }); diff --git a/types/index.d.ts b/types/index.d.ts index 0f79c09..628961d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -35,6 +35,8 @@ declare global { header(key: string, value?: string | RegExp): Assertion; + charset(charset: string): Assertion; + headers: Assertion; json: Assertion; text: Assertion;