2022-05-03 15:10:13 +00:00
|
|
|
import * as httpm from '../lib'
|
|
|
|
import * as am from '../lib/auth'
|
|
|
|
|
|
|
|
describe('auth', () => {
|
|
|
|
beforeEach(() => {})
|
|
|
|
|
|
|
|
afterEach(() => {})
|
|
|
|
|
|
|
|
it('does basic http get request with basic auth', async () => {
|
|
|
|
const bh: am.BasicCredentialHandler = new am.BasicCredentialHandler(
|
|
|
|
'johndoe',
|
|
|
|
'password'
|
|
|
|
)
|
|
|
|
const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [
|
|
|
|
bh
|
|
|
|
])
|
|
|
|
const res: httpm.HttpClientResponse = await http.get(
|
2023-05-23 11:23:56 +00:00
|
|
|
'http://postman-echo.com/get'
|
2022-05-03 15:10:13 +00:00
|
|
|
)
|
|
|
|
expect(res.message.statusCode).toBe(200)
|
|
|
|
const body: string = await res.readBody()
|
|
|
|
const obj = JSON.parse(body)
|
2023-05-23 11:23:56 +00:00
|
|
|
const auth: string = obj.headers.authorization
|
2022-05-03 15:10:13 +00:00
|
|
|
const creds: string = Buffer.from(
|
|
|
|
auth.substring('Basic '.length),
|
|
|
|
'base64'
|
|
|
|
).toString()
|
|
|
|
expect(creds).toBe('johndoe:password')
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(obj.url).toBe('http://postman-echo.com/get')
|
2022-05-03 15:10:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does basic http get request with pat token auth', async () => {
|
|
|
|
const token = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs'
|
|
|
|
const ph: am.PersonalAccessTokenCredentialHandler = new am.PersonalAccessTokenCredentialHandler(
|
|
|
|
token
|
|
|
|
)
|
|
|
|
|
|
|
|
const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [
|
|
|
|
ph
|
|
|
|
])
|
|
|
|
const res: httpm.HttpClientResponse = await http.get(
|
2023-05-23 11:23:56 +00:00
|
|
|
'http://postman-echo.com/get'
|
2022-05-03 15:10:13 +00:00
|
|
|
)
|
|
|
|
expect(res.message.statusCode).toBe(200)
|
|
|
|
const body: string = await res.readBody()
|
|
|
|
const obj = JSON.parse(body)
|
2023-05-23 11:23:56 +00:00
|
|
|
const auth: string = obj.headers.authorization
|
2022-05-03 15:10:13 +00:00
|
|
|
const creds: string = Buffer.from(
|
|
|
|
auth.substring('Basic '.length),
|
|
|
|
'base64'
|
|
|
|
).toString()
|
|
|
|
expect(creds).toBe(`PAT:${token}`)
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(obj.url).toBe('http://postman-echo.com/get')
|
2022-05-03 15:10:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does basic http get request with pat token auth', async () => {
|
|
|
|
const token = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs'
|
|
|
|
const ph: am.BearerCredentialHandler = new am.BearerCredentialHandler(token)
|
|
|
|
|
|
|
|
const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [
|
|
|
|
ph
|
|
|
|
])
|
|
|
|
const res: httpm.HttpClientResponse = await http.get(
|
2023-05-23 11:23:56 +00:00
|
|
|
'http://postman-echo.com/get'
|
2022-05-03 15:10:13 +00:00
|
|
|
)
|
|
|
|
expect(res.message.statusCode).toBe(200)
|
|
|
|
const body: string = await res.readBody()
|
|
|
|
const obj = JSON.parse(body)
|
2023-05-23 11:23:56 +00:00
|
|
|
const auth: string = obj.headers.authorization
|
2022-05-03 15:10:13 +00:00
|
|
|
expect(auth).toBe(`Bearer ${token}`)
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(obj.url).toBe('http://postman-echo.com/get')
|
2022-05-03 15:10:13 +00:00
|
|
|
})
|
|
|
|
})
|