-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
apps/server/src/apps/helpers/request-logger-middleware.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Configuration } from '@hpi-schul-cloud/commons/lib'; | ||
import { Logger } from '@nestjs/common'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { createRequestLoggerMiddleware } from './request-logger-middleware'; | ||
|
||
jest.mock('@hpi-schul-cloud/commons/lib', () => { | ||
return { | ||
Configuration: { | ||
get: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
describe('RequestLoggerMiddleware', () => { | ||
let mockRequest: Partial<Request>; | ||
let mockResponse: Partial<Response>; | ||
let nextFunction: NextFunction; | ||
let loggerSpy: jest.SpyInstance; | ||
let errorLoggerSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
mockRequest = { | ||
method: 'GET', | ||
originalUrl: '/test', | ||
}; | ||
|
||
mockResponse = { | ||
statusCode: 200, | ||
get: jest.fn(), | ||
on: jest.fn(), | ||
}; | ||
|
||
nextFunction = jest.fn(); | ||
|
||
loggerSpy = jest.spyOn(Logger.prototype, 'log'); | ||
errorLoggerSpy = jest.spyOn(Logger.prototype, 'error'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call next() when logging is disabled', () => { | ||
jest.spyOn(Configuration, 'get').mockReturnValue(false); | ||
|
||
const middleware = createRequestLoggerMiddleware(); | ||
middleware(mockRequest as Request, mockResponse as Response, nextFunction); | ||
|
||
expect(nextFunction).toHaveBeenCalled(); | ||
expect(mockResponse.on).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should log request details when logging is enabled', () => { | ||
jest.spyOn(Configuration, 'get').mockReturnValue(true); | ||
|
||
jest.spyOn(process, 'hrtime').mockReturnValueOnce([0, 0]); | ||
jest.spyOn(mockResponse, 'get').mockImplementation().mockReturnValue('100'); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
let finishCallback: Function | undefined; | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
mockResponse.on = jest.fn().mockImplementation((event: string, callback: Function) => { | ||
finishCallback = callback; | ||
}); | ||
|
||
const middleware = createRequestLoggerMiddleware(); | ||
middleware(mockRequest as Request, mockResponse as Response, nextFunction); | ||
|
||
expect(nextFunction).toHaveBeenCalled(); | ||
expect(mockResponse.on).toHaveBeenCalledWith('finish', expect.any(Function)); | ||
|
||
// Simulate response finish | ||
jest.spyOn(process, 'hrtime').mockReturnValueOnce([1, 0]); | ||
|
||
// Make sure callback was set before calling it | ||
expect(finishCallback).toBeDefined(); | ||
finishCallback?.(); | ||
|
||
expect(loggerSpy).toHaveBeenCalledWith('GET /test 200 1000ms 100'); | ||
}); | ||
|
||
it('should handle errors during logging', () => { | ||
jest.spyOn(Configuration, 'get').mockReturnValue(true); | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
mockResponse.on = jest.fn().mockImplementation((event: string, callback: Function) => { | ||
callback(); | ||
}); | ||
|
||
// Force an error by making response.get throw | ||
mockResponse.get = jest.fn().mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
const middleware = createRequestLoggerMiddleware(); | ||
middleware(mockRequest as Request, mockResponse as Response, nextFunction); | ||
|
||
expect(errorLoggerSpy).toHaveBeenCalledWith('unable to write accesslog', Error('Test error')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { Configuration } from '@hpi-schul-cloud/commons/lib'; | ||
import { Logger } from '@nestjs/common'; | ||
|
||
export const createRequestLoggerMiddleware = (): (( | ||
request: Request, | ||
response: Response, | ||
next: NextFunction | ||
) => void) => { | ||
const enabled = Configuration.get('REQUEST_LOGGING_ENABLED') as boolean; | ||
const logger = new Logger('REQUEST_LOG'); | ||
|
||
return (request: Request, response: Response, next: NextFunction): void => { | ||
if (enabled) { | ||
const startAt = process.hrtime(); | ||
const { method, originalUrl } = request; | ||
|
||
response.on('finish', () => { | ||
try { | ||
const { statusCode } = response; | ||
const contentLength = response.get('content-length') || 'unknown'; | ||
const diff = process.hrtime(startAt); | ||
const responseTime = diff[0] * 1e3 + diff[1] * 1e-6; | ||
logger.log(`${method} ${originalUrl} ${statusCode} ${responseTime}ms ${contentLength}`); | ||
} catch (error) { | ||
logger.error('unable to write accesslog', error); | ||
} | ||
}); | ||
} | ||
|
||
next(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters