Skip to content

Commit

Permalink
Merge pull request newrelic#188 from bizob2828/fix-v2
Browse files Browse the repository at this point in the history
fix: Updated the mock AWS server to handle both XML and JSON.
  • Loading branch information
bizob2828 authored May 10, 2023
2 parents 2405689 + 1d214bc commit 4fd1fa1
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 197 deletions.
6 changes: 5 additions & 1 deletion merged/aws-sdk/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
'use strict'
module.exports = {
extends: '@newrelic'
extends: '@newrelic',
parserOptions: {
ecmaVersion: '2020'
},

}
2 changes: 1 addition & 1 deletion merged/aws-sdk/THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ This product includes source derived from [@newrelic/test-utilities](https://git

### aws-sdk

This product includes source derived from [aws-sdk](https://github.com/aws/aws-sdk-js) ([v2.1354.0](https://github.com/aws/aws-sdk-js/tree/v2.1354.0)), distributed under the [Apache-2.0 License](https://github.com/aws/aws-sdk-js/blob/v2.1354.0/LICENSE.txt):
This product includes source derived from [aws-sdk](https://github.com/aws/aws-sdk-js) ([v2.1372.0](https://github.com/aws/aws-sdk-js/tree/v2.1372.0)), distributed under the [Apache-2.0 License](https://github.com/aws/aws-sdk-js/blob/v2.1372.0/LICENSE.txt):

```
Expand Down
6 changes: 3 additions & 3 deletions merged/aws-sdk/lib/v2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function wrapMakeRequest(shim, fn, name, request) {
}

const service = getServiceName(this)
const region = this.config && this.config.region
const region = this?.config?.region
request.on('complete', function onAwsRequestComplete() {
const httpRequest = request.httpRequest && request.httpRequest.stream
const segment = shim.getSegment(httpRequest)
Expand All @@ -55,8 +55,8 @@ function wrapMakeRequest(shim, fn, name, request) {
return
}

const requestRegion = request.httpRequest.region
const requestId = request.response && request.response.requestId
const requestRegion = request?.httpRequest?.region
const requestId = request?.response?.requestId

segment.addAttribute('aws.operation', request.operation || UNKNOWN)
segment.addAttribute('aws.requestId', requestId || UNKNOWN)
Expand Down
14 changes: 7 additions & 7 deletions merged/aws-sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion merged/aws-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@newrelic/eslint-config": "^0.0.2",
"@newrelic/newrelic-oss-cli": "^0.1.2",
"@newrelic/test-utilities": "^7.3.0",
"aws-sdk": "^2.403.0",
"aws-sdk": "^2.1372.0",
"c8": "^7.12.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ const common = module.exports
const path = require('path')
const fs = require('fs')

common.parseBody = function parseBody(body) {
const parsed = Object.create(null)
common.parseBody = function parseBody(body, headers) {
try {
const parsed = JSON.parse(body)
parsed.Action = headers['x-amz-target'].split('.')[1]
return parsed
} catch {
const parsed = Object.create(null)

const items = body.split('&')
items.forEach((item) => {
const [key, value] = item.split('=')
parsed[key] = value
})
const items = body.split('&')
items.forEach((item) => {
const [key, value] = item.split('=')
parsed[key] = value
})

return parsed
return parsed
}
}

common.readFromXml = function readFromXml(filePath, callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ function handlePost(req, res) {
})

req.on('end', () => {
const isJson = !!req.headers['x-amz-target']
const endpoint = `http://localhost:${req.connection.localPort}`
const parsed = parseBody(body)
const parsed = parseBody(body, req.headers)

const getDataFunction = createGetDataFromAction(endpoint, parsed)
const getDataFunction = createGetDataFromAction(endpoint, parsed, isJson)

getDataFunction((err, data) => {
if (err) {
Expand All @@ -55,25 +56,29 @@ function handlePost(req, res) {
console.log(err)
}

if (isJson) {
res.setHeader('x-amz-request-id', data.ResponseMetadata.RequestId)
data = JSON.stringify(data)
}
res.end(data)
})
})
}

function createGetDataFromAction(endpoint, body) {
function createGetDataFromAction(endpoint, body, isJson) {
switch (body.Action) {
case 'Publish':
return getPublishResponse.bind(null)
case 'ListTopics':
return getListTopicsResponse.bind(null)
case 'CreateQueue':
return getCreateQueueResponse.bind(null, endpoint, body.QueueName)
return getCreateQueueResponse.bind(null, endpoint, body.QueueName, isJson)
case 'SendMessage':
return getSendMessageResponse.bind(null)
return getSendMessageResponse.bind(null, isJson)
case 'SendMessageBatch':
return getSendMessageBatchResponse.bind(null)
return getSendMessageBatchResponse.bind(null, isJson)
case 'ReceiveMessage':
return getReceiveMessageResponse.bind(null)
return getReceiveMessageResponse.bind(null, isJson)
case 'SendEmail':
return getSendEmailResponse.bind(null)
case 'AcceptReservedNodeExchange':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,36 @@

'use strict'
const helpers = module.exports
const { readFromXml } = require('../common')

let createQueueResponse = null
helpers.getCreateQueueResponse = function getCreateQueueResponse(endpoint, queueName, callback) {
if (createQueueResponse) {
const modifiedResponse = replaceQueueUrl(createQueueResponse, endpoint, queueName)

setImmediate(() => {
callback(null, modifiedResponse)
})
return
}

readFromXml('./sqs/responses/create-queue-response.xml', (err, data) => {
if (err) {
callback(err)
return
}

createQueueResponse = data
const modifiedResponse = replaceQueueUrl(createQueueResponse, endpoint, queueName)

callback(null, modifiedResponse)
})
}

function replaceQueueUrl(xml, endpoint, queueName) {
const modifiedResponse = xml.replace(
'<QueueUrl></QueueUrl>',
`<QueueUrl>${endpoint}/queue/${queueName}</QueueUrl>`
helpers.getCreateQueueResponse = function getCreateQueueResponse(
endpoint,
queueName,
isJson,
callback
) {
const createQueueResponse = require('./responses/create-queue-response')(
endpoint,
queueName,
isJson
)

return modifiedResponse
callback(null, createQueueResponse)
}

let sendMessageResponse = null
helpers.getSendMessageResponse = function getSendMessageResponse(callback) {
if (sendMessageResponse) {
setImmediate(() => {
callback(null, sendMessageResponse)
})
return
}

readFromXml('./sqs/responses/send-message-response.xml', (err, data) => {
if (err) {
callback(err)
return
}

sendMessageResponse = data
callback(null, sendMessageResponse)
})
helpers.formatUrl = function formatUrl(endpoint, queueName) {
return `${endpoint}/queue/${queueName}`
}

let sendMessageBatchResponse = null
helpers.getSendMessageBatchResponse = function getSendMessageBatchResponse(callback) {
if (sendMessageBatchResponse) {
setImmediate(() => {
callback(null, sendMessageBatchResponse)
})
return
}

readFromXml('./sqs/responses/send-message-batch-response.xml', (err, data) => {
if (err) {
callback(err)
return
}

sendMessageBatchResponse = data
callback(null, sendMessageBatchResponse)
})
helpers.getSendMessageResponse = function getSendMessageResponse(isJson, callback) {
const sendMessageResponse = require('./responses/send-message-response')(isJson)
callback(null, sendMessageResponse)
}

let receiveMessageResponse = null
helpers.getReceiveMessageResponse = function getReceiveMessageResponse(callback) {
if (receiveMessageResponse) {
setImmediate(() => {
callback(null, receiveMessageResponse)
})
return
}

readFromXml('./sqs/responses/receive-message-response.xml', (err, data) => {
if (err) {
callback(err)
return
}
helpers.getSendMessageBatchResponse = function getSendMessageBatchResponse(isJson, callback) {
const sendMessageBatchResponse = require('./responses/send-message-batch-response')(isJson)
callback(null, sendMessageBatchResponse)
}

receiveMessageResponse = data
callback(null, receiveMessageResponse)
})
helpers.getReceiveMessageResponse = function getReceiveMessageResponse(isJson, callback) {
const receiveMessageResponse = require('./responses/receive-message-response')(isJson)
callback(null, receiveMessageResponse)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2023 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const { formatUrl } = require('../index')

module.exports = (endpoint, queueName, isJson) => {
if (isJson) {
return {
QueueUrl: formatUrl(endpoint, queueName),
ResponseMetadata: {
RequestId: 'cb919c0a-9bce-4afe-9b48-9bdf2412bb67'
}
}
}
return `
<CreateQueueResponse>
<CreateQueueResult>
<QueueUrl>${formatUrl(endpoint, queueName)}</QueueUrl>
</CreateQueueResult>
<ResponseMetadata>
<RequestId>cb919c0a-9bce-4afe-9b48-9bdf2412bb67</RequestId>
</ResponseMetadata>
</CreateQueueResponse>`
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
/*
* Copyright 2023 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

module.exports = (isJson) => {
if (isJson) {
return {
Messages: [
{
MessageId: '5fea7756-0ea4-451a-a703-a558b933e274',
ReceiptHandle:
'MbZj6wDWliJvwwJaBV3dcjk2YW2vA3STFFljTM8tJJg6HRG6PYSasuWXPJBCwLj1FjgXUv1uSj1gUPAWV66FU/WeR4mq2OKpEGYWbnLmpRCJVAyeMjeU5ZBdtcQQEauMZc8ZRv37sIW2iJKq3M9MFx1YvV11A2x/KSbkJ0=',
MD5OfBody: 'fafb00f5732ab283681e124bf8747ed1',
Body: 'This is a test message',
Attributes: {
SenderId: '195004372649',
SentTimestamp: '1238099229000',
ApproximateReceiveCount: '2',
ApproximateFirstReceiveTimestamp: '1595887234772'
}
}
],
ResponseMetadata: {
RequestId: 'b6633655-283d-45b4-aee4-4e84e0ae6afa'
}
}
}
return `
<ReceiveMessageResponse>
<ReceiveMessageResult>
<Message>
Expand Down Expand Up @@ -30,4 +61,5 @@
<ResponseMetadata>
<RequestId>b6633655-283d-45b4-aee4-4e84e0ae6afa</RequestId>
</ResponseMetadata>
</ReceiveMessageResponse>
</ReceiveMessageResponse>`
}
Loading

0 comments on commit 4fd1fa1

Please sign in to comment.