test: fix clearAuthCache test (#20015)

This commit is contained in:
Jeremy Apthorp 2019-08-28 17:43:12 -07:00 committed by GitHub
parent 01fdb80f7c
commit 79e936aea8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -492,8 +492,8 @@ describe('session module', () => {
}) })
}) })
describe.skip('ses.clearAuthCache(options)', () => { describe('ses.clearAuthCache(options)', () => {
it('can clear http auth info from cache', (done) => { it('can clear http auth info from cache', async () => {
const ses = session.fromPartition('auth-cache') const ses = session.fromPartition('auth-cache')
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
const credentials = auth(req) const credentials = auth(req)
@ -505,42 +505,31 @@ describe('session module', () => {
res.end('authenticated') res.end('authenticated')
} }
}) })
server.listen(0, '127.0.0.1', () => { await new Promise(resolve => server.listen(0, '127.0.0.1', resolve))
const port = (server.address() as AddressInfo).port const port = (server.address() as AddressInfo).port
function issueLoginRequest (attempt = 1) { const fetch = (url: string) => new Promise((resolve, reject) => {
if (attempt > 2) { const request = net.request({ url, session: ses })
server.close() request.on('response', (response) => {
return done() let data = ''
} response.on('data', (chunk) => {
const request = net.request({ data += chunk
url: `http://127.0.0.1:${port}`,
session: ses
}) })
request.on('login', (info, callback) => { response.on('end', () => {
attempt += 1 resolve(data)
expect(info.scheme).to.equal('basic')
expect(info.realm).to.equal('Restricted')
callback('test', 'test')
}) })
request.on('response', (response) => { response.on('error', (error: any) => { reject(new Error(error)) })
let data = '' });
response.on('data', (chunk) => { request.end()
data += chunk
})
response.on('end', () => {
expect(data).to.equal('authenticated')
ses.clearAuthCache({ type: 'password' }).then(() => {
issueLoginRequest(attempt)
})
})
response.on('error', (error: any) => { done(error) })
});
// Internal api to bypass cache for testing.
(request as any).urlRequest._setLoadFlags(1 << 1)
request.end()
}
issueLoginRequest()
}) })
// the first time should throw due to unauthenticated
await expect(fetch(`http://127.0.0.1:${port}`)).to.eventually.be.rejected()
// passing the password should let us in
expect(await fetch(`http://test:test@127.0.0.1:${port}`)).to.equal('authenticated')
// subsequently, the credentials are cached
expect(await fetch(`http://127.0.0.1:${port}`)).to.equal('authenticated')
await ses.clearAuthCache({ type: 'password' })
// once the cache is cleared, we should get an error again
await expect(fetch(`http://127.0.0.1:${port}`)).to.eventually.be.rejected()
}) })
}) })