diff --git a/.husky/commit-msg b/.husky/commit-msg index 4974c35..a78cc75 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - npx commitlint --edit $1 diff --git a/src/lib/openApiMockGenerator.js b/src/lib/openApiMockGenerator.js index 3fe6ce0..4f8b37b 100644 --- a/src/lib/openApiMockGenerator.js +++ b/src/lib/openApiMockGenerator.js @@ -360,6 +360,10 @@ const customFormatHandlers = { 'date-time': () => { // Always use toISOString() which includes milliseconds return new Date().toISOString() + }, + date: () => { + // Return just the date part in ISO format: YYYY-MM-DD + return new Date().toISOString().split('T')[0] } } @@ -369,6 +373,56 @@ const customTypeHandler = (schema) => { const key = schema.key || '' + // Handle common HTTP headers with proper formats + if (matchesField(key, 'accept', 'Accept')) { + return 'application/vnd.interoperability.transactionRequests+json;version=1' + } + + if (matchesField(key, 'content-type', 'Content-Type', 'contentType')) { + return 'application/vnd.interoperability.transactionRequests+json;version=1.1' + } + + // Handle date in headers vs date in body - different formats for each context + // For HTTP headers: Use UTC string format (Tue, 03 Jun 2025 00:00:24 GMT) + // For body fields: Use ISO format (YYYY-MM-DD) + if (key.toLowerCase() === 'date') { + const date = new Date() + // If this is a header parameter, use UTC string format + if (schema.parentKey === 'parameters' || schema.in === 'header') { + return date.toUTCString() + } + // Otherwise it's a body field, use ISO date format + return date.toISOString().split('T')[0] + } + + if (matchesField(key, 'x-forwarded-for', 'X-Forwarded-For')) { + return 'in' + } + + if (matchesField(key, 'fspiop-source', 'FSPIOP-Source', 'fspiopSource')) { + return 'magna' + } + + if (matchesField(key, 'fspiop-destination', 'FSPIOP-Destination', 'fspiopDestination')) { + return 'culpa magna proident' + } + + if (matchesField(key, 'fspiop-encryption', 'FSPIOP-Encryption', 'fspiopEncryption')) { + return 'voluptate incididunt ut sed' + } + + if (matchesField(key, 'fspiop-signature', 'FSPIOP-Signature', 'fspiopSignature')) { + return 'non Lorem consequat' + } + + if (matchesField(key, 'fspiop-uri', 'FSPIOP-URI', 'fspiopUri')) { + return 'labore' + } + + if (matchesField(key, 'fspiop-http-method', 'FSPIOP-HTTP-Method', 'fspiopHttpMethod')) { + return 'Duis id' + } + // Always handle state, reason, status fields if (matchesField(key, 'state', 'status', 'reason')) { return ['Created', 'Closed'][Math.floor(Math.random() * 2)] @@ -396,8 +450,8 @@ const customTypeHandler = (schema) => { return customFormatHandlers['date-time']() } - // Handle date fields - if (matchesField(key, 'dateOfBirth', 'date')) { + // Handle date fields (as body parameters, not HTTP headers) + if (matchesField(key, 'dateOfBirth', 'date') && schema.parentKey !== 'parameters') { const date = new Date() return date.toISOString().split('T')[0] } @@ -828,7 +882,9 @@ const generateMockHeaders = async (method, name, data, jsfRefs) => { const properties = {} data.parameters.forEach(param => { if (param.in === 'header') { - properties[param.name] = (param.schema && param.schema.type) ? { type: param.schema.type } : {} + properties[param.name] = (param.schema && param.schema.type) + ? { type: param.schema.type, in: 'header' } + : { in: 'header' } } }) diff --git a/test/unit/lib/openApiMockGenerator.test.js b/test/unit/lib/openApiMockGenerator.test.js index 66d5316..45227b1 100644 --- a/test/unit/lib/openApiMockGenerator.test.js +++ b/test/unit/lib/openApiMockGenerator.test.js @@ -700,3 +700,86 @@ describe('postProcessMock and FSP ID Generation Tests', () => { }) }) }) + +describe('Header Generation', () => { + it('should generate specific HTTP headers with correct formats', async () => { + const mockGenerator = new OpenApiMockGenerator() + + // Mock the schema.paths property for the OpenApiRequestGenerator + mockGenerator.schema = { + paths: { + '/test': { + get: { + operationId: 'testOperation', + parameters: [ + { + in: 'header', + name: 'accept', + schema: { type: 'string' } + }, + { + in: 'header', + name: 'content-type', + schema: { type: 'string' } + }, + { + in: 'header', + name: 'date', + schema: { type: 'string' } + }, + { + in: 'header', + name: 'x-forwarded-for', + schema: { type: 'string' } + }, + { + in: 'header', + name: 'fspiop-source', + schema: { type: 'string' } + }, + { + in: 'header', + name: 'fspiop-destination', + schema: { type: 'string' } + }, + { + in: 'header', + name: 'fspiop-encryption', + schema: { type: 'string' } + }, + { + in: 'header', + name: 'fspiop-signature', + schema: { type: 'string' } + }, + { + in: 'header', + name: 'fspiop-uri', + schema: { type: 'string' } + }, + { + in: 'header', + name: 'fspiop-http-method', + schema: { type: 'string' } + } + ] + } + } + } + } + + const headers = await mockGenerator.generateRequestHeaders('/test', 'get', []) + + expect(headers.accept).toBe('application/vnd.interoperability.transactionRequests+json;version=1') + expect(headers['content-type']).toBe('application/vnd.interoperability.transactionRequests+json;version=1.1') + expect(typeof headers.date).toBe('string') + expect(headers.date).toMatch(/^\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/) + expect(headers['x-forwarded-for']).toBe('in') + expect(headers['fspiop-source']).toBe('magna') + expect(headers['fspiop-destination']).toBe('culpa magna proident') + expect(headers['fspiop-encryption']).toBe('voluptate incididunt ut sed') + expect(headers['fspiop-signature']).toBe('non Lorem consequat') + expect(headers['fspiop-uri']).toBe('labore') + expect(headers['fspiop-http-method']).toBe('Duis id') + }) +})