node.js - 试图让 Supertest 将正文发送到 https 帖子?
问题描述
我是新手,无法让超测试为我工作。我想知道:
- 为什么身体未定义?
- 命令行中是否有技巧可以在控制台中显示和检查对象?
- 为什么测试不记录“hello”?
"use strict";
const request = require('supertest');
const express = require('express');
const https = require('https');
const fs = require('fs');
const path = require('path');
const certPath = path.resolve(path.resolve(), './certs');
const app = express();
//This line is from the Node.js HTTPS documentation.
const options = {
key : fs.readFileSync(certPath+'/server-key.pem'),
cert : fs.readFileSync(certPath+'/server-crt.pem'),
ca : fs.readFileSync(certPath+'/ca-crt.pem')
};
// service
app.post('/failService', function(req, res) {
console.log('failService: '+req.body); // failService: undefined
res.send('hello');
});
describe('trial not working', function() {
it('responds with json', function(done) {
request(app)
.post('/failService')
.send({name: 'john'})
.set('Accept', /json/)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
console.log('response: '+res.body); // response: [object Object]
done();
});
});
});
.... 显示
$ mocha supertest.js
trial not working
failService: undefined
response: [object Object]
√ responds with json (125ms)
1 passing (171ms)
请注意,证书(不包括在内)是自签名的。
解决方案
这是由于自签名证书。
我也遇到了类似的问题,有两种可能的解决方案
- 为测试环境创建http服务器而不是https服务器
- 替换
supertest
为superrequest
npm 包并将 strictSsl 设置为 false。
推荐阅读
- java - 如何从服务获取响应的 JSON
- minishift - MiniShift 错误 virNetSocketReadWire:1806
- postgresql - 如何在postgresql中按特定条件按每个日期分组?
- excel - 根据另一工作簿中的数据从一个工作簿中删除行
- amazon-web-services - 如何扩展 Enet (UDP)?
- javascript - 在 JavaScript 中对同步函数使用 await
- php - PHP 从 SHA256 生成 JWT 令牌,缺少签名
- python - 如何将电视节目与超级玉米一起使用?
- git - git push to remote 错误:远程:警告:由于文件太多,跳过了不精确的重命名检测
- python - 安装“lightgbm”时出现很多错误