Skip to content

Commit 72b06f9

Browse files
committed
fix: 404 and 401 logging changes
1 parent 53a8eb8 commit 72b06f9

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/auth/auth.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function _isBasicAuthPass(request) {
1717
return false;
1818
}
1919
const authHeader = request.headers.authorization;
20-
console.log(authHeader);
2120
if (!authHeader) {
2221
return false;
2322
}

src/server.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import {HTTP_STATUS_CODES} from "@aicore/libcommonutils";
2222
import {getConfigs} from "./utils/configs.js";
2323
import {getHelloSchema, hello} from "./api/hello.js";
2424

25-
const server = fastify({logger: true});
25+
const server = fastify({
26+
logger: true,
27+
// https://www.fastify.io/docs/latest/Reference/Server/#trustproxy
28+
// needed to forward correct IP addresses in a reverse proxy configuration
29+
trustProxy: true
30+
});
2631
/* Adding an authentication hook to the server. A hook is a function that is called when a request is made to
2732
the server. */
2833
server.addHook('onRequest', (request, reply, done) => {
@@ -33,11 +38,13 @@ server.addHook('onRequest', (request, reply, done) => {
3338
});
3439

3540
if (!routeExists) {
41+
console.error("route does not exist for ", request.raw.url);
3642
reply.code(HTTP_STATUS_CODES.NOT_FOUND);
37-
done(new Error('Not Found'));
43+
done('Not Found');
3844
} else if (!isAuthenticated(request)) {
45+
console.error(`Unauthorised access for ${request.raw.url} from IP ${request.ips} `);
3946
reply.code(HTTP_STATUS_CODES.UNAUTHORIZED);
40-
done(new Error('Wrong key'));
47+
done("Unauthorized");
4148
} else {
4249
done();
4350
}

test/integration/hello.spec.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,15 @@ describe('Integration Tests for hello api', function () {
3131

3232
it('should not say helloAuth if unauthorised', async function () {
3333
let output = await fetch("http://localhost:5000/helloAuth?name=world", { method: 'GET'});
34-
output = await output.json();
35-
expect(output).eql({
36-
"error": "Unauthorized",
37-
"message": "Wrong key",
38-
"statusCode": 401});
34+
expect(output.status).eql(401);
35+
output = await output.text();
36+
expect(output).eql("Unauthorized");
3937
});
4038

4139
it('should reply 404 if route doesnt exist', async function () {
4240
let output = await fetch("http://localhost:5000/routeNotExist?name=world", { method: 'GET'});
43-
output = await output.json();
44-
expect(output).eql({
45-
"statusCode": 404,
46-
"error": "Not Found",
47-
"message": "Not Found"
48-
});
41+
expect(output.status).eql(404);
42+
output = await output.text();
43+
expect(output).eql("Not Found");
4944
});
5045
});

0 commit comments

Comments
 (0)