Skip to content

Commit 4892512

Browse files
author
Marcos Reyes
committed
Merge pull request #165 from telefonicaid/validateExtraHeaders
Fix #150. Validate ExtraHeaders. Modify test
2 parents a7b4c4a + 3fa0103 commit 4892512

File tree

2 files changed

+119
-10
lines changed

2 files changed

+119
-10
lines changed

lib/eventWorker.js

+20
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ function validateHeaders(simpleRequest) {
191191
}
192192
}
193193

194+
//check 'X-Relayer-header'
195+
var extraHeaders = simpleRequest.headers[MG.HEAD_RELAYER_HEADER];
196+
if (extraHeaders) {
197+
extraHeaders.split(",").forEach(function (h) {
198+
var value = ""+qs.unescapeBuffer(h,true);
199+
var parts = value.split(":");
200+
201+
if (parts.length < 2) {
202+
203+
var error = {};
204+
error.type = MG.INVALID_PARAMETER;
205+
error.parameter = MG.HEAD_RELAYER_HEADER;
206+
error.userMessage = 'Value for header ' + parts[0].trim() + ' is not defined';
207+
208+
errorsHeaders.push(error);
209+
210+
}
211+
});
212+
}
213+
194214
}
195215

196216
return errorsHeaders;

test/e2e/extraHeaderTest.js

+99-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if(vm){console.log('VERBOSE MODE: ON \n Feature to test EXTRA_HEADER #FEH');}
1717
// Time to wait to check the status of the task
1818
var TIMEOUT = 100;
1919
var CREATED = 201; // 200 for older versions
20+
var INVALID_HEADERS = 400;
2021
var describeTimeout = 5000;
2122
DEFAULT_PERSISTENCE = 'BODY';
2223

@@ -188,6 +189,47 @@ function _invalidScenario(data){
188189
});
189190
}
190191

192+
function _invalidHeadersValue(data) {
193+
it(data.name + data.protocol.toUpperCase() +' /' +data.method +' #FEH', function(done){
194+
var agent = superagent.agent();
195+
var id;
196+
197+
var method;
198+
switch(data.method){
199+
case 'DELETE':
200+
method = 'del';
201+
break;
202+
default:
203+
method = data.method.toLowerCase()
204+
}
205+
206+
//SET UP the request to the advancedServer
207+
var req = agent
208+
[method](RUSHENDPOINT + data.path )
209+
.set('x-relayer-host', ENDPOINT) //Always the same endpoint
210+
.set('x-relayer-persistence',DEFAULT_PERSISTENCE)
211+
.set('content-type','application/json')
212+
.set(data.headers)
213+
if(data.method.toUpperCase() === 'POST' || data.method.toUpperCase() === 'PUT'){
214+
req = req.send(data.body);
215+
}
216+
req.end(function(err, res) {
217+
expect(err).to.not.exist;
218+
expect(res.statusCode).to.eql(INVALID_HEADERS);
219+
expect(res.body).to.exist;
220+
expect(res.body.exceptionId).to.eql('SVC0002');
221+
expect(res.body.exceptionText).to.eql('Invalid parameter value: x-relayer-header');
222+
223+
expect(res.body.userMessage).to.eql('Value for header ' + data.invalidHeader +' is not defined');
224+
225+
if(vm){console.log(res.body.userMessage);}
226+
227+
done();
228+
});
229+
230+
});
231+
}
232+
191233

192234

193235
describe('Single Feature: Extra header ' + '#FEH', function() {
@@ -218,28 +260,48 @@ describe('Single Feature: Extra header ' + '#FEH', function() {
218260

219261
describe('Retrieve request with a valid header policy request using HTTPS #FEH', function () {
220262

221-
var responseHeaders = {
263+
var responseHeaders1 = {
222264
'Fake-User-Agent':'Mozilla/5.0 (Macintosh++; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 ',
223265
'Accept-Language':'es-ES,es;q=0.8',
224266
'x': 'X-relayer-NoHost:localhost:8000'
225267
};
226-
var extraHeaders = {
268+
var extraHeaders1 = {
227269
'X-Relayer-Protocol':'https',
228270
'X-Relayer-Header': [
229271
encodeURIComponent('X: X-relayer-NoHost:localhost:8000'),
230272
encodeURIComponent('Fake-User-Agent:Mozilla/5.0 (Macintosh++; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 '),
231273
encodeURIComponent('Accept-Language:es-ES,es;q=0.8')
232274
].join(', ')};
275+
var responseHeaders2 = {
276+
'Fake-User-Agent':'Mozilla/5.0 (Macintosh++; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 ',
277+
'Accept-Language':'',
278+
'x': 'X-relayer-NoHost:localhost:8000'
279+
};
280+
var extraHeaders2 = {
281+
'X-Relayer-Protocol':'https',
282+
'X-Relayer-Header': [
283+
encodeURIComponent('X: X-relayer-NoHost:localhost:8000'),
284+
encodeURIComponent('Fake-User-Agent:Mozilla/5.0 (Macintosh++; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 '),
285+
encodeURIComponent('Accept-Language:')
286+
].join(', ')};
233287

234288
var dataSetHTTPS = [
235-
{protocol : 'HTTPS', method: 'GET', path: '/', headers: extraHeaders, body: {},
236-
name : '1 Should accept the request using ', responseHeaders: responseHeaders},
237-
{protocol : 'HTTPS', method: 'POST', path: '/', headers: extraHeaders, body: {},
238-
name : '2 Should accept the request using ', responseHeaders: responseHeaders},
239-
{protocol : 'HTTPS', method: 'PUT', path: '/', headers: extraHeaders, body: {},
240-
name : '3 Should accept the request using ', responseHeaders: responseHeaders},
241-
{protocol : 'HTTPS', method: 'DELETE', path: '/', headers: extraHeaders, body: {},
242-
name : '4 Should accept the request using ', responseHeaders: responseHeaders}
289+
{protocol : 'HTTPS', method: 'GET', path: '/', headers: extraHeaders1, body: {},
290+
name : '1 Should accept the request using ', responseHeaders: responseHeaders1},
291+
{protocol : 'HTTPS', method: 'POST', path: '/', headers: extraHeaders1, body: {},
292+
name : '2 Should accept the request using ', responseHeaders: responseHeaders1},
293+
{protocol : 'HTTPS', method: 'PUT', path: '/', headers: extraHeaders1, body: {},
294+
name : '3 Should accept the request using ', responseHeaders: responseHeaders1},
295+
{protocol : 'HTTPS', method: 'DELETE', path: '/', headers: extraHeaders1, body: {},
296+
name : '4 Should accept the request using ', responseHeaders: responseHeaders1},
297+
{protocol : 'HTTPS', method: 'GET', path: '/', headers: extraHeaders2, body: {},
298+
name : '5 Should accept the request using ', responseHeaders: responseHeaders2},
299+
{protocol : 'HTTPS', method: 'POST', path: '/', headers: extraHeaders2, body: {},
300+
name : '6 Should accept the request using ', responseHeaders: responseHeaders2},
301+
{protocol : 'HTTPS', method: 'PUT', path: '/', headers: extraHeaders2, body: {},
302+
name : '7 Should accept the request using ', responseHeaders: responseHeaders2},
303+
{protocol : 'HTTPS', method: 'DELETE', path: '/', headers: extraHeaders2, body: {},
304+
name : '8 Should accept the request using ', responseHeaders: responseHeaders2}
243305
];
244306

245307
for(i=0; i < dataSetHTTPS.length; i++){
@@ -335,6 +397,33 @@ describe('Single Feature: Extra header ' + '#FEH', function() {
335397
}
336398
});
337399

400+
describe('Retrieve request with an invalid ExtraHeader #FEH', function () {
401+
402+
var INVALID_HEADER = 'Accept-Value';
403+
404+
var extraHeaders = {
405+
'X-Relayer-Header': [
406+
encodeURIComponent('X: X-relayer-NoHost:localhost:8000'),
407+
encodeURIComponent('Fake-User-Agent:Mozilla/5.0 (Macintosh++; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 '),
408+
encodeURIComponent(INVALID_HEADER)
409+
].join(', ')};
410+
411+
var dataSetHTTP = [
412+
{protocol : 'http', method: 'GET', path: '/', headers: extraHeaders, body: {},
413+
name : '1 Should not accept the request: ' + INVALID_HEADER + ' has not a defined value using ', invalidHeader: INVALID_HEADER },
414+
{protocol : 'http', method: 'POST', path: '/', headers: extraHeaders, body: {},
415+
name : '2 Should not accept the request: ' + INVALID_HEADER + ' has not a defined value using ', invalidHeader: INVALID_HEADER },
416+
{protocol : 'http', method: 'PUT', path: '/', headers: extraHeaders, body: {},
417+
name : '3 Should not accept the request: ' + INVALID_HEADER + ' has not a defined value using ', invalidHeader: INVALID_HEADER },
418+
{protocol : 'http', method: 'DELETE', path: '/', headers: extraHeaders, body: {},
419+
name : '4 Should not accept the request: ' + INVALID_HEADER + ' has not a defined value using ', invalidHeader: INVALID_HEADER }
420+
];
421+
422+
for(i=0; i < dataSetHTTP.length; i++){
423+
_invalidHeadersValue(dataSetHTTP[i]); //Launch every test in data set
424+
}
425+
});
426+
338427

339428
});
340429

0 commit comments

Comments
 (0)