【问题标题】:Trying to make Supertest send a body to https post?试图让 Supertest 将正文发送到 https 帖子?
【发布时间】:2018-05-15 20:16:13
【问题描述】:

我是新手,无法让超测为我工作。我想知道:

  • 为什么主体未定义?
  • 命令行中是否有技巧可以在控制台中显示和检查对象?
  • 为什么测试不记录“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)

请注意,证书(不包括在内)是自签名的。

【问题讨论】:

    标签: node.js express https supertest


    【解决方案1】:

    在隔离上面的代码时,我错过了选项。因此,它不使用 SSL。很抱歉。

    我在我的测试用例中重新启动服务器并使用客户端。为此我必须解决:

    1. CORS
    2. The actual problem of this post by using a body parser

    【讨论】:

      【解决方案2】:

      这是由于自签名证书。

      我也遇到了类似的问题,有两种可能的解决方案

      1. 为测试环境创建http服务器而不是https服务器
      2. supertest 替换为superrequest npm 包并将strictSsl 设置为false。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-02
        • 1970-01-01
        • 2010-11-12
        • 1970-01-01
        • 2013-02-25
        • 2020-12-23
        • 2020-03-21
        相关资源
        最近更新 更多