-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.test.js
More file actions
69 lines (54 loc) · 2.5 KB
/
utils.test.js
File metadata and controls
69 lines (54 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { getTraceId } from '@defra/hapi-tracing'
import { config } from '~/src/config/index.js'
import { applyTraceHeaders, isValidUUID } from '~/src/server/utils/utils.js'
jest.mock('@defra/hapi-tracing')
describe('Header helper functions', () => {
it('should include the trace id in the headers if available', () => {
jest.mocked(getTraceId).mockReturnValue('my-trace-id')
const result = applyTraceHeaders() // Updated to applyTraceHeaders
expect(result).toEqual({
[config.get('tracing').header]: 'my-trace-id'
})
})
it('should exclude the trace id in the headers if missing', () => {
jest.mocked(getTraceId).mockReturnValue(null)
const result = applyTraceHeaders() // Updated to applyTraceHeaders
expect(result).toBeUndefined()
})
it('should merge existing headers with the trace id if available', () => {
jest.mocked(getTraceId).mockReturnValue('my-trace-id')
const existingHeaders = { Authorization: 'Bearer token' }
const result = applyTraceHeaders(existingHeaders) // Updated to applyTraceHeaders
expect(result).toEqual({
Authorization: 'Bearer token',
[config.get('tracing').header]: 'my-trace-id'
})
})
it('should return existing headers without modification if trace id is missing', () => {
jest.mocked(getTraceId).mockReturnValue(null)
const existingHeaders = { Authorization: 'Bearer token' }
const result = applyTraceHeaders(existingHeaders) // Updated to applyTraceHeaders
expect(result).toEqual({
Authorization: 'Bearer token'
})
})
it('should return existing headers if tracing header configuration is missing', () => {
const existingHeaders = { Authorization: 'Bearer token' }
const result = applyTraceHeaders(existingHeaders, '')
expect(result).toBe(existingHeaders)
})
it.each([
{ uuid: '1f457a37-7b99-452e-8324-df9e041abff2', valid: true },
{ uuid: '0c9a2690-9a0c-4a2c-98d7-e9ef95615ac9', valid: true },
{ uuid: 'f223de3b-5ae5-44b2-8cee-ea8439adc335', valid: true },
{ uuid: '82ecc90c-bc47-4ec5-80af-1a9fc1c4c08c', valid: true },
{ uuid: 'd99ff582-ecce-474f-a44b-bc5961d977c5', valid: true },
{ uuid: '7afffc8a-81ab-4aa6-a8f5-ecf6a600a781', valid: true },
{ uuid: '7afffc8a81ab4aa6a8f5ecf6a600a781', valid: true },
{ uuid: '', valid: false },
{ uuid: 'uuid', valid: false },
{ uuid: 'h4f84ef8-b5e1-4544-94aa-1b671d50d8cb', valid: false }
])('should validate uuid appropriately %s', ({ uuid, valid }) => {
expect(isValidUUID(uuid)).toBe(valid)
})
})