npm run custom-tests - sendHttpRequest failed non200Response

泄露秘密 提交于 2020-01-16 16:11:00

问题


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' }

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

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)

回答1:


You test fails on retrieving request from GET //.temporary/csrf/token/for/tests while you define your route as GET /.temporary/csrf/token/for/tests

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

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

So what you need is just to call the right URL



来源:https://stackoverflow.com/questions/53637743/npm-run-custom-tests-sendhttprequest-failed-non200response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!