Skip to content
This repository
tag: 1.0.1
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 67 lines (55 sloc) 2.421 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
require 'test/spec'
require 'rack/mock'

begin
  require 'csshttprequest'
  require 'rack/contrib/csshttprequest'

  context "Rack::CSSHTTPRequest" do

    before(:each) do
      @test_body = '{"bar":"foo"}'
      @test_headers = {'Content-Type' => 'text/plain'}
      @encoded_body = CSSHTTPRequest.encode(@test_body)
      @app = lambda { |env| [200, @test_headers, [@test_body]] }
    end

    specify "env['csshttprequest.chr'] should be set to true when \
PATH_INFO ends with '.chr'" do
      request = Rack::MockRequest.env_for("/blah.chr", :lint => true, :fatal => true)
      Rack::CSSHTTPRequest.new(@app).call(request)
      request['csshttprequest.chr'].should.equal true
    end

    specify "env['csshttprequest.chr'] should be set to true when \
request parameter _format == 'chr'" do
      request = Rack::MockRequest.env_for("/?_format=chr", :lint => true, :fatal => true)
      Rack::CSSHTTPRequest.new(@app).call(request)
      request['csshttprequest.chr'].should.equal true
    end

    specify "should not change the headers or response when !env['csshttprequest.chr']" do
      request = Rack::MockRequest.env_for("/", :lint => true, :fatal => true)
      status, headers, response = Rack::CSSHTTPRequest.new(@app).call(request)
      headers.should.equal @test_headers
      response.join.should.equal @test_body
    end

    context "when env['csshttprequest.chr']" do
      before(:each) do
        @request = Rack::MockRequest.env_for("/",
          'csshttprequest.chr' => true, :lint => true, :fatal => true)
      end

      specify "should modify the content length to the correct value" do
        headers = Rack::CSSHTTPRequest.new(@app).call(@request)[1]
        headers['Content-Length'].should.equal @encoded_body.length.to_s
      end

      specify "should modify the content type to the correct value" do
        headers = Rack::CSSHTTPRequest.new(@app).call(@request)[1]
        headers['Content-Type'].should.equal 'text/css'
      end

      specify "should not modify any other headers" do
        headers = Rack::CSSHTTPRequest.new(@app).call(@request)[1]
        headers.should.equal @test_headers.merge({
          'Content-Type' => 'text/css',
          'Content-Length' => @encoded_body.length.to_s
        })
      end
    end

  end
rescue LoadError => boom
  STDERR.puts "WARN: Skipping Rack::CSSHTTPRequest tests (nbio-csshttprequest not installed)"
end
Something went wrong with that request. Please try again.