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

adds support for HTTP basic authentication #220

Merged
merged 2 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function Client (opts) {
this.timeout = (this.opts.timeout || 10) * 1000
this.ipc = !!this.opts.socketPath
this.secure = this.opts.protocol === 'https:'
this.auth = this.opts.auth || null

if (this.secure && this.opts.port === 80) {
this.opts.port = 443
Expand Down
5 changes: 5 additions & 0 deletions lib/httpRequestBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ function requestBuilder (defaults) {
host = hostname + ':' + port
}

if (reqData.auth) {
const encodedAuth = Buffer.from(reqData.auth).toString('base64')
mcollina marked this conversation as resolved.
Show resolved Hide resolved
headers.Authorization = `Basic ${encodedAuth}`
}

if (methods.indexOf(method) < 0) {
throw new Error(`${method} HTTP method is not supported`)
}
Expand Down
17 changes: 17 additions & 0 deletions test/httpClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,20 @@ test('client should have 2 different requests it iterates over', (t) => {
}
})
})

test('client supports http basic authentication', (t) => {
t.plan(2)

const opts = server.address()
opts.auth = 'username:password'
const client = new Client(opts)

server.once('request', (req, res) => {
t.equal(req.headers.authorization, 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=', 'authorization header matches')
})

client.on('response', (statusCode, length) => {
t.equal(statusCode, 200, 'status code matches')
client.destroy()
})
})
14 changes: 14 additions & 0 deletions test/httpRequestBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,17 @@ test('request builder should add a Content-Length header with value "[<contentLe
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 33\r\n\r\n[<id>]\r\n`),
'request is okay')
})

test('request builder should allow http basic authentication', (t) => {
t.plan(1)

const opts = server.address()
opts.auth = 'username:password'

const build = RequestBuilder(opts)

const result = build()
t.same(result,
Buffer.from(`GET / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nAuthorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\r\n\r\n`),
'request is okay')
})