Skip to content

Commit

Permalink
tests: add cases with empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
herrmannplatz committed Jan 21, 2023
1 parent 238eedd commit f704193
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
8 changes: 8 additions & 0 deletions test/charset.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ describe('accepts.charsets()', function () {
assert.strictEqual(accept.charsets(['utf-7', 'utf-8']), 'utf-8')
})
})

describe('with an empty array', function () {
it('should return accepted types', function () {
var req = createRequest('utf-8, iso-8859-1;q=0.2, utf-7;q=0.5')
var accept = accepts(req)
assert.ok(deepEqual(accept.charsets(), ['utf-8', 'utf-7', 'iso-8859-1']))
})
})
})

function createRequest (charset) {
Expand Down
9 changes: 9 additions & 0 deletions test/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ describe('accepts.encodings()', function () {
assert.strictEqual(accept.encodings(['compress', 'gzip']), 'gzip')
})
})

describe('with an empty array', function () {
it('should return accepted types', function () {
var req = createRequest('gzip, compress;q=0.2')
var accept = accepts(req)
assert.ok(deepEqual(accept.encodings([]), ['gzip', 'compress', 'identity']))
assert.strictEqual(accept.encodings('gzip', 'compress'), 'gzip')
})
})
})

function createRequest (encoding) {
Expand Down
8 changes: 8 additions & 0 deletions test/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ describe('accepts.languages()', function () {
assert.strictEqual(accept.languages(['es', 'en']), 'es')
})
})

describe('with an empty array', function () {
it('should return accepted types', function () {
var req = createRequest('en;q=0.8, es, pt')
var accept = accepts(req)
assert.deepEqual(accept.languages([]), ['es', 'pt', 'en'])
})
})
})

function createRequest (language) {
Expand Down
8 changes: 8 additions & 0 deletions test/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ describe('accepts.types()', function () {
assert.strictEqual(accept.types('image/png'), false)
})
})

describe('with an empty array', function () {
it('should return all accepted types', function () {
var req = createRequest('application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain')
var accept = accepts(req)
assert.ok(deepEqual(accept.types([]), ['text/html', 'text/plain', 'image/jpeg', 'application/*']))
})
})
})

function createRequest (type) {
Expand Down
20 changes: 10 additions & 10 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare namespace accepts {
*/
charset(): string[];
charset(charsets: string[]): string | string[] | false;
charset(...charsets: string[]): string | string[] | false;
charset(...charsets: string[]): string | false;

/**
* Return the first accepted charset. If nothing in `charsets` is accepted, then `false` is returned.
Expand All @@ -20,7 +20,7 @@ declare namespace accepts {
*/
charsets(): string[];
charsets(charsets: string[]): string | string[] | false;
charsets(...charsets: string[]): string | string[] | false;
charsets(...charsets: string[]): string | false;

/**
* Return the first accepted encoding. If nothing in `encodings` is accepted, then `false` is returned.
Expand All @@ -29,7 +29,7 @@ declare namespace accepts {
*/
encoding(): string[];
encoding(encodings: string[]): string | string[] | false;
encoding(...encodings: string[]): string | string[] | false;
encoding(...encodings: string[]): string | false;

/**
* Return the first accepted encoding. If nothing in `encodings` is accepted, then `false` is returned.
Expand All @@ -38,7 +38,7 @@ declare namespace accepts {
*/
encodings(): string[];
encodings(encodings: string[]): string | string[] | false;
encodings(...encodings: string[]): string | string[] | false;
encodings(...encodings: string[]): string | false;

/**
* Return the first accepted language. If nothing in `languages` is accepted, then `false` is returned.
Expand All @@ -47,7 +47,7 @@ declare namespace accepts {
*/
language(): string[];
language(languages: string[]): string | string[] | false;
language(...languages: string[]): string | string[] | false;
language(...languages: string[]): string | false;

/**
* Return the first accepted language. If nothing in `languages` is accepted, then `false` is returned.
Expand All @@ -56,7 +56,7 @@ declare namespace accepts {
*/
languages(): string[];
languages(languages: string[]): string | string[] | false;
languages(...languages: string[]): string | string[] | false;
languages(...languages: string[]): string | false;

/**
* Return the first accepted language. If nothing in `languages` is accepted, then `false` is returned.
Expand All @@ -65,7 +65,7 @@ declare namespace accepts {
*/
lang(): string[];
lang(languages: string[]): string | string[] | false;
lang(...languages: string[]): string | string[] | false;
lang(...languages: string[]): string | false;

/**
* Return the first accepted language. If nothing in `languages` is accepted, then `false` is returned.
Expand All @@ -74,7 +74,7 @@ declare namespace accepts {
*/
langs(): string[];
langs(languages: string[]): string | string[] | false;
langs(...languages: string[]): string | string[] | false;
langs(...languages: string[]): string | false;

/**
* Return the first accepted type (and it is returned as the same text as what appears in the `types` array). If nothing in `types` is accepted, then `false` is returned.
Expand All @@ -83,9 +83,9 @@ declare namespace accepts {
* The `types` array can contain full MIME types or file extensions. Any value that is not a full MIME types is passed to `require('mime-types').lookup`.
*/
type(types: string[]): string | string[] | false;
type(...types: string[]): string | string[] | false;
type(...types: string[]): string | false;
types(types: string[]): string | string[] | false;
types(...types: string[]): string | string[] | false;
types(...types: string[]): string | false;
}
}

Expand Down

0 comments on commit f704193

Please sign in to comment.