Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use headers from requests if any #364

Merged
merged 4 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/requestIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ RequestIterator.prototype.rebuildRequest = function () {
let data
this.resetted = false
if (this.currentRequest) {
this.currentRequest.headers = this.headers
this.currentRequest.headers = this.currentRequest.headers || this.headers
data = this.requestBuilder(this.currentRequest, this.context)
if (data) {
this.currentRequest.requestBuffer = this.reqDefaults.idReplacement
Expand Down
28 changes: 28 additions & 0 deletions test/httpClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,34 @@ test('client supports custom headers', (t) => {
})
})

test('client supports custom headers in requests', (t) => {
t.plan(2)

const opts = server.address()
opts.requests = [
{
path: '/',
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
foo: 'example'
})
}
]
const client = new Client(opts)

server.once('request', (req, res) => {
t.equal(req.headers['content-type'], 'application/json', 'custom header matches')
})

client.on('response', (statusCode, length) => {
t.equal(statusCode, 200, 'status code matches')
client.destroy()
})
})

test('client supports host custom header', (t) => {
t.plan(2)

Expand Down
29 changes: 29 additions & 0 deletions test/requestIterator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,35 @@ test('request iterator should create requests with overwritten defaults', (t) =>
'request is okay')
})

test('request iterator should use headers from requests', (t) => {
t.plan(2)

const opts = server.address()
opts.requests = [
{
path: '/',
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({ foo: 'bar' })
},
{
path: '/2',
method: 'POST',
headers: {
'content-type': 'text/html'
},
body: JSON.stringify({ foo: 'bar' })
}
]
const iterator = new RequestIterator(opts)

t.same(iterator.currentRequest.headers['content-type'], 'application/json')
iterator.nextRequest()
t.same(iterator.currentRequest.headers['content-type'], 'text/html')
})

test('request iterator should create requests with overwritten defaults', (t) => {
t.plan(3)

Expand Down