Sha256: bb742fdcd2e9cf22df06ebf19d5549907d7cadf0b25821d95bd182d15d454159

Contents?: true

Size: 1.56 KB

Versions: 12

Compression:

Stored size: 1.56 KB

Contents

require "spec_helper"

describe Pliny::Middleware::CORS do
  def app
    Rack::Builder.new do
      use Rack::Lint
      use Pliny::Middleware::CORS
      run Sinatra.new {
        get "/" do
          "hi"
        end
      }
    end
  end

  it "doesn't do anything when the Origin header is not present" do
    get "/"
    assert_equal 200, last_response.status
    assert_equal "hi", last_response.body
    assert_nil last_response.headers["Access-Control-Allow-Origin"]
  end

  it "intercepts OPTION requests to render a stub (preflight request)" do
    header "Origin", "http://localhost"
    options "/"
    assert_equal 200, last_response.status
    assert_equal "", last_response.body
    assert_equal "GET, POST, PUT, PATCH, DELETE, OPTIONS",
      last_response.headers["Access-Control-Allow-Methods"]
    assert_equal "http://localhost",
      last_response.headers["Access-Control-Allow-Origin"]
  end

  it "delegates other calls, adding the CORS headers to the response" do
    header "Origin", "http://localhost"
    get "/"
    assert_equal 200, last_response.status
    assert_equal "hi", last_response.body
    assert_equal "http://localhost",
      last_response.headers["Access-Control-Allow-Origin"]
  end

  it "allows additional headers to be added to every response" do
    Pliny::Middleware::CORS.add_additional_header("X-Origin")

    header "Origin", "http://localhost"
    get "/"
    assert_equal 200, last_response.status
    assert_equal "hi", last_response.body

    assert last_response.headers["Access-Control-Allow-Headers"].include?("X-Origin")
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pliny-1.1.0 spec/middleware/cors_spec.rb
pliny-1.0.0 spec/middleware/cors_spec.rb
pliny-0.32.0 spec/middleware/cors_spec.rb
pliny-0.31.0 spec/middleware/cors_spec.rb
pliny-0.30.1 spec/middleware/cors_spec.rb
pliny-0.30.0 spec/middleware/cors_spec.rb
pliny-0.29.0 spec/middleware/cors_spec.rb
pliny-0.28.0 spec/middleware/cors_spec.rb
pliny-0.27.1 spec/middleware/cors_spec.rb
pliny-0.27.0 spec/middleware/cors_spec.rb
pliny-0.26.2 spec/middleware/cors_spec.rb
pliny-0.26.1 spec/middleware/cors_spec.rb