柴response.body始终为空{} [英] Chai response.body is always empty {}

查看:415
本文介绍了柴response.body始终为空{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我的服务器实际返回什么,当我断言response.body时,Chai都会给我这个异常:

No matter what my server actually returns, Chai always gives me this exception when I assert response.body:

未捕获的AssertionError:期望{}与测试"高度相等

Uncaught AssertionError: expected {} to deeply equal 'test'

即使实际的服务器响应是测试",也不是{}:

Even though the actual server response is 'test', not {}:

这是我的考试:

const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();    
chai.use(chaiHttp);

describe('GET /test', () => {
  it('it should give test result', (done) => {
    chai.request(server)
        .get('/test')
        .end((err, res) => {
            console.log(err); // outputs null
            console.log(res); // outputs normal-looking response
            res.body.should.be.eql('test');
            done();
        });
  });
});

这是我的服务器(test-server.js):

Here is my server (test-server.js):

const http = require('http');
const server = http.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end('test');
});

module.exports = server;

server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");

我在做什么错了?

推荐答案

内容类型:application/json

res.body根据Content-Type标头填充

Content-Type: application/json

res.body is populated depending on Content-Type header

test-server.js

test-server.js

const http = require('http');
const server = http.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "application/json"});

    var b = JSON.stringify({
      name: 'asad',
      class: 'paewe'
    });

    response.end(b);
});

module.exports = server;

server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");

res.body将包含已分析的对象

test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);

describe('GET /test', () => {
    it('it should give test result', (done) => {
        chai.request(server)
            .get('/test')
            .end((err, res) => {
                console.log(err); // outputs null
                console.log(res); // outputs normal-looking response
                console.log(res.body) // { name: 'asad', class: 'paewe' }

                var checkObj = {
                    name: 'asad',
                    class: 'paewe'
                }
                res.body.should.be.eql(checkObj); // passes test
                done();
            });
    });
});

--------------------------------------------------- -------------------------------------------------- -------

内容类型:text/plain

如果Content-Type标头为text/plain,则不会将响应正文解析为任何内容,但是res.text将包含数据作为字符串

--------------------------------------------------------------------------------------------------------

Content-Type: text/plain

If the Content-Type header is text/plain, then the response body won't be parsed as anything, but res.text will contain the data as string

test-server.js

test-server.js

const http = require('http');
const server = http.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end('test');
});

module.exports = server;

server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");

test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();    
chai.use(chaiHttp);

describe('GET /test', () => {
  it('it should give test result', (done) => {
    chai.request(server)
        .get('/test')
        .end((err, res) => {
            console.log(err); // outputs null
            console.log(res); // outputs normal-looking response
            console.log(res.body) // {}
            res.text.should.be.eql('test'); // passes test
            done();
        });
  });
});

一些参考

  1. https://github.com/visionmedia/superagent/issues/990
  2. https://github.com/visionmedia/supertest/pull/10
  3. https://github.com/visionmedia/supertest/issues/68
  1. https://github.com/visionmedia/superagent/issues/990
  2. https://github.com/visionmedia/supertest/pull/10
  3. https://github.com/visionmedia/supertest/issues/68

这篇关于柴response.body始终为空{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆