title: Testing API requests on localhost
body: <p>I'm using Mocha and Chai to develop testing scripts for my API in Node.js. I have an endpoint called /measurements. This endpoint should return statusCode 200.</p>
  
  <p>Here's how I trying to reach my goal:</p>
  
  <pre><code>var request = require("request");
  var chai = require("chai");
  var expect = chai.expect;
  
  
  // Criamos nosso primeiro caso de teste e fornecemos uma descricao utilizando describe
  describe("Teste monitora-sec", function () {
  
      it("Deve receber as medições", function (done) {
          request.get('localhost:3000/measurements')
              .expect(200)
              .end(function (err, res) {
                  expect(res.body).to.have.lengthOf(506);
                  done(err);
              });
      });
  });
  </code></pre>
  
  <p>When I run <code>mocha</code> on gitbash, I get this error:</p>
  
  <pre><code>Teste monitora-sec
      1) Deve receber as medições
  
  
  0 passing (34ms)
  1 failing
  
  1) Teste monitora-sec
      Deve receber as medições:
      Error: Invalid protocol: localhost:
      at Request.init (node_modules\request\request.js:458:31)
      at new Request (node_modules\request\request.js:127:8)
      at request (node_modules\request\index.js:53:10)
      at Function.get (node_modules\request\index.js:61:12)
      at Context.&lt;anonymous&gt; (test\measurement.spec.js:11:17)
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>[EDIT]</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Change the code a little bit:</p>
  
  <pre><code>var request = require("request");
  var chai = require("chai");
  var expect = chai.expect;
  var urlBase = "http://localhost:3000";
  
  describe("Teste monitora-sec", function () {
  
      it("Deve receber as medições", function (done) {
          request.get(
              {
                  url: urlBase + "/measurementsLast"
              },
              function (error, response, body) {
  
                  // precisamos converter o retorno para um objeto json
                  var _body = {};
                  try {
                      _body = JSON.parse(body);
                  }
                  catch (e) {
                      _body = {};
                  }
  
                  expect(response.statusCode).to.equal(200);
  
                  done(); // avisamos o test runner que acabamos a validacao e ja pode proseeguir
              }
          );
      });
  });
  </code></pre>
  
  <p>One of the problems was that I wrote just <code>localhost:3000</code>. Should be <code>http://localhost:3000</code></p>