Skip to content

Commit

Permalink
refactor(test): Chai assert defaultReplyHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
mastermatt committed Mar 14, 2020
1 parent 263fe91 commit 9d98b63
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions tests/test_default_reply_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

// Tests of `.defaultReplyHeaders()`.

const { expect } = require('chai')
const { test } = require('tap')
const nock = require('..')
const got = require('./got_client')

require('./cleanup_after_each')()
require('./setup')

test('when no headers are specified on the request, default reply headers work', async t => {
test('when no headers are specified on the request, default reply headers work', async () => {
nock('http://example.test')
.defaultReplyHeaders({
'X-Powered-By': 'Meeee',
Expand All @@ -19,20 +21,20 @@ test('when no headers are specified on the request, default reply headers work',

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

t.deepEqual(headers, {
expect(headers).to.deep.equal({
'x-powered-by': 'Meeee',
'x-another-header': 'foo, bar',
})

t.deepEqual(rawHeaders, [
expect(rawHeaders).to.deep.equal([
'X-Powered-By',
'Meeee',
'X-Another-Header',
['foo', 'bar'],
])
})

test('default reply headers can be provided as a raw array', async t => {
test('default reply headers can be provided as a raw array', async () => {
const defaultHeaders = [
'X-Powered-By',
'Meeee',
Expand All @@ -46,15 +48,15 @@ test('default reply headers can be provided as a raw array', async t => {

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

t.deepEqual(headers, {
expect(headers).to.deep.equal({
'x-powered-by': 'Meeee',
'x-another-header': 'foo, bar',
})

t.deepEqual(rawHeaders, defaultHeaders)
expect(rawHeaders).to.deep.equal(defaultHeaders)
})

test('default reply headers can be provided as a Map', async t => {
test('default reply headers can be provided as a Map', async () => {
const defaultHeaders = new Map([
['X-Powered-By', 'Meeee'],
['X-Another-Header', ['foo', 'bar']],
Expand All @@ -66,20 +68,20 @@ test('default reply headers can be provided as a Map', async t => {

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

t.deepEqual(headers, {
expect(headers).to.deep.equal({
'x-powered-by': 'Meeee',
'x-another-header': 'foo, bar',
})

t.deepEqual(rawHeaders, [
expect(rawHeaders).to.deep.equal([
'X-Powered-By',
'Meeee',
'X-Another-Header',
['foo', 'bar'],
])
})

test('when headers are specified on the request, they override default reply headers', async t => {
test('when headers are specified on the request, they override default reply headers', async () => {
nock('http://example.test')
.defaultReplyHeaders({
'X-Powered-By': 'Meeee',
Expand All @@ -90,14 +92,14 @@ test('when headers are specified on the request, they override default reply hea

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

t.deepEqual(headers, {
expect(headers).to.deep.equal({
'x-powered-by': 'Meeee',
'x-another-header': 'Hey man!',
a: 'b',
})
})

test('default reply headers as functions work', async t => {
test('default reply headers as functions work', async () => {
const date = new Date().toUTCString()
const message = 'A message.'

Expand All @@ -112,14 +114,14 @@ test('default reply headers as functions work', async t => {

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

t.deepEqual(headers, {
'content-length': message.length,
expect(headers).to.deep.equal({
'content-length': message.length.toString(),
date,
foo: 'bar',
})
})

test('reply should not cause an error on header conflict', async t => {
test('reply should not cause an error on header conflict', async () => {
const scope = nock('http://example.test').defaultReplyHeaders({
'content-type': 'application/json',
})
Expand All @@ -130,12 +132,12 @@ test('reply should not cause an error on header conflict', async t => {

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

t.equal(statusCode, 200)
t.equal(headers['content-type'], 'application/xml')
t.equal(body, '<html></html>')
expect(statusCode).to.equal(200)
expect(headers['content-type']).to.equal('application/xml')
expect(body).to.equal('<html></html>')
})

test('direct reply headers override defaults when casing differs', async t => {
test('direct reply headers override defaults when casing differs', async () => {
const scope = nock('http://example.test')
.defaultReplyHeaders({
'X-Default-Only': 'default',
Expand All @@ -149,12 +151,12 @@ test('direct reply headers override defaults when casing differs', async t => {

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

t.deepEqual(headers, {
expect(headers).to.deep.equal({
'x-default-only': 'default',
'x-reply-only': 'from-reply',
'x-overridden': 'from-reply', // note this overrode the default value, despite the case difference
})
t.deepEqual(rawHeaders, [
expect(rawHeaders).to.deep.equal([
'X-Reply-Only',
'from-reply',
'x-overridden',
Expand All @@ -166,7 +168,7 @@ test('direct reply headers override defaults when casing differs', async t => {
scope.done()
})

test('dynamic reply headers override defaults when casing differs', async t => {
test('dynamic reply headers override defaults when casing differs', async () => {
const scope = nock('http://example.test')
.defaultReplyHeaders({
'X-Default-Only': 'default',
Expand All @@ -184,12 +186,12 @@ test('dynamic reply headers override defaults when casing differs', async t => {

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

t.deepEqual(headers, {
expect(headers).to.deep.equal({
'x-default-only': 'default',
'x-reply-only': 'from-reply',
'x-overridden': 'from-reply',
})
t.deepEqual(rawHeaders, [
expect(rawHeaders).to.deep.equal([
'X-Reply-Only',
'from-reply',
'x-overridden',
Expand Down

0 comments on commit 9d98b63

Please sign in to comment.