我正在尝试设置一些我在网上找到的SailsJS样板。我的代码如下,我认为这是由于我在routes
中设置的配置所致:
'GET /.temporary/csrf/token/for/tests': { action: 'security/grant-csrf-token' }
api/controllers/
中security/grant-csrf-token
中没有任何东西,我是否必须生成这样的东西?
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
时,出现以下错误:
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)
答案 0 :(得分:1)
将路由定义为GET //.temporary/csrf/token/for/tests
时,从GET /.temporary/csrf/token/for/tests
检索请求时测试失败
因此,您肯定会输入错误的URL。这是在您使用助手时发生的
sails.helpers.http.sendHttpRequest.with({
method: 'GET',
url: '/.temporary/csrf/token/for/tests',
baseUrl: sails.config.custom.baseUrl
})
我猜您在/
中有baseUrl
,或者您将其添加到了助手中。
所以您只需要调用正确的URL