0

我有一个Express 4 应用程序,它使csurf用户在 API 路由上进行 CSRF 保护。该应用程序运行良好,CSRF 保护确实在没有csrf-token标头的请求会给出适当的错误的情况下工作。

我使用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)
})

我已经验证了请求中确实设置了标头。任何对可行的替代库的想法或建议表示赞赏。

4

1 回答 1

0

我以前在supertest登录和登录时也遇到过错误,但仍未解决,但使用supertest-session似乎已经为我解决了这个问题。修复是替换:

import request from 'supertest'

import request from 'supertest-session'

一切都神奇地起作用。

于 2017-06-29T14:36:29.130 回答