summaryrefslogtreecommitdiffstats
path: root/test/fetch/cookies.js
blob: 18b001d864477241209a5a543ce257e3c48bd7ae (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
'use strict'

const { once } = require('events')
const { createServer } = require('http')
const { test } = require('tap')
const { fetch, Headers } = require('../..')

test('Can receive set-cookie headers from a server using fetch - issue #1262', async (t) => {
  const server = createServer((req, res) => {
    res.setHeader('set-cookie', 'name=value; Domain=example.com')
    res.end()
  }).listen(0)

  t.teardown(server.close.bind(server))
  await once(server, 'listening')

  const response = await fetch(`http://localhost:${server.address().port}`)

  t.equal(response.headers.get('set-cookie'), 'name=value; Domain=example.com')

  const response2 = await fetch(`http://localhost:${server.address().port}`, {
    credentials: 'include'
  })

  t.equal(response2.headers.get('set-cookie'), 'name=value; Domain=example.com')

  t.end()
})

test('Can send cookies to a server with fetch - issue #1463', async (t) => {
  const server = createServer((req, res) => {
    t.equal(req.headers.cookie, 'value')
    res.end()
  }).listen(0)

  t.teardown(server.close.bind(server))
  await once(server, 'listening')

  const headersInit = [
    new Headers([['cookie', 'value']]),
    { cookie: 'value' },
    [['cookie', 'value']]
  ]

  for (const headers of headersInit) {
    await fetch(`http://localhost:${server.address().port}`, { headers })
  }

  t.end()
})

test('Cookie header is delimited with a semicolon rather than a comma - issue #1905', async (t) => {
  t.plan(1)

  const server = createServer((req, res) => {
    t.equal(req.headers.cookie, 'FOO=lorem-ipsum-dolor-sit-amet; BAR=the-quick-brown-fox')
    res.end()
  }).listen(0)

  t.teardown(server.close.bind(server))
  await once(server, 'listening')

  await fetch(`http://localhost:${server.address().port}`, {
    headers: [
      ['cookie', 'FOO=lorem-ipsum-dolor-sit-amet'],
      ['cookie', 'BAR=the-quick-brown-fox']
    ]
  })
})