Skip to content

Two templates needed for stubbing Cross-origin request #22

@asehra

Description

@asehra

When a site makes a cross-origin ajax request, the browser makes 2 calls to the destination URL under the hood. The first call is a request with the OPTIONS HTTP verb, to which the webserver sends back appropriate Access-Control headers which determine whether the webserver will accept the next request. This is followed by the actual request.

When mocking such a cross origin request, we need to populate mirage with two templates. Below is an example of what we had to do for a simple POST request for example:

  mirage.put('/api/endpoint', "") do
    http_method :options
    headers "Access-Control-Allow-Headers" => "Origin, X-Requested-With, Content-Type",
            "Access-Control-Allow-Methods" => "POST",
            "Access-Control-Allow-Origin" => "*"
  end

  mirage.put('/api/endpoint', {response: :body}.to_json) do
    http_method :post
  end

Might be worth having a DSL method for doing it in one go. If not then might be worth having it somewhere in the README as a recipe for front-end cross-origin testing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions