{"size": 1344, "ext": "js", "max_stars_repo_path": "spec/browser/middleware/csrf.spec.js", "max_stars_repo_name": "ollelindeman/mappersmith", "max_stars_repo_stars_event_min_datetime": "2015-01-16T21:59:35.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-25T05:13:03.000Z", "max_issues_repo_path": "spec/browser/middleware/csrf.spec.js", "max_issues_repo_name": "ollelindeman/mappersmith", "max_issues_repo_issues_event_min_datetime": "2015-01-06T18:29:44.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-16T09:55:54.000Z", "max_forks_repo_path": "spec/browser/middleware/csrf.spec.js", "max_forks_repo_name": "ollelindeman/mappersmith", "max_forks_repo_forks_event_min_datetime": "2015-04-13T09:49:55.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-03T15:12:07.000Z"}
{"max_stars_count": 313, "max_issues_count": 207, "max_forks_count": 85, "avg_line_length": 33.6, "max_line_length": 88, "alphanum_fraction": 0.6681547619}
import MethodDescriptor from 'src/method-descriptor'
import Request from 'src/request'
import CsrfMiddleware from 'src/middlewares/csrf'

describe('Middleware / CSRF', () => {
  let methodDescriptor, request, middleware

  beforeEach(() => {
    methodDescriptor = new MethodDescriptor({ method: 'get' })
    request = new Request(methodDescriptor)
  })

  it('exposes name', () => {
    expect(CsrfMiddleware().name).toEqual('CsrfMiddleware')
  })

  it('adds a header if cookie is set in document.cookie', async () => {
    middleware = CsrfMiddleware()()
    const token = 'eacb7710-3a75-49ab-a26a-cdffc5250f1c'
    document.cookie = 'csrfToken=eacb7710-3a75-49ab-a26a-cdffc5250f1c; _ga=GAxx'
    const newRequest = await middleware.prepareRequest(() => Promise.resolve(request))
    expect(newRequest.headers()['x-csrf-token']).toEqual(token)
  })

  describe('csrfToken is missing from document.cookie', () => {
    beforeEach(() => {
      const data = {}
      data.attr = data
      middleware = CsrfMiddleware('csrfToken', 'x-csrf-token')()
      request = new Request(methodDescriptor)
    })

    it('does not add "x-csrf-token"', async () => {
      document.cookie = 'csrfToken='
      const newRequest = await middleware.prepareRequest(() => Promise.resolve(request))
      expect(newRequest.headers()).toEqual({})
    })
  })
})
