gpt4 book ai didi

node.js - Chai response.body 始终为空 {}

转载 作者:行者123 更新时间:2023-12-02 06:50:32 27 4
gpt4 key购买 nike

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

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

即使实际的服务器响应是“test”,而不是 {}: enter image description here

这是我的测试:

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):

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 header 进行填充

测试服务器.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 将包含 Parsed 对象

测试.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();
});
});
});

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

内容类型:文本/纯文本

如果 Content-Type header 为 text/plain,则响应正文不会被解析为任何内容,但 res.text 将包含字符串形式的数据

测试服务器.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/");

测试.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

关于node.js - Chai response.body 始终为空 {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320699/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com