All files / lib/httpadapter httpadapter.js

80% Statements 32/40
55.56% Branches 10/18
62.5% Functions 5/8
80% Lines 32/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99    9x 9x             9x 21x 18x     21x 21x 21x                 9x   3x 3x               3x 2x 2x 2x     2x       3x 1x 1x 1x 1x       1x 1x 1x                       3x 1x     3x             3x   3x               3x     9x       9x  
'use strict';
 
var HttpError = require('../error/httperror.js');
var querystring = require('querystring');
 
/**
* HttpAdapter
* @param <object>   http      an optional http instance to use
* @param <object>   options   additional options to set on the request
*/
var HttpAdapter = function(http, options) {
  if (!http || http === 'undefined') {
    http = require('http');
  }
 
  this.url = require('url');
  this.http = http;
  this.options = options;
};
 
/**
* Geocode
* @param <string>   url      Webservice url
* @param <array>    params   array of query string parameters
* @param <function> callback Callback method
*/
HttpAdapter.prototype.get = function(url, params, callback) {
 
  var urlParsed = this.url.parse(url);
  var options = {
    host: urlParsed.host,
    path: urlParsed.path + '?' + querystring.stringify(params),
    headers: {
      'user-agent': 'Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0'
    }
  };
 
  if (this.options) {
    for (var k in this.options) {
      var v = this.options[k];
      Iif (!v) {
        continue;
      }
      options[k] = v;
    }
  }
 
  var request = this.http.request(options, function(response) {
    var str = '';
    var contentType = response.headers['content-type'];
    response.on('data', function(chunk) {
      str += chunk;
    });
 
    //the whole response has been recieved, so we just print it out here
    response.on('end', function() {
      Eif (response.statusCode !== 200) {
        return callback(new Error('Response status code is ' + response.statusCode), null);
      }
 
      if (contentType !== undefined && contentType.indexOf('application/json') >= 0) {
        callback(false, JSON.parse(str));
      } else {
        callback(false, str);
      }
 
    });
  });
 
  if(typeof options.timeout !== 'undefined') {
    request.setTimeout(options.timeout);
  }
 
  var onError = function(err) {
    var error = err instanceof HttpError ? err : new HttpError(err.message, {
      code: err.code
    });
    callback(error, null);
  };
 
  request.on('error', onError);
 
  request.on('timeout', function() {
    onError(new HttpError('connect ETIMEDOUT', {
      code: 'ETIMEDOUT',
      errno: 'ETIMEDOUT',
      syscall: 'connect'
    }));
  });
 
  request.end();
};
 
HttpAdapter.prototype.supportsHttps = function() {
  return false;
};
 
module.exports = HttpAdapter;