From 1a67bf6f9db949bcc4cd9c4c241f3c5a076b0f05 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 14 Sep 2024 14:15:45 +0800 Subject: [PATCH] test: fix keepalive assert check (#535) ## 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. --- test/fixtures/server.ts | 21 +++++++++++++++++++++ test/keep-alive-header.test.ts | 16 ++++++++++------ test/options.digestAuth.test.ts | 11 +++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/test/fixtures/server.ts b/test/fixtures/server.ts index 6bdd593b..ba9bbcf3 100644 --- a/test/fixtures/server.ts +++ b/test/fixtures/server.ts @@ -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) { diff --git a/test/keep-alive-header.test.ts b/test/keep-alive-header.test.ts index 83e1d620..aef85147 100644 --- a/test/keep-alive-header.test.ts +++ b/test/keep-alive-header.test.ts @@ -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); @@ -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: {} diff --git a/test/options.digestAuth.test.ts b/test/options.digestAuth.test.ts index e9385a3d..be5e5a0e 100644 --- a/test/options.digestAuth.test.ts +++ b/test/options.digestAuth.test.ts @@ -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',