/
test_basic_auth.js
60 lines (52 loc) · 1.49 KB
/
test_basic_auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict'
const { test } = require('tap')
const assertRejects = require('assert-rejects')
const nock = require('..')
const got = require('./got_client')
require('./cleanup_after_each')()
test('basic auth with username and password', async t => {
t.beforeEach(done => {
nock('http://example.test')
.get('/test')
.basicAuth({ user: 'foo', pass: 'bar' })
.reply(200, 'Here is the content')
done()
})
await t.test('succeeds when it matches', async tt => {
const response = await got('http://example.test/test', {
username: 'foo',
password: 'bar',
})
tt.equal(response.statusCode, 200)
tt.equal(response.body, 'Here is the content')
})
await t.test('fails when it doesnt match', async tt => {
await assertRejects(
got('http://example.test/test'),
/Nock: No match for request/
)
})
})
test('basic auth with username only', async t => {
t.beforeEach(done => {
nock('http://example.test')
.get('/test')
.basicAuth({ user: 'foo' })
.reply(200, 'Here is the content')
done()
})
await t.test('succeeds when it matches', async tt => {
const response = await got('http://example.test/test', {
username: 'foo',
password: '',
})
tt.equal(response.statusCode, 200)
tt.equal(response.body, 'Here is the content')
})
await t.test('fails when it doesnt match', async tt => {
await assertRejects(
got('http://example.test/test'),
/Nock: No match for request/
)
})
})