forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request newrelic#65 from michaelgoin/remove-aws-server-dep…
…endency Remove AWS servers as dependency for versioned tests
- Loading branch information
Showing
16 changed files
with
573 additions
and
289 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
44 changes: 44 additions & 0 deletions
44
merged/aws-sdk/tests/versioned/aws-server-stubs/empty-response-server/index.js
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,44 @@ | ||
/* | ||
* Copyright 2020 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
'use strict' | ||
|
||
const http = require('http') | ||
|
||
function createEmptyResponseServer() { | ||
const server = http.createServer(function(req, res) { | ||
if ( | ||
req.method === 'GET' || | ||
req.method === 'POST' || | ||
req.method === 'PUT' || | ||
req.method === 'HEAD' || | ||
req.method === 'DELETE' | ||
) { | ||
handlePost(req, res) | ||
return | ||
} | ||
|
||
// sometimes the aws-sdk will obfuscate this error | ||
// so logging out. | ||
// eslint-disable-next-line no-console | ||
console.log('Unhandled request method: ', req.method) | ||
|
||
res.statusCode = 500 | ||
res.end('Unhandled request method') | ||
}) | ||
|
||
return server | ||
} | ||
|
||
function handlePost(req, res) { | ||
req.on('data', () => {}) | ||
|
||
req.on('end', () => { | ||
// currently, some tests do not rely on real responses back. | ||
// it is enough to return something valid to the client. | ||
res.end() | ||
}) | ||
} | ||
|
||
module.exports = createEmptyResponseServer |
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,21 @@ | ||
/* | ||
* Copyright 2020 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
'use strict' | ||
|
||
const createSqsServer = require('./sqs-server') | ||
const createEmptyResponseServer = require('./empty-response-server') | ||
|
||
// Specific values are unimportant because we'll be hitting our | ||
// custom servers. But they need to be populated. | ||
const FAKE_CREDENTIALS = { | ||
accessKeyId: 'FAKE ID', | ||
secretAccessKey: 'FAKE KEY' | ||
} | ||
|
||
module.exports = { | ||
createSqsServer, | ||
createEmptyResponseServer, | ||
FAKE_CREDENTIALS | ||
} |
181 changes: 181 additions & 0 deletions
181
merged/aws-sdk/tests/versioned/aws-server-stubs/sqs-server/index.js
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,181 @@ | ||
/* | ||
* Copyright 2020 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
'use strict' | ||
|
||
const http = require('http') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
function createSqsServer() { | ||
const server = http.createServer(function(req, res) { | ||
if (req.method === 'POST') { | ||
handleSqsPost(req, res) | ||
return | ||
} | ||
|
||
res.statusCode = 500 | ||
res.end() | ||
}) | ||
|
||
return server | ||
} | ||
|
||
function handleSqsPost(req, res) { | ||
let body = '' | ||
|
||
req.on('data', chunk => { | ||
body += chunk.toString() | ||
}) | ||
|
||
req.on('end', () => { | ||
const endpoint = `http://localhost:${req.connection.localPort}` | ||
const parsed = parseBody(body) | ||
|
||
const getDataFunction = createGetDataFromAction(endpoint, parsed) | ||
|
||
getDataFunction((err, data) => { | ||
if (err) { | ||
res.statusCode = 500 | ||
// eslint-disable-next-line no-console | ||
console.log(err) | ||
} | ||
|
||
res.end(data) | ||
}) | ||
}) | ||
} | ||
|
||
function createGetDataFromAction(endpoint, body) { | ||
switch (body.Action) { | ||
case 'CreateQueue': | ||
return getCreateQueueResponse.bind(null, endpoint, body.QueueName) | ||
case 'SendMessage': | ||
return getSendMessageResponse.bind(null) | ||
case 'SendMessageBatch': | ||
return getSendMessageBatchResponse.bind(null) | ||
case 'ReceiveMessage': | ||
return getReceiveMessageResponse.bind(null) | ||
default: | ||
return function actionNotImplemented(callback) { | ||
setImmediate(() => { | ||
callback(new Error('Action not implemented')) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
function parseBody(body) { | ||
const parsed = Object.create(null) | ||
|
||
const items = body.split('&') | ||
items.forEach((item) => { | ||
const [key, value] = item.split('=') | ||
parsed[key] = value | ||
}) | ||
|
||
return parsed | ||
} | ||
|
||
let createQueueResponse = null | ||
function getCreateQueueResponse(endpoint, queueName, callback) { | ||
if (createQueueResponse) { | ||
const modifiedResponse = replaceQueueUrl(createQueueResponse, endpoint, queueName) | ||
|
||
setImmediate(() => { | ||
callback(null, modifiedResponse) | ||
}) | ||
return | ||
} | ||
|
||
readFromXml('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>` | ||
) | ||
|
||
return modifiedResponse | ||
} | ||
|
||
let sendMessageResponse = null | ||
function getSendMessageResponse(callback) { | ||
if (sendMessageResponse) { | ||
setImmediate(() => { | ||
callback(null, sendMessageResponse) | ||
}) | ||
return | ||
} | ||
|
||
readFromXml('send-message-response.xml', (err, data) => { | ||
if (err) { | ||
callback(err) | ||
return | ||
} | ||
|
||
sendMessageResponse = data | ||
callback(null, sendMessageResponse) | ||
}) | ||
} | ||
|
||
let sendMessageBatchResponse = null | ||
function getSendMessageBatchResponse(callback) { | ||
if (sendMessageBatchResponse) { | ||
setImmediate(() => { | ||
callback(null, sendMessageBatchResponse) | ||
}) | ||
return | ||
} | ||
|
||
readFromXml('send-message-batch-response.xml', (err, data) => { | ||
if (err) { | ||
callback(err) | ||
return | ||
} | ||
|
||
sendMessageBatchResponse = data | ||
callback(null, sendMessageBatchResponse) | ||
}) | ||
} | ||
|
||
let receiveMessageResponse = null | ||
function getReceiveMessageResponse(callback) { | ||
if (receiveMessageResponse) { | ||
setImmediate(() => { | ||
callback(null, receiveMessageResponse) | ||
}) | ||
return | ||
} | ||
|
||
readFromXml('receive-message-response.xml', (err, data) => { | ||
if (err) { | ||
callback(err) | ||
return | ||
} | ||
|
||
receiveMessageResponse = data | ||
callback(null, receiveMessageResponse) | ||
}) | ||
} | ||
|
||
function readFromXml(fileName, callback) { | ||
const fullPath = path.join(__dirname, 'responses', fileName) | ||
fs.readFile(fullPath, 'utf8', function(err, data) { | ||
callback(err, data) | ||
}) | ||
} | ||
|
||
module.exports = createSqsServer |
Oops, something went wrong.