Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix keepalive assert check #535

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}));
}
Comment on lines +103 to +122
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The new /digestAuth2 endpoint implementation looks good.

The Digest Authentication flow is correctly implemented, handling the cases when the Authorization header is missing or invalid, and responding with the appropriate status codes and headers.

Consider refactoring the common logic between the /digestAuth and /digestAuth2 endpoints to avoid code duplication. You can extract the shared logic into a separate function that takes the header name (www-authenticate or x-www-authenticate) as a parameter.

For example:

function handleDigestAuth(req: IncomingMessage, res: ServerResponse, headerName: string) {
  const authorization = req.headers.authorization;
  if (!authorization) {
    res.setHeader(headerName, '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(headerName, 'Digest realm="testrealm@urllib.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"');
    res.statusCode = 401;
    return res.end(JSON.stringify({
      error: 'authorization invalid',
    }));
  }
  return res.end(JSON.stringify({
    authorization,
  }));
}

// Usage in the `/digestAuth` endpoint
if (pathname === '/digestAuth') {
  return handleDigestAuth(req, res, 'www-authenticate');
}

// Usage in the `/digestAuth2` endpoint  
if (pathname === '/digestAuth2') {
  return handleDigestAuth(req, res, 'x-www-authenticate');
}

This refactoring will make the code more maintainable and easier to update in the future if any changes are needed to the Digest Authentication logic.


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
Loading