rack.git  about / heads / tags
a modular Ruby webserver interface
blob daf75355dd473d47d0822a81b61dfa6bcd1abafd 1500 bytes (raw)
$ git show webrick-devdep:test/spec_content_type.rb	# shows this blob on the CLI

 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
 
require 'minitest/autorun'
require 'rack/content_type'
require 'rack/lint'
require 'rack/mock'

describe Rack::ContentType do
  def content_type(app, *args)
    Rack::Lint.new Rack::ContentType.new(app, *args)
  end

  def request
    Rack::MockRequest.env_for
  end

  it "set Content-Type to default text/html if none is set" do
    app = lambda { |env| [200, {}, "Hello, World!"] }
    headers = content_type(app).call(request)[1]
    headers['Content-Type'].must_equal 'text/html'
  end

  it "set Content-Type to chosen default if none is set" do
    app = lambda { |env| [200, {}, "Hello, World!"] }
    headers =
      content_type(app, 'application/octet-stream').call(request)[1]
    headers['Content-Type'].must_equal 'application/octet-stream'
  end

  it "not change Content-Type if it is already set" do
    app = lambda { |env| [200, {'Content-Type' => 'foo/bar'}, "Hello, World!"] }
    headers = content_type(app).call(request)[1]
    headers['Content-Type'].must_equal 'foo/bar'
  end

  it "detect Content-Type case insensitive" do
    app = lambda { |env| [200, {'CONTENT-Type' => 'foo/bar'}, "Hello, World!"] }
    headers = content_type(app).call(request)[1]
    headers.to_a.select { |k,v| k.downcase == "content-type" }.
      must_equal [["CONTENT-Type","foo/bar"]]
  end

  it "not set Content-Type on 304 responses" do
    app = lambda { |env| [304, {}, []] }
    response = content_type(app, "text/html").call(request)
    response[1]['Content-Type'].must_be_nil
  end
end

git clone https://yhbt.net/rack.git