summaryrefslogtreecommitdiffstats
path: root/test/node-fetch/mock.js
blob: a53f464a1a91ba33fb0f4dabda1771abbf203080 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* eslint no-unused-expressions: "off" */

// Test tools
const chai = require('chai')

const {
  fetch,
  MockAgent,
  setGlobalDispatcher,
  Headers
} = require('../../index.js')

const { expect } = chai

describe('node-fetch with MockAgent', () => {
  it('should match the url', async () => {
    const mockAgent = new MockAgent()
    setGlobalDispatcher(mockAgent)
    const mockPool = mockAgent.get('http://localhost:3000')

    mockPool
      .intercept({
        path: '/test',
        method: 'GET'
      })
      .reply(200, { success: true })
      .persist()

    const res = await fetch('http://localhost:3000/test', {
      method: 'GET'
    })

    expect(res.status).to.equal(200)
    expect(await res.json()).to.deep.equal({ success: true })
  })

  it('should match the body', async () => {
    const mockAgent = new MockAgent()
    setGlobalDispatcher(mockAgent)
    const mockPool = mockAgent.get('http://localhost:3000')

    mockPool
      .intercept({
        path: '/test',
        method: 'POST',
        body: (value) => {
          return value === 'request body'
        }
      })
      .reply(200, { success: true })
      .persist()

    const res = await fetch('http://localhost:3000/test', {
      method: 'POST',
      body: 'request body'
    })

    expect(res.status).to.equal(200)
    expect(await res.json()).to.deep.equal({ success: true })
  })

  it('should match the headers', async () => {
    const mockAgent = new MockAgent()
    setGlobalDispatcher(mockAgent)
    const mockPool = mockAgent.get('http://localhost:3000')

    mockPool
      .intercept({
        path: '/test',
        method: 'GET',
        headers: (h) => {
          return h['user-agent'] === 'undici'
        }
      })
      .reply(200, { success: true })
      .persist()

    const res = await fetch('http://localhost:3000/test', {
      method: 'GET',
      headers: new Headers({ 'User-Agent': 'undici' })
    })
    expect(res.status).to.equal(200)
    expect(await res.json()).to.deep.equal({ success: true })
  })

  it('should match the headers with a matching function', async () => {
    const mockAgent = new MockAgent()
    setGlobalDispatcher(mockAgent)
    const mockPool = mockAgent.get('http://localhost:3000')

    mockPool
      .intercept({
        path: '/test',
        method: 'GET',
        headers (headers) {
          expect(headers).to.be.an('object')
          expect(headers).to.have.property('user-agent', 'undici')
          return true
        }
      })
      .reply(200, { success: true })
      .persist()

    const res = await fetch('http://localhost:3000/test', {
      method: 'GET',
      headers: new Headers({ 'User-Agent': 'undici' })
    })

    expect(res.status).to.equal(200)
    expect(await res.json()).to.deep.equal({ success: true })
  })
})