pax_global_header00006660000000000000000000000064143655435150014525gustar00rootroot0000000000000052 comment=6115a4064e4dfd9845241c3f89c233ee2423deeb content-type-1.0.5/000077500000000000000000000000001436554351500141615ustar00rootroot00000000000000content-type-1.0.5/.eslintignore000066400000000000000000000000261436554351500166620ustar00rootroot00000000000000coverage node_modules content-type-1.0.5/.eslintrc.yml000066400000000000000000000000771436554351500166110ustar00rootroot00000000000000root: true extends: standard rules: no-param-reassign: error content-type-1.0.5/.github/000077500000000000000000000000001436554351500155215ustar00rootroot00000000000000content-type-1.0.5/.github/workflows/000077500000000000000000000000001436554351500175565ustar00rootroot00000000000000content-type-1.0.5/.github/workflows/ci.yml000066400000000000000000000153351436554351500207030ustar00rootroot00000000000000name: ci on: - pull_request - push jobs: test: runs-on: ubuntu-20.04 strategy: matrix: name: - Node.js 0.6 - Node.js 0.8 - Node.js 0.10 - Node.js 0.12 - io.js 1.x - io.js 2.x - io.js 3.x - Node.js 4.x - Node.js 5.x - Node.js 6.x - Node.js 7.x - Node.js 8.x - Node.js 9.x - Node.js 10.x - Node.js 11.x - Node.js 12.x - Node.js 13.x - Node.js 14.x - Node.js 15.x - Node.js 16.x - Node.js 17.x - Node.js 18.x - Node.js 19.x include: - name: Node.js 0.6 node-version: "0.6" npm-i: mocha@1.21.5 npm-rm: nyc - name: Node.js 0.8 node-version: "0.8" npm-i: mocha@2.5.3 npm-rm: nyc - name: Node.js 0.10 node-version: "0.10" npm-i: mocha@3.5.3 nyc@10.3.2 - name: Node.js 0.12 node-version: "0.12" npm-i: mocha@3.5.3 nyc@10.3.2 - name: io.js 1.x node-version: "1.8" npm-i: mocha@3.5.3 nyc@10.3.2 - name: io.js 2.x node-version: "2.5" npm-i: mocha@3.5.3 nyc@10.3.2 - name: io.js 3.x node-version: "3.3" npm-i: mocha@3.5.3 nyc@10.3.2 - name: Node.js 4.x node-version: "4.9" npm-i: mocha@5.2.0 nyc@11.9.0 - name: Node.js 5.x node-version: "5.12" npm-i: mocha@5.2.0 nyc@11.9.0 - name: Node.js 6.x node-version: "6.16" npm-i: mocha@6.2.3 nyc@14.1.1 - name: Node.js 7.x node-version: "7.10" npm-i: mocha@6.2.3 nyc@14.1.1 - name: Node.js 8.x node-version: "8.17" npm-i: mocha@7.2.0 - name: Node.js 9.x node-version: "9.11" npm-i: mocha@7.2.0 - name: Node.js 10.x node-version: "10.24" npm-i: mocha@8.4.0 - name: Node.js 11.x node-version: "11.15" npm-i: mocha@8.4.0 - name: Node.js 12.x node-version: "12.22" npm-i: mocha@9.2.2 - name: Node.js 13.x node-version: "13.14" npm-i: mocha@9.2.2 - name: Node.js 14.x node-version: "14.21" - name: Node.js 15.x node-version: "15.14" - name: Node.js 16.x node-version: "16.19" - name: Node.js 17.x node-version: "17.9" - name: Node.js 18.x node-version: "18.13" - name: Node.js 19.x node-version: "19.5" steps: - uses: actions/checkout@v3 - name: Install Node.js ${{ matrix.node-version }} shell: bash -eo pipefail -l {0} run: | if [[ "${{ matrix.node-version }}" == 0.6* ]]; then sudo sh -c 'echo "deb http://us.archive.ubuntu.com/ubuntu/ bionic universe" >> /etc/apt/sources.list' sudo sh -c 'echo "deb http://security.ubuntu.com/ubuntu bionic-security main" >> /etc/apt/sources.list' sudo apt-get update sudo apt-get install g++-4.8 gcc-4.8 libssl1.0-dev python2 python-is-python2 export CC=/usr/bin/gcc-4.8 export CXX=/usr/bin/g++-4.8 export CXXCOM='$CXX -o $TARGET -c $CXXFLAGS $CCFLAGS -Wno-unused-local-typedefs -Wno-maybe-uninitialized -Wno-narrowing -Wno-strict-overflow $_CCCOMCOM $SOURCES' fi nvm install --default ${{ matrix.node-version }} if [[ "${{ matrix.node-version }}" == 0.* && "$(cut -d. -f2 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then nvm install --alias=npm 0.10 nvm use ${{ matrix.node-version }} if [[ "$(npm -v)" == 1.1.* ]]; then nvm exec npm npm install -g npm@1.1 ln -fs "$(which npm)" "$(dirname "$(nvm which npm)")/npm" else sed -i '1s;^.*$;'"$(printf '#!%q' "$(nvm which npm)")"';' "$(readlink -f "$(which npm)")" fi npm config set strict-ssl false fi dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH" - name: Configure npm run: | if [[ "$(npm config get package-lock)" == "true" ]]; then npm config set package-lock false else npm config set shrinkwrap false fi - name: Remove npm module(s) ${{ matrix.npm-rm }} run: npm rm --silent --save-dev ${{ matrix.npm-rm }} if: matrix.npm-rm != '' - name: Install npm module(s) ${{ matrix.npm-i }} run: npm install --save-dev ${{ matrix.npm-i }} if: matrix.npm-i != '' - name: Setup Node.js version-specific dependencies shell: bash run: | # eslint for linting # - remove on Node.js < 12 if [[ "$(cut -d. -f1 <<< "${{ matrix.node-version }}")" -lt 12 ]]; then node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \ grep -E '^eslint(-|$)' | \ sort -r | \ xargs -n1 npm rm --silent --save-dev fi - name: Install Node.js dependencies run: npm install - name: List environment id: list_env shell: bash run: | echo "node@$(node -v)" echo "npm@$(npm -v)" npm -s ls ||: (npm -s ls --depth=0 ||:) | awk -F'[ @]' 'NR>1 && $2 { print $2 "=" $3 }' >> "$GITHUB_OUTPUT" - name: Run tests shell: bash run: | if npm -ps ls nyc | grep -q nyc; then npm run test-ci cp coverage/lcov.info "coverage/${{ matrix.name }}.lcov" else npm test fi - name: Lint code if: steps.list_env.outputs.eslint != '' run: npm run lint - name: Collect code coverage if: steps.list_env.outputs.nyc != '' run: | if [[ -d ./coverage ]]; then mv ./coverage "./${{ matrix.name }}" mkdir ./coverage mv "./${{ matrix.name }}" "./coverage/${{ matrix.name }}" fi - name: Upload code coverage uses: actions/upload-artifact@v3 if: steps.list_env.outputs.nyc != '' with: name: coverage path: ./coverage retention-days: 1 coverage: needs: test runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Install lcov shell: bash run: sudo apt-get -y install lcov - name: Collect coverage reports uses: actions/download-artifact@v3 with: name: coverage path: ./coverage - name: Merge coverage reports shell: bash run: find ./coverage -name lcov.info -exec printf '-a %q\n' {} \; | xargs lcov -o ./coverage/lcov.info - name: Upload coverage report uses: coverallsapp/github-action@master with: github-token: ${{ secrets.GITHUB_TOKEN }} content-type-1.0.5/.gitignore000066400000000000000000000001051436554351500161450ustar00rootroot00000000000000.nyc_output/ coverage/ node_modules/ npm-debug.log package-lock.json content-type-1.0.5/HISTORY.md000066400000000000000000000010131436554351500156370ustar00rootroot000000000000001.0.5 / 2023-01-29 ================== * perf: skip value escaping when unnecessary 1.0.4 / 2017-09-11 ================== * perf: skip parameter parsing when no parameters 1.0.3 / 2017-09-10 ================== * perf: remove argument reassignment 1.0.2 / 2016-05-09 ================== * perf: enable strict mode 1.0.1 / 2015-02-13 ================== * Improve missing `Content-Type` header error message 1.0.0 / 2015-02-01 ================== * Initial implementation, derived from `media-typer@0.3.0` content-type-1.0.5/LICENSE000066400000000000000000000021011436554351500151600ustar00rootroot00000000000000(The MIT License) Copyright (c) 2015 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. content-type-1.0.5/README.md000066400000000000000000000053361436554351500154470ustar00rootroot00000000000000# content-type [![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Node.js Version][node-image]][node-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] Create and parse HTTP Content-Type header according to RFC 7231 ## Installation ```sh $ npm install content-type ``` ## API ```js var contentType = require('content-type') ``` ### contentType.parse(string) ```js var obj = contentType.parse('image/svg+xml; charset=utf-8') ``` Parse a `Content-Type` header. This will return an object with the following properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`): - `type`: The media type (the type and subtype, always lower case). Example: `'image/svg+xml'` - `parameters`: An object of the parameters in the media type (name of parameter always lower case). Example: `{charset: 'utf-8'}` Throws a `TypeError` if the string is missing or invalid. ### contentType.parse(req) ```js var obj = contentType.parse(req) ``` Parse the `Content-Type` header from the given `req`. Short-cut for `contentType.parse(req.headers['content-type'])`. Throws a `TypeError` if the `Content-Type` header is missing or invalid. ### contentType.parse(res) ```js var obj = contentType.parse(res) ``` Parse the `Content-Type` header set on the given `res`. Short-cut for `contentType.parse(res.getHeader('content-type'))`. Throws a `TypeError` if the `Content-Type` header is missing or invalid. ### contentType.format(obj) ```js var str = contentType.format({ type: 'image/svg+xml', parameters: { charset: 'utf-8' } }) ``` Format an object into a `Content-Type` header. This will return a string of the content type for the given object with the following properties (examples are shown that produce the string `'image/svg+xml; charset=utf-8'`): - `type`: The media type (will be lower-cased). Example: `'image/svg+xml'` - `parameters`: An object of the parameters in the media type (name of the parameter will be lower-cased). Example: `{charset: 'utf-8'}` Throws a `TypeError` if the object contains an invalid type or parameter names. ## License [MIT](LICENSE) [ci-image]: https://badgen.net/github/checks/jshttp/content-type/master?label=ci [ci-url]: https://github.com/jshttp/content-type/actions/workflows/ci.yml [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/content-type/master [coveralls-url]: https://coveralls.io/r/jshttp/content-type?branch=master [node-image]: https://badgen.net/npm/node/content-type [node-url]: https://nodejs.org/en/download [npm-downloads-image]: https://badgen.net/npm/dm/content-type [npm-url]: https://npmjs.org/package/content-type [npm-version-image]: https://badgen.net/npm/v/content-type content-type-1.0.5/index.js000066400000000000000000000116121436554351500156270ustar00rootroot00000000000000/*! * content-type * Copyright(c) 2015 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * RegExp to match *( ";" parameter ) in RFC 7231 sec 3.1.1.1 * * parameter = token "=" ( token / quoted-string ) * token = 1*tchar * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" * / DIGIT / ALPHA * ; any VCHAR, except delimiters * quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE * qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text * obs-text = %x80-FF * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text ) */ var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g // eslint-disable-line no-control-regex var TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/ // eslint-disable-line no-control-regex var TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/ /** * RegExp to match quoted-pair in RFC 7230 sec 3.2.6 * * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text ) * obs-text = %x80-FF */ var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g // eslint-disable-line no-control-regex /** * RegExp to match chars that must be quoted-pair in RFC 7230 sec 3.2.6 */ var QUOTE_REGEXP = /([\\"])/g /** * RegExp to match type in RFC 7231 sec 3.1.1.1 * * media-type = type "/" subtype * type = token * subtype = token */ var TYPE_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/ /** * Module exports. * @public */ exports.format = format exports.parse = parse /** * Format object to media type. * * @param {object} obj * @return {string} * @public */ function format (obj) { if (!obj || typeof obj !== 'object') { throw new TypeError('argument obj is required') } var parameters = obj.parameters var type = obj.type if (!type || !TYPE_REGEXP.test(type)) { throw new TypeError('invalid type') } var string = type // append parameters if (parameters && typeof parameters === 'object') { var param var params = Object.keys(parameters).sort() for (var i = 0; i < params.length; i++) { param = params[i] if (!TOKEN_REGEXP.test(param)) { throw new TypeError('invalid parameter name') } string += '; ' + param + '=' + qstring(parameters[param]) } } return string } /** * Parse media type to object. * * @param {string|object} string * @return {Object} * @public */ function parse (string) { if (!string) { throw new TypeError('argument string is required') } // support req/res-like objects as argument var header = typeof string === 'object' ? getcontenttype(string) : string if (typeof header !== 'string') { throw new TypeError('argument string is required to be a string') } var index = header.indexOf(';') var type = index !== -1 ? header.slice(0, index).trim() : header.trim() if (!TYPE_REGEXP.test(type)) { throw new TypeError('invalid media type') } var obj = new ContentType(type.toLowerCase()) // parse parameters if (index !== -1) { var key var match var value PARAM_REGEXP.lastIndex = index while ((match = PARAM_REGEXP.exec(header))) { if (match.index !== index) { throw new TypeError('invalid parameter format') } index += match[0].length key = match[1].toLowerCase() value = match[2] if (value.charCodeAt(0) === 0x22 /* " */) { // remove quotes value = value.slice(1, -1) // remove escapes if (value.indexOf('\\') !== -1) { value = value.replace(QESC_REGEXP, '$1') } } obj.parameters[key] = value } if (index !== header.length) { throw new TypeError('invalid parameter format') } } return obj } /** * Get content-type from req/res objects. * * @param {object} * @return {Object} * @private */ function getcontenttype (obj) { var header if (typeof obj.getHeader === 'function') { // res-like header = obj.getHeader('content-type') } else if (typeof obj.headers === 'object') { // req-like header = obj.headers && obj.headers['content-type'] } if (typeof header !== 'string') { throw new TypeError('content-type header is missing from object') } return header } /** * Quote a string if necessary. * * @param {string} val * @return {string} * @private */ function qstring (val) { var str = String(val) // no need to quote tokens if (TOKEN_REGEXP.test(str)) { return str } if (str.length > 0 && !TEXT_REGEXP.test(str)) { throw new TypeError('invalid parameter value') } return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"' } /** * Class to represent a content type. * @private */ function ContentType (type) { this.parameters = Object.create(null) this.type = type } content-type-1.0.5/package.json000066400000000000000000000020631436554351500164500ustar00rootroot00000000000000{ "name": "content-type", "description": "Create and parse HTTP Content-Type header", "version": "1.0.5", "author": "Douglas Christopher Wilson ", "license": "MIT", "keywords": [ "content-type", "http", "req", "res", "rfc7231" ], "repository": "jshttp/content-type", "devDependencies": { "deep-equal": "1.0.1", "eslint": "8.32.0", "eslint-config-standard": "15.0.1", "eslint-plugin-import": "2.27.5", "eslint-plugin-node": "11.1.0", "eslint-plugin-promise": "6.1.1", "eslint-plugin-standard": "4.1.0", "mocha": "10.2.0", "nyc": "15.1.0" }, "files": [ "LICENSE", "HISTORY.md", "README.md", "index.js" ], "engines": { "node": ">= 0.6" }, "scripts": { "lint": "eslint .", "test": "mocha --reporter spec --check-leaks --bail test/", "test-ci": "nyc --reporter=lcovonly --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test", "version": "node scripts/version-history.js && git add HISTORY.md" } } content-type-1.0.5/scripts/000077500000000000000000000000001436554351500156505ustar00rootroot00000000000000content-type-1.0.5/scripts/version-history.js000066400000000000000000000027771436554351500214070ustar00rootroot00000000000000'use strict' var fs = require('fs') var path = require('path') var HISTORY_FILE_PATH = path.join(__dirname, '..', 'HISTORY.md') var MD_HEADER_REGEXP = /^====*$/ var VERSION = process.env.npm_package_version var VERSION_PLACEHOLDER_REGEXP = /^(?:unreleased|(\d+\.)+x)$/ var historyFileLines = fs.readFileSync(HISTORY_FILE_PATH, 'utf-8').split('\n') if (!MD_HEADER_REGEXP.test(historyFileLines[1])) { console.error('Missing header in HISTORY.md') process.exit(1) } if (!VERSION_PLACEHOLDER_REGEXP.test(historyFileLines[0])) { console.error('Missing placeholder version in HISTORY.md') process.exit(1) } if (historyFileLines[0].indexOf('x') !== -1) { var versionCheckRegExp = new RegExp('^' + historyFileLines[0].replace('x', '.+') + '$') if (!versionCheckRegExp.test(VERSION)) { console.error('Version %s does not match placeholder %s', VERSION, historyFileLines[0]) process.exit(1) } } historyFileLines[0] = VERSION + ' / ' + getLocaleDate() historyFileLines[1] = repeat('=', historyFileLines[0].length) fs.writeFileSync(HISTORY_FILE_PATH, historyFileLines.join('\n')) function getLocaleDate () { var now = new Date() return zeroPad(now.getFullYear(), 4) + '-' + zeroPad(now.getMonth() + 1, 2) + '-' + zeroPad(now.getDate(), 2) } function repeat (str, length) { var out = '' for (var i = 0; i < length; i++) { out += str } return out } function zeroPad (number, length) { var num = number.toString() while (num.length < length) { num = '0' + num } return num } content-type-1.0.5/test/000077500000000000000000000000001436554351500151405ustar00rootroot00000000000000content-type-1.0.5/test/.eslintrc.yml000066400000000000000000000000231436554351500175570ustar00rootroot00000000000000env: mocha: true content-type-1.0.5/test/contentType_format.js000066400000000000000000000050051436554351500213620ustar00rootroot00000000000000 var assert = require('assert') var contentType = require('..') describe('contentType.format(obj)', function () { it('should format basic type', function () { var str = contentType.format({ type: 'text/html' }) assert.strictEqual(str, 'text/html') }) it('should format type with suffix', function () { var str = contentType.format({ type: 'image/svg+xml' }) assert.strictEqual(str, 'image/svg+xml') }) it('should format type with parameter', function () { var str = contentType.format({ type: 'text/html', parameters: { charset: 'utf-8' } }) assert.strictEqual(str, 'text/html; charset=utf-8') }) it('should format type with parameter that needs quotes', function () { var str = contentType.format({ type: 'text/html', parameters: { foo: 'bar or "baz"' } }) assert.strictEqual(str, 'text/html; foo="bar or \\"baz\\""') }) it('should format type with parameter with empty value', function () { var str = contentType.format({ type: 'text/html', parameters: { foo: '' } }) assert.strictEqual(str, 'text/html; foo=""') }) it('should format type with multiple parameters', function () { var str = contentType.format({ type: 'text/html', parameters: { charset: 'utf-8', foo: 'bar', bar: 'baz' } }) assert.strictEqual(str, 'text/html; bar=baz; charset=utf-8; foo=bar') }) it('should require argument', function () { assert.throws(contentType.format.bind(null), /argument obj is required/) }) it('should reject non-objects', function () { assert.throws(contentType.format.bind(null, 7), /argument obj is required/) }) it('should require type', function () { var obj = {} assert.throws(contentType.format.bind(null, obj), /invalid type/) }) it('should reject invalid type', function () { var obj = { type: 'text/' } assert.throws(contentType.format.bind(null, obj), /invalid type/) }) it('should reject invalid type with LWS', function () { var obj = { type: ' text/html' } assert.throws(contentType.format.bind(null, obj), /invalid type/) }) it('should reject invalid parameter name', function () { var obj = { type: 'image/svg', parameters: { 'foo/': 'bar' } } assert.throws(contentType.format.bind(null, obj), /invalid parameter name/) }) it('should reject invalid parameter value', function () { var obj = { type: 'image/svg', parameters: { foo: 'bar\u0000' } } assert.throws(contentType.format.bind(null, obj), /invalid parameter value/) }) }) content-type-1.0.5/test/contentType_parse.js000066400000000000000000000110431436554351500212030ustar00rootroot00000000000000 var assert = require('assert') var contentType = require('..') var deepEqual = require('deep-equal') var invalidTypes = [ ' ', 'null', 'undefined', '/', 'text / plain', 'text/;plain', 'text/"plain"', 'text/p£ain', 'text/(plain)', 'text/@plain', 'text/plain,wrong' ] describe('contentType.parse(string)', function () { it('should parse basic type', function () { var type = contentType.parse('text/html') assert.strictEqual(type.type, 'text/html') }) it('should parse with suffix', function () { var type = contentType.parse('image/svg+xml') assert.strictEqual(type.type, 'image/svg+xml') }) it('should parse basic type with surrounding OWS', function () { var type = contentType.parse(' text/html ') assert.strictEqual(type.type, 'text/html') }) it('should parse parameters', function () { var type = contentType.parse('text/html; charset=utf-8; foo=bar') assert.strictEqual(type.type, 'text/html') assert.ok(deepEqual(type.parameters, { charset: 'utf-8', foo: 'bar' })) }) it('should parse parameters with extra LWS', function () { var type = contentType.parse('text/html ; charset=utf-8 ; foo=bar') assert.strictEqual(type.type, 'text/html') assert.ok(deepEqual(type.parameters, { charset: 'utf-8', foo: 'bar' })) }) it('should lower-case type', function () { var type = contentType.parse('IMAGE/SVG+XML') assert.strictEqual(type.type, 'image/svg+xml') }) it('should lower-case parameter names', function () { var type = contentType.parse('text/html; Charset=UTF-8') assert.strictEqual(type.type, 'text/html') assert.ok(deepEqual(type.parameters, { charset: 'UTF-8' })) }) it('should unquote parameter values', function () { var type = contentType.parse('text/html; charset="UTF-8"') assert.strictEqual(type.type, 'text/html') assert.ok(deepEqual(type.parameters, { charset: 'UTF-8' })) }) it('should unquote parameter values with escapes', function () { var type = contentType.parse('text/html; charset = "UT\\F-\\\\\\"8\\""') assert.strictEqual(type.type, 'text/html') assert.ok(deepEqual(type.parameters, { charset: 'UTF-\\"8"' })) }) it('should handle balanced quotes', function () { var type = contentType.parse('text/html; param="charset=\\"utf-8\\"; foo=bar"; bar=foo') assert.strictEqual(type.type, 'text/html') assert.ok(deepEqual(type.parameters, { param: 'charset="utf-8"; foo=bar', bar: 'foo' })) }) invalidTypes.forEach(function (type) { it('should throw on invalid media type ' + type, function () { assert.throws(contentType.parse.bind(null, type), /invalid media type/) }) }) it('should throw on invalid parameter format', function () { assert.throws(contentType.parse.bind(null, 'text/plain; foo="bar'), /invalid parameter format/) assert.throws(contentType.parse.bind(null, 'text/plain; profile=http://localhost; foo=bar'), /invalid parameter format/) assert.throws(contentType.parse.bind(null, 'text/plain; profile=http://localhost'), /invalid parameter format/) }) it('should require argument', function () { assert.throws(contentType.parse.bind(null), /string.*required/) }) it('should reject non-strings', function () { assert.throws(contentType.parse.bind(null, 7), /string.*required/) }) }) describe('contentType.parse(req)', function () { it('should parse content-type header', function () { var req = { headers: { 'content-type': 'text/html' } } var type = contentType.parse(req) assert.strictEqual(type.type, 'text/html') }) it('should reject objects without headers property', function () { assert.throws(contentType.parse.bind(null, {}), /content-type header is missing/) }) it('should reject missing content-type', function () { var req = { headers: {} } assert.throws(contentType.parse.bind(null, req), /content-type header is missing/) }) }) describe('contentType.parse(res)', function () { it('should parse content-type header', function () { var res = { getHeader: function () { return 'text/html' } } var type = contentType.parse(res) assert.strictEqual(type.type, 'text/html') }) it('should reject objects without getHeader method', function () { assert.throws(contentType.parse.bind(null, {}), /content-type header is missing/) }) it('should reject missing content-type', function () { var res = { getHeader: function () {} } assert.throws(contentType.parse.bind(null, res), /content-type header is missing/) }) })