Skip to content

Commit

Permalink
feat: implements "getSetCookie" method on Headers (#57)
Browse files Browse the repository at this point in the history
* implements getSetCookie method on headers

* cut a few bytes

* comment

* correct return of empty values
  • Loading branch information
mattcosta7 committed Aug 26, 2023
1 parent df975e9 commit cff8faf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
39 changes: 31 additions & 8 deletions src/Headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('[Symbol.iterator]', () => {
'content-type': 'application/json',
})

const entries = []
const entries: Array<[string, string]> = []

for (const entry of headers) {
entries.push(entry)
Expand All @@ -74,7 +74,7 @@ describe('[Symbol.iterator]', () => {

it('returns an empty iterator when there is no headers', () => {
const headers = new Headers()
const entries = []
const entries: Array<[string, string]> = []

for (const entry of headers) {
entries.push(entry)
Expand All @@ -91,7 +91,7 @@ describe('.keys()', () => {
'accept-language': 'en-US',
'content-type': 'application/json',
})
const keys = []
const keys: Array<string> = []

for (const name of headers.keys()) {
keys.push(name)
Expand All @@ -102,7 +102,7 @@ describe('.keys()', () => {

it('returns an empty iterator when there is no headers', () => {
const headers = new Headers()
const keys = []
const keys: Array<string> = []

for (const name of headers.keys()) {
keys.push(name)
Expand All @@ -119,7 +119,7 @@ describe('.values()', () => {
'accept-language': 'en-US',
'content-type': 'application/json',
})
const values = []
const values: Array<string> = []

for (const value of headers.values()) {
values.push(value)
Expand All @@ -130,7 +130,7 @@ describe('.values()', () => {

it('returns an empty iterator when there is no headers', () => {
const headers = new Headers()
const values = []
const values: Array<string> = []

for (const value of headers.values()) {
values.push(value)
Expand All @@ -147,7 +147,7 @@ describe('.entries()', () => {
'accept-language': 'en-US',
'content-type': 'application/json',
})
const entries = []
const entries: Array<[string, string]> = []

for (const entry of headers.entries()) {
entries.push(entry)
Expand All @@ -162,7 +162,7 @@ describe('.entries()', () => {

it('returns an empty iterator when there is no headers', () => {
const headers = new Headers()
const entries = []
const entries: Array<[string, string]> = []

for (const entry of headers.entries()) {
entries.push(entry)
Expand All @@ -186,6 +186,11 @@ describe('.get()', () => {
expect(headers.get('Accept')).toBeNull()
expect(headers.get('AcCePt')).toBeNull()
})

it('return an empty string for an empty header value', () => {
const headers = new Headers({ 'Content-Type': '' })
expect(headers.get('Content-Type')).toEqual('')
})
})

describe('.all()', () => {
Expand Down Expand Up @@ -330,3 +335,21 @@ describe('.forEach()', () => {
expect(headerSet).toEqual(new Set(['accept', 'user-agent']))
})
})

describe('.getSetCookie()', () => {
it('returns the value of the existing Set-Cookie header', () => {
const headers = new Headers({ 'Set-Cookie': '' })
expect(headers.getSetCookie()).toEqual([''])
headers.append('Set-Cookie', 'a')
expect(headers.getSetCookie()).toEqual(['', 'a'])
headers.append('Set-Cookie', 'b')
expect(headers.getSetCookie()).toEqual(['', 'a', 'b'])
headers.append('Set-Cookie', 'c')
expect(headers.getSetCookie()).toEqual(['', 'a', 'b', 'c'])
})

it('returns [] given a non-existing Set-Cookie header', () => {
const headers = new Headers()
expect(headers.getSetCookie()).toEqual([])
})
})
24 changes: 21 additions & 3 deletions src/Headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { normalizeHeaderValue } from './utils/normalizeHeaderValue'

const NORMALIZED_HEADERS: unique symbol = Symbol('normalizedHeaders')
const RAW_HEADER_NAMES: unique symbol = Symbol('rawHeaderNames')
const HEADER_VALUE_DELIMITER = ', ' as const

export default class HeadersPolyfill {
// Normalized header {"name":"a, b"} storage.
Expand All @@ -28,12 +29,18 @@ export default class HeadersPolyfill {
}, this)
} else if (Array.isArray(init)) {
init.forEach(([name, value]) => {
this.append(name, Array.isArray(value) ? value.join(', ') : value)
this.append(
name,
Array.isArray(value) ? value.join(HEADER_VALUE_DELIMITER) : value
)
})
} else if (init) {
Object.getOwnPropertyNames(init).forEach((name) => {
const value = init[name]
this.append(name, Array.isArray(value) ? value.join(', ') : value)
this.append(
name,
Array.isArray(value) ? value.join(HEADER_VALUE_DELIMITER) : value
)
})
}
}
Expand Down Expand Up @@ -64,7 +71,7 @@ export default class HeadersPolyfill {
* Returns a `ByteString` sequence of all the values of a header with a given name.
*/
get(name: string): string | null {
return this[NORMALIZED_HEADERS][normalizeHeaderName(name)] || null
return this[NORMALIZED_HEADERS][normalizeHeaderName(name)] ?? null
}

/**
Expand Down Expand Up @@ -147,4 +154,15 @@ export default class HeadersPolyfill {
}
}
}

/**
* Returns an array containing the values
* of all Set-Cookie headers associated
* with a response
*/
getSetCookie(): string[] {
const setCookieHeader = this.get('set-cookie')
if (setCookieHeader === null) return []
return setCookieHeader.split(HEADER_VALUE_DELIMITER)
}
}

0 comments on commit cff8faf

Please sign in to comment.