Skip to content

Commit

Permalink
test: Test reply header arrays, args to header functions, and minor u…
Browse files Browse the repository at this point in the history
…pdates (#1541)

Related discussion at #1542.
  • Loading branch information
paulmelnikow committed May 6, 2019
1 parent d0dcf02 commit 6a26f41
Showing 1 changed file with 75 additions and 35 deletions.
110 changes: 75 additions & 35 deletions tests/test_reply_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Tests for header objects passed to `.reply()`, including header objects
// containing lambdas.

const { IncomingMessage } = require('http')
const { test } = require('tap')
const mikealRequest = require('request')
const lolex = require('lolex')
Expand All @@ -11,68 +12,119 @@ const got = require('./got_client')

require('./cleanup_after_each')()

test('headers work', async t => {
const scope = nock('http://example.com')
test('reply header is sent in the mock response', async t => {
const scope = nock('http://example.test')
.get('/')
.reply(200, 'Hello World!', { 'X-My-Headers': 'My Header value' })

const { headers } = await got('http://example.com/')
const { headers } = await got('http://example.test/')

t.equivalent(headers, { 'x-my-headers': 'My Header value' })
scope.done()
})

test('reply headers as function work', async t => {
const scope = nock('http://example.com')
test('content-length header is sent with response', async t => {
const scope = nock('http://example.test')
.replyContentLength()
.get('/')
.reply(200, { hello: 'world' })

const { headers } = await got('http://example.test/')

t.equal(headers['content-length'], 17)
scope.done()
})

test('header array sends multiple reply headers', async t => {
const scope = nock('http://example.test')
.get('/')
.reply(200, 'Hello World!', {
'Set-Cookie': ['cookie1=foo', 'cookie2=bar'],
})

const { headers, rawHeaders } = await got('http://example.test/')
t.equivalent(headers, {
'set-cookie': ['cookie1=foo', 'cookie2=bar'],
})
t.equivalent(rawHeaders, ['Set-Cookie', ['cookie1=foo', 'cookie2=bar']])

scope.done()
})

test('reply header function is evaluated and the result sent in the mock response', async t => {
const scope = nock('http://example.test')
.get('/')
.reply(200, 'boo!', {
'X-My-Headers': (req, res, body) => body.toString(),
'X-My-Headers': () => 'yo!',
})

const { headers, rawHeaders } = await got('http://example.test/')

t.equivalent(headers, { 'x-my-headers': 'yo!' })
t.equivalent(rawHeaders, ['X-My-Headers', 'yo!'])
scope.done()
})

test('reply header function receives the correct arguments', async t => {
t.plan(4)

const { ClientRequest: OverriddenClientRequest } = require('http')
const scope = nock('http://example.test')
.post('/')
.reply(200, 'boo!', {
'X-My-Headers': (req, res, body) => {
t.type(req, OverriddenClientRequest)
t.type(res, IncomingMessage)
// TODO The current behavior is to pass the response body as a buffer.
// This doesn't seem at all helpful and seems like it is probably a
// bug.
// https://github.com/nock/nock/issues/1542
t.type(body, Buffer)
t.true(Buffer.from('boo!').equals(body))
return 'gotcha'
},
})

const { headers, rawHeaders } = await got('http://example.com/')
await got.post('http://example.test/')

t.equivalent(headers, { 'x-my-headers': 'boo!' })
t.equivalent(rawHeaders, ['X-My-Headers', 'boo!'])
scope.done()
})

test('reply headers as function are evaluated only once per request', async t => {
test('reply headers function is evaluated exactly once', async t => {
let counter = 0
const scope = nock('http://example.com')
const scope = nock('http://example.test')
.get('/')
.reply(200, 'boo!', {
'X-My-Headers': (req, res, body) => {
'X-My-Headers': () => {
++counter
return body.toString()
return 'heya'
},
})

const { headers, rawHeaders } = await got('http://example.com/')
await got('http://example.test/')

t.equivalent(headers, { 'x-my-headers': 'boo!' })
t.equivalent(rawHeaders, ['X-My-Headers', 'boo!'])
scope.done()

t.equal(counter, 1)
})

test('reply headers as function are evaluated on each request', async t => {
test('reply header function are re-evaluated for every matching request', async t => {
let counter = 0
const scope = nock('http://example.com')
const scope = nock('http://example.test')
.get('/')
.times(2)
.reply(200, 'boo!', {
'X-My-Headers': (req, res, body) => `${++counter}`,
'X-My-Headers': () => `${++counter}`,
})

const { headers, rawHeaders } = await got('http://example.com/')
const { headers, rawHeaders } = await got('http://example.test/')
t.equivalent(headers, { 'x-my-headers': '1' })
t.equivalent(rawHeaders, ['X-My-Headers', '1'])

t.equal(counter, 1)

const { headers: headers2, rawHeaders: rawHeaders2 } = await got(
'http://example.com/'
'http://example.test/'
)
t.equivalent(headers2, { 'x-my-headers': '2' })
t.equivalent(rawHeaders2, ['X-My-Headers', '2'])
Expand All @@ -82,19 +134,7 @@ test('reply headers as function are evaluated on each request', async t => {
scope.done()
})

test('reply with content-length header', async t => {
const scope = nock('http://example.test')
.replyContentLength()
.get('/')
.reply(200, { hello: 'world' })

const { headers } = await got('http://example.test/')

t.equal(headers['content-length'], 17)
scope.done()
})

test('reply with explicit date header', async t => {
test('replyDate() sends explicit date header with response', async t => {
const date = new Date()

const scope = nock('http://example.test')
Expand All @@ -110,7 +150,7 @@ test('reply with explicit date header', async t => {

// async / got version is returning "not ok test unfinished".
// https://github.com/nock/nock/issues/1305#issuecomment-451701657
test('reply with implicit date header', t => {
test('replyDate() sends date header with response', t => {
const clock = lolex.install()
const date = new Date()

Expand Down

0 comments on commit 6a26f41

Please sign in to comment.