-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
3 changed files
with
125 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { shouldLogIncomingRequest } from './log-requests' | ||
import type { NodeNextRequest } from '../base-http/node' | ||
import type { LoggingConfig } from '../config-shared' | ||
|
||
describe('shouldLogIncomingRequest', () => { | ||
const createMockRequest = (url: string): NodeNextRequest => { | ||
return { url } as NodeNextRequest | ||
} | ||
|
||
it('should return true when logging config is undefined', () => { | ||
const req = createMockRequest('/api/test') | ||
expect(shouldLogIncomingRequest(req, undefined)).toBe(true) | ||
}) | ||
|
||
it('should return true when incomingRequest is true', () => { | ||
const req = createMockRequest('/api/test') | ||
const config: LoggingConfig = { incomingRequest: true } | ||
expect(shouldLogIncomingRequest(req, config)).toBe(true) | ||
}) | ||
|
||
it('should return false when incomingRequest is false', () => { | ||
const req = createMockRequest('/api/test') | ||
const config: LoggingConfig = { incomingRequest: false } | ||
expect(shouldLogIncomingRequest(req, config)).toBe(false) | ||
}) | ||
|
||
it('should return true when ignorePattern is empty', () => { | ||
const req = createMockRequest('/api/test') | ||
const config: LoggingConfig = { | ||
incomingRequest: { ignorePattern: [] }, | ||
} | ||
expect(shouldLogIncomingRequest(req, config)).toBe(true) | ||
}) | ||
|
||
it('should filter requests matching ignorePattern', () => { | ||
const req = createMockRequest('/api/health') | ||
const config: LoggingConfig = { | ||
incomingRequest: { | ||
ignorePattern: [/^\/api\/health/], | ||
}, | ||
} | ||
expect(shouldLogIncomingRequest(req, config)).toBe(false) | ||
}) | ||
|
||
it('should allow requests not matching ignorePattern', () => { | ||
const req = createMockRequest('/api/users') | ||
const config: LoggingConfig = { | ||
incomingRequest: { | ||
ignorePattern: [/^\/api\/health/, /^\/metrics/], | ||
}, | ||
} | ||
expect(shouldLogIncomingRequest(req, config)).toBe(true) | ||
}) | ||
|
||
it('should return true when ignorePattern contains invalid patterns', () => { | ||
const req = createMockRequest('/api/test') | ||
const config: LoggingConfig = { | ||
incomingRequest: { | ||
// @ts-expect-error testing invalid pattern | ||
ignorePattern: ['not-a-regex'], | ||
}, | ||
} | ||
expect(shouldLogIncomingRequest(req, config)).toBe(true) | ||
}) | ||
}) |
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