【问题标题】:Can't test OPTIONS method using mocha and supertest无法使用 mocha 和 supertest 测试 OPTIONS 方法
【发布时间】:2015-08-21 19:26:01
【问题描述】:

相关问题Cant test DELETE method using mocha and supertest 及其当前的两个答案并没有解决我的问题:(

以下是测试定义:

api = supertest(url);

describe('when OPTIONS', function () {
    it('should return only method GET', function (done) {
      api
        .options('/')
        .expect('Allow', 'GET')
        .expect(200, done);
    });
  });

它一直触发以下错误:

Uncaught TypeError: Cannot read property 'header' of undefined
      at _stream_readable.js:944:16

以及何时:

 api
        .options('/')
        .end(function(error,res){
          if (error) return done(error);
          done()
        });

上面写着:

SyntaxError: Unexpected token G
      at Object.parse (native)
      at _stream_readable.js:944:16

我已经尝试了很多东西但无法完成这项工作。

我正在使用 supertest ^1.0.1,它在版本 ~0.13.0 上运行良好。

https://github.com/visionmedia/supertest/issues/272的相关问题

谢谢

【问题讨论】:

  • 我无法复制。您确定您的 API 的响应有效吗?
  • @robertklep 感谢您的评论,这对于检测问题非常有用。

标签: javascript node.js testing mocha.js supertest


【解决方案1】:

我已经检测到这个问题,我在 API 中返回了一个带有 Content-Type: application/json 的 OPTIONS 响应,但正文是无效的 JSON(它是 GET、HEAD)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    相关资源
    最近更新 更多