Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mikicho committed Feb 3, 2024
1 parent ab8dd93 commit dc2dd81
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 94 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/continuous-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 20
cache: 'npm'
- name: Install dependencies
run: npm ci --ignore-scripts --no-audit --no-progress --prefer-offline
Expand Down Expand Up @@ -87,11 +87,8 @@ jobs:
fail-fast: false
matrix:
node-version:
- 10
- 12
- 14
- 16
- 18
- 20
os:
- macos-latest
- ubuntu-latest
Expand Down
38 changes: 18 additions & 20 deletions lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,27 +437,25 @@ function activate() {
}
})

if (global.fetch) {
const originalFetch = global.fetch
global.fetch = async (input, init = undefined) => {
const request = new Request(input, init)
const options = common.convertFetchRequestToClientRequest(request)
options.isFetchRequest = true
const body = await request.arrayBuffer()
const clientRequest = new http.ClientRequest(options)

// If mock found
if (clientRequest.interceptors) {
return new Promise((resolve, reject) => {
clientRequest.on('response', response => {
resolve(createResponse(response))
})
clientRequest.on('error', reject)
clientRequest.end(body)
const originalFetch = global.fetch
global.fetch = async (input, init = undefined) => {
const request = new Request(input, init)
const options = common.convertFetchRequestToClientRequest(request)
options.isFetchRequest = true
const body = await request.arrayBuffer()
const clientRequest = new http.ClientRequest(options)

// If mock found
if (clientRequest.interceptors) {
return new Promise((resolve, reject) => {
clientRequest.on('response', response => {
resolve(createResponse(response))
})
} else {
return originalFetch(input, init)
}
clientRequest.on('error', reject)
clientRequest.end(body)
})
} else {
return originalFetch(input, init)
}
}

Expand Down
136 changes: 67 additions & 69 deletions tests/test_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,89 @@ const nock = require('..')
const assertRejects = require('assert-rejects')
const { startHttpServer } = require('./servers')

if (global.fetch) {
describe('Native Fetch', () => {
it('input is string', async () => {
const scope = nock('http://example.test').get('/').reply()

const { status } = await fetch('http://example.test/')
expect(status).to.equal(200)
scope.done()
})
describe('Native Fetch', () => {
it('input is string', async () => {
const scope = nock('http://example.test').get('/').reply()

it('input is URL', async () => {
const scope = nock('http://example.test').get('/').reply()
const { status } = await fetch('http://example.test/')
expect(status).to.equal(200)
scope.done()
})

const { status } = await fetch(new URL('http://example.test/'))
expect(status).to.equal(200)
scope.done()
})
it('input is URL', async () => {
const scope = nock('http://example.test').get('/').reply()

const { status } = await fetch(new URL('http://example.test/'))
expect(status).to.equal(200)
scope.done()
})

it('input is Request object', async () => {
const scope = nock('http://example.test').get('/').reply()
it('input is Request object', async () => {
const scope = nock('http://example.test').get('/').reply()

const { status } = await fetch(new Request('http://example.test/'))
expect(status).to.equal(200)
scope.done()
const { status } = await fetch(new Request('http://example.test/'))
expect(status).to.equal(200)
scope.done()
})

it('filter by body', async () => {
const scope = nock('http://example.test')
.post('/', { test: 'fetch' })
.reply()

const { status } = await fetch('http://example.test/', {
method: 'POST',
body: JSON.stringify({ test: 'fetch' }),
})
expect(status).to.equal(200)
scope.done()
})

it('filter by body', async () => {
const scope = nock('http://example.test')
.post('/', { test: 'fetch' })
.reply()
it('filter by request body', async () => {
const scope = nock('http://example.test')
.post('/', { test: 'fetch' })
.reply()

const { status } = await fetch('http://example.test/', {
const { status } = await fetch(
new Request('http://example.test/', {
method: 'POST',
body: JSON.stringify({ test: 'fetch' }),
})
expect(status).to.equal(200)
scope.done()
})
}),
)
expect(status).to.equal(200)
scope.done()
})

it('filter by request body', async () => {
const scope = nock('http://example.test')
.post('/', { test: 'fetch' })
.reply()

const { status } = await fetch(
new Request('http://example.test/', {
method: 'POST',
body: JSON.stringify({ test: 'fetch' }),
}),
)
expect(status).to.equal(200)
scope.done()
})
it('no match', async () => {
nock('http://example.test').get('/').reply()

it('no match', async () => {
nock('http://example.test').get('/').reply()
await assertRejects(
fetch('http://example.test/wrong-path'),
/Nock: No match for request/,
)
})

await assertRejects(
fetch('http://example.test/wrong-path'),
/Nock: No match for request/,
)
it('forward request if no mock', async () => {
const { origin } = await startHttpServer((request, response) => {
response.write('live')
response.end()
})

it('forward request if no mock', async () => {
const { origin } = await startHttpServer((request, response) => {
response.write('live')
response.end()
})

const { status } = await fetch(origin)
expect(status).to.equal(200)
})
const { status } = await fetch(origin)
expect(status).to.equal(200)
})

it('should work with empty response', async () => {
nock('http://example.test').get('/').reply(204)
it('should work with empty response', async () => {
nock('http://example.test').get('/').reply(204)

const { status } = await fetch('http://example.test')
expect(status).to.equal(204)
})
const { status } = await fetch('http://example.test')
expect(status).to.equal(204)
})

it('should work https', async () => {
nock('https://example.test').get('/').reply()
it('should work https', async () => {
nock('https://example.test').get('/').reply()

const { status } = await fetch('https://example.test')
expect(status).to.equal(200)
})
const { status } = await fetch('https://example.test')
expect(status).to.equal(200)
})
}
})

0 comments on commit dc2dd81

Please sign in to comment.