local expect  = require 'spec.expect'
local cors    = require 'cors.cors'


describe("CORS", function()
  describe("when the status is not 404", function()
    it("does nothing", function()
      local request  = { method = 'GET', uri = '/'}
      local response = { status = 200, body = 'ok' }

      expect(cors):called_with(request, response)
        :to_pass(request)
        :to_return(response)
        :to_send_number_of_emails(0)
        :to_set_number_of_keys_in_middleware_bucket(0)
    end)
  end)

  describe("when the status is 404", function()
    it("sends an email and marks the middleware bucket", function()
      local request  = { method = 'GET', uri = '/'}
      local response = { status = 404, body = 'error' }

      expect(cors):called_with(request, response)
        :to_return(response)
        :to_set_in_middleware_bucket('last_mail')
        :to_send_email('YOUR-MAIL-HERE@gmail.com', 'A 404 has ocurred', 'a 404 error happened in http://localhost/')
    end)

    it("does not send two emails when called twice in rapid succession", function()
      local request  = { method = 'GET', uri = '/'}
      local response = { status = 404, body = 'error' }

      expect(cors)
        :called_with(request, response) -- not a typo, call the same request twice
        :called_with(request, response)
        :to_send_number_of_emails(1)
        :to_set_number_of_keys_in_middleware_bucket(1)
    end)

  end)
end)

local expect  = require 'spec.expect'
local cors    = require 'cors.cors'


describe("CORS", function()
  it("adds a header", function()
      local request           = { method = 'GET', uri = '/'}
      local backend_response  = { status = 200, body = 'ok' }
      local expected_response = { status = 200, body = 'ok', headers = {['Access-Control-Allow-Origin'] = "http://domain1.com http://domain2.com"}}

      expect(cors):called_with(request, backend_response)
        :to_pass(request)
        :to_receive(backend_response)
        :to_return(expected_response)
  end)
end)

Update CORS spec to reflect what it really does
Update CORS spec to reflect what it really does
