Skip to content

Commit

Permalink
fix: broken basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Sep 21, 2024
1 parent a992598 commit aca54da
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/log_pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ export class LogPusher {
headers: {
...this.#options.headers,
...(this.#options.basicAuth && {
Authorization: Buffer.from(
`${this.#options.basicAuth.username}:${this.#options.basicAuth.password}`,
).toString('base64'),
Authorization:
'Basic ' +
Buffer.from(
`${this.#options.basicAuth.username}:${this.#options.basicAuth.password}`,
).toString('base64'),
}),
'Content-Type': 'application/json',
},
Expand Down
15 changes: 13 additions & 2 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@ interface QueryRangeResponse<StreamType extends Record<string, string>> {
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class LokiClient {
static async getLogs(query: string) {
const url = new URL('loki/api/v1/query', process.env.LOKI_HOST!)
const url = new URL('loki/api/v1/query_range', process.env.LOKI_HOST!)
url.searchParams.append('query', query)
url.searchParams.append('limit', '10')

return fetch(url).then((response) => response.json() as Promise<QueryRangeResponse<any>>)
return fetch(url, {
headers: {
...(process.env.LOKI_USERNAME &&
process.env.LOKI_PASSWORD && {
Authorization:
'Basic ' +
Buffer.from(`${process.env.LOKI_USERNAME}:${process.env.LOKI_PASSWORD}`).toString(
'base64',
),
}),
},
}).then((response) => response.json() as Promise<QueryRangeResponse<any>>)
}
}
2 changes: 1 addition & 1 deletion tests/unit/log_pusher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test.group('LogPusher', (group) => {

await pusher.push({ level: 30 })

assert.equal(basicAuthHeader, Buffer.from('user:pass').toString('base64'))
assert.equal(basicAuthHeader, 'Basic ' + Buffer.from('user:pass').toString('base64'))
})

test('should not output error when silenceErrors is true', async ({ assert }) => {
Expand Down

0 comments on commit aca54da

Please sign in to comment.