简体   繁体   English

使用超级代理查询参数

[英]Query parameter using superagent

I'm creating a small Node.js application which calls some REST api´s using Superagent (v5.1.2).我正在创建一个小的 Node.js 应用程序,它使用 Superagent (v5.1.2) 调用一些 REST api。 I need to send a filter option with a get request.我需要发送一个带有 get 请求的过滤器选项。

The API endpoint needs the following structure: API 端点需要以下结构:

endpoint-url/test?filter=_name eq 'testname'端点 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' .一旦我通过邮递员测试它,我将只得到返回的指定项目_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 => {
               ...
           });

Here is a minimal working example:这是一个最小的工作示例:

app.js : 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 : 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源代码: https : //github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59246379

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM