Skip to content

Commit

Permalink
Add configs for tests
Browse files Browse the repository at this point in the history
Bugs fixed 138414116 138413577 138413303
  • Loading branch information
rleddy committed Aug 14, 2019
1 parent fe2a860 commit 18d08d1
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/plugins-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, cor
}

var hostname = target_headers.host;
if (hostname) { // might be missing (with an http-1.0 client for example)
if ( hostname ) { // might be missing (with an http-1.0 client for example)

if (_configured(config, 'x-forwarded-host')) {
// append host header to the x-forwarded-host header
Expand Down Expand Up @@ -240,9 +240,11 @@ function getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, cor
targetRequestOptions.timeout = config.edgemicro.request_timeout * 1000;
}

// TARGET REQUEST MADE HERE. TARGET REQUEST
const targetRequest = httpLibrary.request(targetRequestOptions,
(targetResponse) => cb(null, targetResponse));

// locally enforced timeout, other than relying on the http module timeout.
if (config.edgemicro.request_timeout) {
const timeoutInterval = config.edgemicro.request_timeout * 1000;
targetRequest.setTimeout(timeoutInterval, () => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"mocha": "^5.2.0",
"nyc": "^14.1.1",
"restify": "~7.2.1",
"rewire": "^4.0.1",
"should": "~8.2.2",
"test-until": "^1.1.1",
"tmp": "0.0.28"
Expand Down
14 changes: 14 additions & 0 deletions tests/hello_rest/notalker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const http = require('http');

const server = http.createServer((req, res) => {
//res.end();
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});

server.listen(8999);

console.log("SERVER CREATED")

process.send({ "port" : 8999 })
278 changes: 278 additions & 0 deletions tests/target-behavior.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
const assert = require('assert');
const rewire = require('rewire');
const PluginsMiddleware = rewire('../lib/plugins-middleware');
const {spawn} = require('child_process')


var mockLogger = {
info: function (obj, msg) {
},
warn: function (obj, msg) {
},
error: function (obj, msg) {
},
eventLog: function (obj, msg) {
},
consoleLog: function (level, ...data) {
},
stats: function (statsInfo, msg) {
},
setLevel: function (level) {
},
writeLogRecord: function(record,cb) {
}
};

function launchFauxServer() {
//
var myServer = spawn('node', [__dirname + '/hello_rest/notalker.js'], {
'stdio' : [ 'pipe', 'pipe', 'pipe', 'ipc' ]
})

myServer.on('error', (e) => {
console.log('little server error: ' + e.message)
})

return myServer
}


describe('target behavior', () => {
//
it('handles connection refused', (done) => {
//
var getTargetRequest = PluginsMiddleware.__get__('getTargetRequest')
//
PluginsMiddleware.__set__('logging',{
getLogger : () => {
return mockLogger
}
})

//
var mockConfig = {
headers : {
'x-forwarded-for' : "xyz",
'x-forwarded-id' : "xyz",
'x-forwarded-proto' : "xyz",
'via' : "xyz",
'x-forwarded-host' : "wasit"
},
edgemicro : {
request_timeout : 100
}
};

PluginsMiddleware.__set__('configService', {
get : () => {
return mockConfig
}
});
//

//getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, correlation_id, cb)

var sourceRequest = {
'method' : 'GET',
'connection' : {
'encrypted' : false
},
'socket' : {
remoteAddress : "localhost"
},
'headers': {
'hostname' : 'localhost:7777',
'transfer-encoding' : 'chunked'
},
'targetSecure' : false,
'targetHostname' : 'localhost',
'targetPort' : 8888,
}

var sourceResponse = {
'proxy' : {
'agent' : undefined
},
'content-length' : 50
}

var plugins = []
var startTime = Date.now()

var correlation_id = "osirusor"
var cb = (e) => {
console.log(e.message)

console.dir(sourceResponse,{ depth : 2 })
assert(sourceResponse.statusCode === 502)
assert("connect ECONNREFUSED 127.0.0.1:8888" === e.message)
done()
}

var treq = getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, correlation_id, cb)

treq.end()
});

//
it('handles connection ENOTFOUND', (done) => {
//
var getTargetRequest = PluginsMiddleware.__get__('getTargetRequest')
//
PluginsMiddleware.__set__('logging',{
getLogger : () => {
return mockLogger
}
})

//
var mockConfig = {
headers : {
'x-forwarded-for' : "xyz",
'x-forwarded-id' : "xyz",
'x-forwarded-proto' : "xyz",
'via' : "xyz",
'x-forwarded-host' : "wasit"
},
edgemicro : {
request_timeout : 100
}
};

PluginsMiddleware.__set__('configService', {
get : () => {
return mockConfig
}
});
//

//getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, correlation_id, cb)

var sourceRequest = {
'method' : 'GET',
'connection' : {
'encrypted' : false
},
'socket' : {
remoteAddress : "this.does.not.exist"
},
'headers': {
'hostname' : 'this.does.not.exist:7777',
'transfer-encoding' : 'chunked'
},
'targetSecure' : false,
'targetHostname' : 'this.does.not.exist',
'targetPort' : 8999,
}

var sourceResponse = {
'proxy' : {
'agent' : undefined
},
'content-length' : 50
}

var plugins = []
var startTime = Date.now()

var correlation_id = "osirusor"
var cb = (e) => {
console.log(e)
assert(e.code === 'ENOTFOUND')

console.dir(sourceResponse,{ depth : 2 })
console.log(e.message)

assert(typeof e.message === "string")
assert(e.message.indexOf("getaddrinfo ENOTFOUND this.does.not.exist") >= 0)
done()
}

var treq = getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, correlation_id, cb)

treq.end()
});

//
it('handles connection TIMEMOUT', (done) => {
//
var noTalker = launchFauxServer()

noTalker.on('message',(msg) => {
console.log(msg)
var getTargetRequest = PluginsMiddleware.__get__('getTargetRequest')
//
PluginsMiddleware.__set__('logging',{
getLogger : () => {
return mockLogger
}
})
//
var mockConfig = {
headers : {
'x-forwarded-for' : "xyz",
'x-forwarded-id' : "xyz",
'x-forwarded-proto' : "xyz",
'via' : "xyz",
'x-forwarded-host' : "wasit"
},
edgemicro : {
request_timeout : (1/2)
}
};

PluginsMiddleware.__set__('configService', {
get : () => {
return mockConfig
}
});
//

//getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, correlation_id, cb)

var sourceRequest = {
'method' : 'GET',
'connection' : {
'encrypted' : false
},
'socket' : {
remoteAddress : "this.does.not.exist"
},
'headers': {
'hostname' : 'localhost:8999',
'transfer-encoding' : 'chunked'
},
'targetSecure' : false,
'targetHostname' : 'localhost',
'targetPort' : 8999,
}

var sourceResponse = {
'proxy' : {
'agent' : undefined
},
'content-length' : 50
}

var plugins = []
var startTime = Date.now()

var correlation_id = "osirusor"
var cb = (e) => {
assert("Gateway timed out trying to reach target" === e.message)
assert(sourceResponse.statusCode === 504)

noTalker.kill()
//
assert(true)
done()
}

var treq = getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, correlation_id, cb)

treq.end()
})

});

});

0 comments on commit 18d08d1

Please sign in to comment.