npm运行自定义测试-sendHttpRequest失败non200Response [英] npm run custom-tests - sendHttpRequest failed non200Response

查看:112
本文介绍了npm运行自定义测试-sendHttpRequest失败non200Response的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一些我在网上找到的SailsJS样板.我的代码在下面,我认为这是由于我在设置的routes中的配置所致:

I'm trying to setup some SailsJS boilerplate I'm finding on the web. My code is below, I am thinking it is due to my config in routes where I set:

'GET /.temporary/csrf/token/for/tests': { action: 'security/grant-csrf-token' }

api/controllers/中我没有任何东西,对于security/grant-csrf-token,我是否必须生成这样的东西?

I don't have any thing in api/controllers/ for security/grant-csrf-token, do I have to generate such a thing?

const sails = require('sails');

before(function(done) {

  // Increase the Mocha timeout so that Sails has enough time to lift, even if you have a bunch of assets.
  this.timeout(10000);

  try {

    // Note that we mix in env vars, CLI opts, and the .sailsrc file using
    // the `.getRc()` method, if possible.  But also note that we mix in
    // a few additional overrides to remove clutter from test output, ensure
    // we are working with a clean database, etc.
    const configOverrides = sails.getRc();

    sails.lift(
      {
        // Your sails app's configuration files will be loaded automatically,
        // but you can also specify any other special overrides here for testing purposes.

        // For example, we might want to skip the Grunt hook,
        // and disable all logs except errors and warnings:
        ...configOverrides,
        hooks: {
          ...configOverrides.hooks,
          grunt: false
        },
        models: {
          ...configOverrides.models,
          migrate: 'drop'
        },
        log: {
          ...configOverrides.log,
          level: 'warn'
        },
        routes: {
          ...configOverrides.routes,
          // Provide a way to get a CSRF token:
          'GET /.temporary/csrf/token/for/tests': { action: 'security/grant-csrf-token' }
        },
        policies: {
          ...configOverrides.policies,
          // Poke a hole in any global policies to ensure the test runner can
          // actually get access to a CSRF token.
          'security/grant-csrf-token': true,
        },
        datastores: {
          ...configOverrides.datastores,
          default: {
          ...(configOverrides.datastores || {}).default
          // To have the tests run against a local mysql database, for example,
          // add configuration here:  (e.g. uncomment the two lines below)
          // adapter: 'sails-mysql',
          // url: 'mysql://root@127.0.0.1:3306/pba',
          }
        }
      },
      err => {

        if (err) return done(err);

        // First, get a cookie and a CSRF token.
        sails.helpers.http.sendHttpRequest.with({
          method: 'GET',
          url: '/.temporary/csrf/token/for/tests',
          baseUrl: sails.config.custom.baseUrl
        }).exec((err, serverResponse) => {

          if (err) return done(new Error('Test runner could not fetch CSRF token.\nDetails:\n' + err.stack));

当我运行npm run custom-test时,出现以下错误:

When I run npm run custom-test this I get the following error:

PS C:\Users\Mercurius\Documents\GitHub\Homie-Web> npm run custom-tests

> homie@0.0.0 custom-tests C:\Users\Mercurius\Documents\GitHub\Homie-Web
> node ./node_modules/mocha/bin/mocha test/lifecycle.test.js test/integration/**/*.test.js



 - GET //.temporary/csrf/token/for/tests      (15ms 404)
  1) "before all" hook

  0 passing (3s)
  1 failing

  1) "before all" hook:
     Test runner could not fetch CSRF token.
Details:
Exception: `sendHttpRequest` failed ("non200Response").  A non-2xx status code was returned from the server.
Server response:

{ statusCode: 404,
  headers:
   { 'x-powered-by': 'Sails <sailsjs.com>',
     'content-type': 'text/plain; charset=utf-8',
     'content-length': '9',
     etag: 'W/"9-0gXL1ngzMqISxa6S1zx3F4wtLyg"',
     'set-cookie':
      [ 'sails.sid=s%3AbfUzLBhmfPZ7EsQM9kSV4Bg2y_s0lA0e.b382JvWtrDt1pZhRuafObxxkoqwQaaFlOPqXrYmzNn0; Path=/; HttpOnly' ],
     date: 'Wed, 05 Dec 2018 17:26:50 GMT',
     connection: 'close' },
  body: 'Not Found' }
    at sails.lift.err (C:\Users\Mercurius\Documents\GitHub\Homie-Web\test\lifecycle.test.js:64:48)
    at whenSailsIsReady (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\sails\lib\app\lift.js:127:12)
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:3861:9
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:421:16
    at replenish (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:941:25)
    at iterateeCallback (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:931:17)
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:906:16
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:3858:13
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\sails\lib\app\private\initialize.js:91:14
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:421:16
    at iteratorCallback (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:998:13)
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:906:16
    at expressListening (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\sails\lib\hooks\http\start.js:169:14)
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:421:16
    at processQueue (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:1565:20)
    at taskComplete (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:1588:9)
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:1612:17
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:906:16
    at async.auto.verify (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\sails\lib\hooks\http\start.js:160:9)
    at runTask (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:1619:13)
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:1559:13
    at processQueue (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:1569:13)
    at taskComplete (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:1588:9)
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:1612:17
    at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\async\dist\async.js:906:16
    at Server.<anonymous> (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\sails\lib\hooks\http\start.js:38:20)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
    at Server.emit (events.js:208:7)
    at emitListeningNT (net.js:1387:10)
    at _combinedTickCallback (internal/process/next_tick.js:136:11)
    at process._tickCallback (internal/process/next_tick.js:181:9)

推荐答案

在将路由定义为GET /.temporary/csrf/token/for/tests

因此,您肯定会输入错误的URL.这是在您使用助手时发生的

So you definitely call a wrong URL. It happens while you're using a helper

sails.helpers.http.sendHttpRequest.with({
  method: 'GET',
  url: '/.temporary/csrf/token/for/tests',
  baseUrl: sails.config.custom.baseUrl
})

我猜您是在baseUrl中包含了/,还是将其添加到了助手中.

I guess you either has / in your baseUrl or you add it inside a helper.

所以您只需要调用正确的URL

So what you need is just to call the right URL

这篇关于npm运行自定义测试-sendHttpRequest失败non200Response的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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