TT#5036 Added debug logger for requests. Normalize url and path via makeUrl.

Change-Id: If2113fbd715912e30ef7ce8b0bf7e0ebe5352dd5
changes/14/9214/1
Hans-Peter Herzog 10 years ago
parent 3fd7c74359
commit 53d1c92e7b

@ -1,5 +1,6 @@
'use strict';
var url = require('url');
var validator = require('validator');
var _ = require('lodash');
var createId = require('uuid').v4;
@ -7,6 +8,7 @@ var assert = require('chai').assert;
var userAgent = require('superagent');
var Response = require('./response').Response;
var ResponseError = require('./error').ResponseError;
var logger = require('debug-logger')('janus:admin');
/**
* @class
@ -15,15 +17,15 @@ class Admin {
constructor(options) {
assert.property(options, 'url', 'Missing option url');
assert(validator.isURL(options.url), 'Invalid url');
assert(validator.isURL(options.url), 'Invalid url', options.url);
assert.property(options, 'secret');
this.url = options.url;
this.url = url.parse(options.url);
this.secret = options.secret;
this.userAgent = options.userAgent || userAgent;
}
makeUrl(path) {
return this.url + path;
return this.url.format() + path.replace(/^\//g, '');
}
request(req, options) {
@ -31,10 +33,12 @@ class Admin {
var path = _.get(options, 'path', '/admin');
req.admin_secret = this.secret;
req.transaction = createId();
logger.debug('Request', req);
this.userAgent.post(this.makeUrl(path)).type('json').send(req).end((err, res)=>{
if(_.isObject(err)) {
return reject(err);
}
logger.debug('Response', res.body);
var response = new Response(req, res.body);
if(response.isSuccess()) {
resolve(response);

@ -27,6 +27,24 @@ describe('Admin', function() {
});
});
it('should make url by given path', function(done){
var url = 'http://localhost:7088';
var finalUrl = url + '/path';
var adminClient1 = new Admin({
url: url,
secret: adminServer.getAdminSecret()
});
var adminClient2 = new Admin({
url: url + '/',
secret: adminServer.getAdminSecret()
});
assert(adminClient1.makeUrl('/path'), finalUrl);
assert(adminClient1.makeUrl('path'), finalUrl);
assert(adminClient2.makeUrl('/path'), finalUrl);
assert(adminClient2.makeUrl('path'), finalUrl);
done();
});
it('should throw an error caused by missing option url', function(done){
try {
adminClient = new Admin({

Loading…
Cancel
Save