summaryrefslogtreecommitdiffstats
path: root/test/cookie/global-headers.js
blob: 1d58dce1b6082113a172aff94672c79cb6d1aa24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict'

const { test, skip } = require('tap')
const {
  deleteCookie,
  getCookies,
  getSetCookies,
  setCookie
} = require('../..')
const { getHeadersList } = require('../../lib/cookies/util')

/* global Headers */

if (!globalThis.Headers) {
  skip('No global Headers to test')
  process.exit(0)
}

test('Using global Headers', (t) => {
  t.test('deleteCookies', (t) => {
    const headers = new Headers()

    t.equal(headers.get('set-cookie'), null)
    deleteCookie(headers, 'undici')
    t.equal(headers.get('set-cookie'), 'undici=; Expires=Thu, 01 Jan 1970 00:00:00 GMT')

    t.end()
  })

  t.test('getCookies', (t) => {
    const headers = new Headers({
      cookie: 'get=cookies; and=attributes'
    })

    t.same(getCookies(headers), { get: 'cookies', and: 'attributes' })
    t.end()
  })

  t.test('getSetCookies', (t) => {
    const headers = new Headers({
      'set-cookie': 'undici=getSetCookies; Secure'
    })

    const supportsCookies = getHeadersList(headers).cookies

    if (!supportsCookies) {
      t.same(getSetCookies(headers), [])
    } else {
      t.same(getSetCookies(headers), [
        {
          name: 'undici',
          value: 'getSetCookies',
          secure: true
        }
      ])
    }

    t.end()
  })

  t.test('setCookie', (t) => {
    const headers = new Headers()

    setCookie(headers, { name: 'undici', value: 'setCookie' })
    t.equal(headers.get('Set-Cookie'), 'undici=setCookie')
    t.end()
  })

  t.end()
})