Skip to content
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
39 changes: 13 additions & 26 deletions lib/__tests__/cookieJar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import {
afterAll,
assert,
beforeAll,
beforeEach,
describe,
Expand Down Expand Up @@ -74,9 +75,7 @@ describe('CookieJar', () => {
'foo=bar',
'http://example.com',
(_error, result) => {
if (result == null) {
throw new Error('Result should not have been undefined')
}
expect(result).toBeDefined()
cookie = result
done()
},
Expand All @@ -90,9 +89,7 @@ describe('CookieJar', () => {
'foo=bar',
'http://example.com',
)
if (result == null) {
throw new Error('Result should not have been undefined')
}
expect(result).toBeDefined()
cookie = result
},
},
Expand Down Expand Up @@ -173,9 +170,8 @@ describe('CookieJar', () => {
})

it('should set a timestamp when storing or retrieving a cookie', async () => {
// We know that we're passing a valid cookie, so we can use the non-null assertion
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
cookie = Cookie.parse('a=b; Domain=example.com; Path=/')!
cookie = Cookie.parse('a=b; Domain=example.com; Path=/')
assert.exists(cookie)
const t0 = new Date()

expect(cookie).toEqual(
Expand Down Expand Up @@ -785,12 +781,9 @@ describe('CookieJar', () => {
{
callbackStyle(done) {
cookieJar.getCookieString('http://example.com', (_err, result) => {
if (typeof result === 'string') {
cookieString = result
done()
} else {
throw new Error('Result should not have been undefined')
}
assert.exists(result)
cookieString = result
done()
})
},
async asyncStyle() {
Expand Down Expand Up @@ -885,9 +878,7 @@ describe('CookieJar', () => {
cookieJar.getSetCookieStrings(
'http://example.com',
(_error, result) => {
if (!result) {
throw new Error('Result should not have been undefined')
}
expect(result).toBeDefined()
cookieHeaders = result
done()
},
Expand Down Expand Up @@ -1019,9 +1010,7 @@ describe('CookieJar', () => {
{
callbackStyle(done) {
cookieJar.serialize((_error, result) => {
if (!result) {
throw new Error()
}
assert.exists(result)
data = result
done()
})
Expand Down Expand Up @@ -1329,8 +1318,8 @@ describe('setCookie errors', () => {
'foo=bar; Domain=127.0.0.1; Path=/',
'http://127.0.0.1/',
)
expect(cookie).toBeDefined()
expect(cookie?.domain).toBe('127.0.0.1')
assert.exists(cookie)
expect(cookie.domain).toBe('127.0.0.1')
const cookies = await cookieJar.getCookies('http://127.0.0.1/')
expect(cookies).toHaveLength(1)
expect(cookies[0]?.key).toBe('foo')
Expand Down Expand Up @@ -1816,9 +1805,7 @@ function createCookie(
} = {},
): Cookie {
const cookie = Cookie.parse(cookieString)
if (!cookie) {
throw new Error('This should not be undefined')
}
assert.exists(cookie)
if (options.hostOnly) {
cookie.hostOnly = options.hostOnly
}
Expand Down
11 changes: 4 additions & 7 deletions lib/__tests__/cookieToAndFromJson.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { afterAll, assert, beforeAll, describe, expect, it, vi } from 'vitest'
import { Cookie } from '../cookie/cookie.js'

beforeAll(() => vi.useFakeTimers())
Expand All @@ -9,9 +9,8 @@ describe('Cookie.toJSON()', () => {
const cookie = Cookie.parse(
'alpha=beta; Domain=example.com; Path=/foo; Expires=Tue, 19 Jan 2038 03:14:07 GMT; HttpOnly',
)
expect(cookie).toBeInstanceOf(Cookie)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(cookie!.toJSON()).toEqual({
assert.instanceOf(cookie, Cookie)
expect(cookie.toJSON()).toEqual({
// Sometimes we roll over a millisecond, so we need to check both

creation: new Date().toISOString(),
Expand Down Expand Up @@ -62,9 +61,7 @@ describe('Cookie.fromJSON()', () => {
lastAccessed: 'Infinity',
})
const cookie = Cookie.fromJSON(json)
if (!cookie) {
throw new Error('This should not be null')
}
assert.exists(cookie)
expect(cookie.expires).toBe('Infinity')
expect(cookie.creation).toBe('Infinity')
expect(cookie.lastAccessed).toBe('Infinity')
Expand Down
6 changes: 2 additions & 4 deletions lib/__tests__/cookieToString.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { describe, expect, it } from 'vitest'
import { assert, describe, expect, it } from 'vitest'
import { Cookie } from '../cookie/cookie.js'

describe('Cookie.toString()', () => {
const parse = (cookieString: string): Cookie => {
const cookie = Cookie.parse(cookieString)
if (!cookie) {
throw new Error('This should have parsed')
}
assert.exists(cookie)
return cookie
}

Expand Down
6 changes: 2 additions & 4 deletions lib/__tests__/ietf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

import { describe, expect, it } from 'vitest'
import { assert, describe, expect, it } from 'vitest'
import url from 'url'
import parserData from './data/parser.js'
import bsdExampleDates from './data/dates/bsd-examples.js'
Expand Down Expand Up @@ -60,9 +60,7 @@ describe('IETF http state tests', () => {
expect(actual.length).toBe(expected.length)
actual.forEach((actualCookie, idx) => {
const expectedCookie = expected[idx]
if (!expectedCookie) {
throw new Error('This should not be undefined')
}
assert.exists(expectedCookie)
expect(actualCookie.key).toBe(expectedCookie.name)
expect(actualCookie.value).toBe(expectedCookie.value)
})
Expand Down
34 changes: 9 additions & 25 deletions lib/__tests__/removeAll.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it, vi } from 'vitest'
import { assert, describe, expect, it, vi } from 'vitest'
import type { Cookie } from '../cookie/cookie.js'
import { CookieJar } from '../cookie/cookieJar.js'
import { MemoryCookieStore } from '../memstore.js'
Expand Down Expand Up @@ -142,9 +142,7 @@ class StoreWithoutRemoveAll extends Store {
_key: string,
callback?: Callback<Cookie | undefined>,
): unknown {
if (!callback) {
throw new Error('This should not be undefined')
}
assert.exists(callback)
callback(null, undefined)
return
}
Expand All @@ -166,9 +164,7 @@ class StoreWithoutRemoveAll extends Store {
_allowSpecialUseDomain: boolean,
callback?: Callback<Cookie[]>,
): unknown {
if (!callback) {
throw new Error('This should not be undefined')
}
assert.exists(callback)
callback(null, [])
return
}
Expand All @@ -178,9 +174,7 @@ class StoreWithoutRemoveAll extends Store {
override putCookie(cookie: Cookie, callback?: ErrorCallback): unknown {
this.stats.put++
this.cookies.push(cookie)
if (!callback) {
throw new Error('This should not be undefined')
}
assert.exists(callback)
callback(null)
return
}
Expand All @@ -189,9 +183,7 @@ class StoreWithoutRemoveAll extends Store {
override getAllCookies(callback: Callback<Cookie[]>): void
override getAllCookies(callback?: Callback<Cookie[]>): unknown {
this.stats.getAll++
if (!callback) {
throw new Error('This should not be undefined')
}
assert.exists(callback)
callback(null, this.cookies.slice())
return
}
Expand All @@ -214,9 +206,7 @@ class StoreWithoutRemoveAll extends Store {
callback?: ErrorCallback,
): unknown {
this.stats.remove++
if (!callback) {
throw new Error('This should not be undefined')
}
assert.exists(callback)
callback(null)
return
}
Expand All @@ -238,9 +228,7 @@ class MemoryStoreExtension extends MemoryCookieStore {
override getAllCookies(callback: Callback<Cookie[]>): void
override getAllCookies(callback?: Callback<Cookie[]>): unknown {
this.stats.getAll++
if (!callback) {
throw new Error('This should not be undefined')
}
assert.exists(callback)
super.getAllCookies(callback)
return
}
Expand All @@ -263,9 +251,7 @@ class MemoryStoreExtension extends MemoryCookieStore {
callback?: ErrorCallback,
): unknown {
this.stats.remove++
if (!callback) {
throw new Error('This should not be undefined')
}
assert.exists(callback)
super.removeCookie(domain, path, key, callback)
return
}
Expand All @@ -274,9 +260,7 @@ class MemoryStoreExtension extends MemoryCookieStore {
override removeAllCookies(callback: ErrorCallback): void
override removeAllCookies(callback?: ErrorCallback): unknown {
this.stats.removeAll++
if (!callback) {
throw new Error('This should not be undefined')
}
assert.exists(callback)
super.removeAllCookies(callback)
return
}
Expand Down
6 changes: 2 additions & 4 deletions lib/__tests__/sameSite.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from 'vitest'
import { assert, beforeEach, describe, expect, it } from 'vitest'
import { Cookie } from '../cookie/cookie.js'
import { CookieJar } from '../cookie/cookieJar.js'

Expand All @@ -13,9 +13,7 @@ describe('Same-Site Cookies', function () {

const parse = (cookieString: string): Cookie => {
const result = Cookie.parse(cookieString)
if (!result) {
throw new Error('This should not be undefined')
}
assert.exists(result)
return result
}

Expand Down
Loading