Skip to content

Commit

Permalink
test: fix keepalive assert check (#535)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced a new endpoint `/digestAuth2` for enhanced security through
Digest Authentication.
  
- **Bug Fixes**
- Improved error handling in tests to prevent runtime errors related to
missing dispatcher pool stats.

- **Tests**
- Added a test case to verify server response for invalid username and
password in digest authentication.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 committed Sep 14, 2024
1 parent 77eee5b commit 1a67bf6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
21 changes: 21 additions & 0 deletions test/fixtures/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ export async function startServer(options?: {
}));
}

if (pathname === '/digestAuth2') {
const authorization = req.headers.authorization;
if (!authorization) {
res.setHeader('x-www-authenticate', 'Digest realm="testrealm@urllib.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"');
res.statusCode = 401;
return res.end(JSON.stringify({
error: 'authorization required',
}));
}
if (!authorization.includes('Digest username="user"')) {
res.setHeader('x-www-authenticate', 'Digest realm="testrealm@urllib.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"');
res.statusCode = 401;
return res.end(JSON.stringify({
error: 'authorization invaild',
}));
}
return res.end(JSON.stringify({
authorization,
}));
}

if (pathname === '/digestAuth/multi') {
const authorization = req.headers.authorization;
if (!authorization) {
Expand Down
16 changes: 10 additions & 6 deletions test/keep-alive-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ describe('keep-alive-header.test.ts', () => {
}
let response = await task;
// console.log('after response stats: %o', httpClient.getDispatcherPoolStats());
assert.equal(httpClient.getDispatcherPoolStats()[origin].pending, 0);
// assert.equal(httpClient.getDispatcherPoolStats()[origin].connected, 1);
assert.equal(httpClient.getDispatcherPoolStats()[origin].connected, 0);
if (httpClient.getDispatcherPoolStats()[origin]) {
assert.equal(httpClient.getDispatcherPoolStats()[origin].pending, 0);
// assert.equal(httpClient.getDispatcherPoolStats()[origin].connected, 1);
assert.equal(httpClient.getDispatcherPoolStats()[origin].connected, 0);
}
// console.log(response.res.socket);
assert.equal(response.status, 200);
// console.log(response.headers);
Expand Down Expand Up @@ -134,9 +136,11 @@ describe('keep-alive-header.test.ts', () => {
// console.log('before sleep stats: %o', httpClient.getDispatcherPoolStats());
// { connected: 2, free: 1, pending: 0, queued: 0, running: 0, size: 0 }
// assert.equal(httpClient.getDispatcherPoolStats()[origin].connected, 2);
assert.equal(httpClient.getDispatcherPoolStats()[origin].connected, 0);
// assert.equal(httpClient.getDispatcherPoolStats()[origin].free, 1);
assert.equal(httpClient.getDispatcherPoolStats()[origin].free, 0);
if (httpClient.getDispatcherPoolStats()[origin]) {
assert.equal(httpClient.getDispatcherPoolStats()[origin].connected, 0);
// assert.equal(httpClient.getDispatcherPoolStats()[origin].free, 1);
assert.equal(httpClient.getDispatcherPoolStats()[origin].free, 0);
}
await sleep(keepAliveTimeout);
// console.log('after sleep stats: %o', httpClient.getDispatcherPoolStats());
// clients maybe all gone => after sleep stats: {}
Expand Down
11 changes: 11 additions & 0 deletions test/options.digestAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ describe('options.digestAuth.test.ts', () => {
});
});

it('should auth fail on x-www-authenticate', async () => {
const response = await urllib.request(`${_url}digestAuth2`, {
digestAuth: 'invailduser:pwd',
dataType: 'json',
});
assert.equal(response.status, 401);
assert.deepEqual(response.data, {
error: 'authorization invaild',
});
});

it('should digest auth required', async () => {
const response = await urllib.request(`${_url}digestAuth?t=123123`, {
dataType: 'json',
Expand Down

0 comments on commit 1a67bf6

Please sign in to comment.