2017-06-29 52 views
0

我有一個Express 4個應用程序,使用戶csurf CSRF保護API路由。該應用程序運行良好,並且CSRF保護確實在沒有csrf-token標頭的請求會提供適當的錯誤。CSRF Supertest請求失敗

我利用Ava用於測試supertest測試路線。當啓用CSRF檢查下面的測試失敗,但如果通過我刪除中間件:

test('booking api no auth', async t => { 
    t.plan(4) 

    const server = await request(makeServer(t.context.config, t.context.connection)) 

    const csrf = await server 
    .get('/') 
    .then(res => new JSDOM(res.text)) 
    .then(dom => dom.window.document.querySelector('meta[name="csrf_token"]')) 
    .then(csrfMeta => csrfMeta.getAttribute('content')) 

    const GET = await server 
    .get('/v2/Booking') 
    .set('csrf-token', csrf) 

    const POST = await server 
    .post('/v2/Booking') 
    .set('csrf-token', csrf) 
    .send({ 
     name: 'Test', 
     description: 'Test', 
     category: 'diving', 
     minimumPax: 1, 
     maximumPax: 2, 
     priceAdult: 1, 
     priceChild: 1 
    }) 

    const res = { GET, POST } 

    t.is(res.GET.status, 403) 
    t.deepEqual(res.GET.body, text['403']) 
    t.is(res.POST.status, 201) 
    t.truthy(res.POST.body._id) 
}) 

我已驗證的頭在請求中確實設置。任何意見或建議的替代圖書館的作品表示讚賞。

回答

0

我以前也有錯誤supertest和登錄,仍然沒有解決,但使用supertest-session似乎已經解決了這個問題。修復程序替換:

import request from 'supertest' 

import request from 'supertest-session' 

,一切都神奇地運行。

相關問題