Skip to content

Commit

Permalink
feat(3666): encoded/decoded message payload
Browse files Browse the repository at this point in the history
  • Loading branch information
geka-evk committed Dec 19, 2023
1 parent 97942e7 commit 79fe024
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
11 changes: 8 additions & 3 deletions src/lib/dto.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const { Headers } = require('@mojaloop/central-services-shared').Enum.Http
const { Enum, Util } = require('@mojaloop/central-services-shared')

const { Headers } = Enum.Http
const { decodePayload, encodePayload } = Util.StreamingProtocol

const messageFromRequestDto = (request, type, action) => {
const { headers, payload, params } = request
const { spanContext } = request.span || {}
const id = params.id || payload.quoteId || payload.bulkQuoteId
const encodedJson = encodePayload(JSON.stringify(payload), headers[Headers.GENERAL.CONTENT_TYPE.value])

return Object.freeze({
content: {
requestId: request.info?.id,
headers,
payload, // todo: base64 encoded
payload: encodedJson,
uriParams: params,
spanContext,
id,
Expand All @@ -30,7 +34,8 @@ const requestDataFromMessageDto = (message) => {
return Object.freeze({
topic,
requestData: {
...value.content
...value.content,
payload: decodePayload(value.content?.payload)
// see messageFromRequestDto for details of "content" field
}
})
Expand Down
10 changes: 7 additions & 3 deletions test/unit/handlers/QuotingHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ const createRequestData = ({
action = 'action'
} = {}) => {
const httpRequest = mocks.mockHttpRequest({ payload })
const { content } = dto.messageFromRequestDto(httpRequest, type, action)
return content
const messageValue = dto.messageFromRequestDto(httpRequest, type, action)
const { requestData } = dto.requestDataFromMessageDto({ value: messageValue })

return requestData
}

const createKafkaMessage = (topic) => ({
topic,
value: { content: {} }
value: {
content: { payload: '{}' }
}
})

describe('QuotingHandler Tests -->', () => {
Expand Down
17 changes: 5 additions & 12 deletions test/unit/mocks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const { randomUUID } = require('node:crypto')
// const dto = require('../../src/lib/dto')

const mockHttpRequest = ({
requestId = randomUUID(),
payload = {},
params = {},
headers = { 'fspiop-source': 'payerFsp' },
requestId = randomUUID()
headers = {
'fspiop-source': 'payerFsp',
'content-type': 'application/vnd.interoperability.quotes+json;version=1.0'
}
} = {}) => ({
payload,
params,
Expand All @@ -25,14 +27,6 @@ const mockHttpRequest = ({
}
})

// const toKafkaMessageFormat = ({
// topic = 'topic',
// messageData = {}
// } = {}) => Object.freeze({
// topic,
// value: messageData
// })

const createMockHapiHandler = () => {
const code = jest.fn()
const handler = {
Expand All @@ -43,7 +37,6 @@ const createMockHapiHandler = () => {
}

module.exports = {
// toKafkaMessageFormat,
mockHttpRequest,
createMockHapiHandler
}

0 comments on commit 79fe024

Please sign in to comment.