Search code examples
node.jssuperagent

Query parameter using superagent


I'm creating a small Node.js application which calls some REST api´s using Superagent (v5.1.2). I need to send a filter option with a get request.

The API endpoint needs the following structure:

endpoint-url/test?filter=_name eq 'testname'

I´m struggling to achieve this result using superagent with the built-in query method. When I send the request I´ll get all items returned, so the filter option isn't making any effect. Once I test it via postman I´ll get just the specified item returned with the _name = 'testname'.

Here is my code snippet

let name = 'testname';
superagent.get('endpoint-url/test')
           .authBearer(token)
           .query({'filter': '_name eq ' + name})
           .then(res => {
               ...
           })
           .catch(err => {
               ...
           });

Solution

  • Here is a minimal working example:

    app.js:

    const express = require("express");
    const app = express();
    
    const memoryDB = {
      users: [{ name: "testname" }, { name: "haha" }],
    };
    
    app.get("/test", (req, res) => {
      console.log(req.query);
      const filterName = req.query.filter.split("eq")[1].trim();
      const user = memoryDB.users.find((user) => user.name === filterName);
      res.status(200).json(user);
    });
    
    module.exports = app;
    

    app.test.js:

    const app = require("./app");
    const superagent = require("superagent");
    const { expect } = require("chai");
    
    describe("59246379", () => {
      let server;
      before((done) => {
        server = app.listen(3003, () => {
          console.info(`HTTP server is listening on http://localhost:${server.address().port}`);
          done();
        });
      });
    
      after((done) => {
        server.close(done);
      });
      it("should pass", () => {
        let name = "testname";
        return superagent
          .get("http://localhost:3003/test")
          .query({ filter: "_name eq " + name })
          .then((res) => {
            expect(res.status).to.be.equal(200);
            expect(res.body).to.be.eql({ name: "testname" });
          });
      });
    });
    

    Integration test result with coverage report:

      59246379
    HTTP server is listening on http://localhost:3003
    { filter: '_name eq testname' }
        ✓ should pass (46ms)
    
    
      1 passing (57ms)
    
    -------------|----------|----------|----------|----------|-------------------|
    File         |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
    -------------|----------|----------|----------|----------|-------------------|
    All files    |      100 |      100 |      100 |      100 |                   |
     app.js      |      100 |      100 |      100 |      100 |                   |
     app.test.js |      100 |      100 |      100 |      100 |                   |
    -------------|----------|----------|----------|----------|-------------------|
    

    Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59246379