rack.git  about / heads / tags
a modular Ruby webserver interface
blob 4ab90c2efd486937fe1f400094e749bb466eb51c 1485 bytes (raw)
$ git show chunk:test/spec_head.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
47
48
49
50
51
 
# frozen_string_literal: true

require_relative 'helper'

separate_testing do
  require_relative '../lib/rack/head'
  require_relative '../lib/rack/lint'
  require_relative '../lib/rack/mock_request'
end

describe Rack::Head do

  def test_response(headers = {})
    body = StringIO.new "foo"
    app = lambda do |env|
      [200, { "content-type" => "test/plain", "content-length" => "3" }, body]
    end
    request = Rack::MockRequest.env_for("/", headers)
    response = Rack::Lint.new(Rack::Head.new(app)).call(request)

    return response, body
  end

  it "pass GET, POST, PUT, DELETE, OPTIONS, TRACE requests" do
    %w[GET POST PUT DELETE OPTIONS TRACE].each do |type|
      resp, _ = test_response("REQUEST_METHOD" => type)

      resp[0].must_equal 200
      resp[1].must_equal "content-type" => "test/plain", "content-length" => "3"
      resp[2].to_enum.to_a.must_equal ["foo"]
    end
  end

  it "remove body from HEAD requests" do
    resp, _ = test_response("REQUEST_METHOD" => "HEAD")

    resp[0].must_equal 200
    resp[1].must_equal "content-type" => "test/plain", "content-length" => "3"
    resp[2].to_enum.to_a.must_equal []
  end

  it "close the body when it is removed" do
    resp, body = test_response("REQUEST_METHOD" => "HEAD")
    resp[0].must_equal 200
    resp[1].must_equal "content-type" => "test/plain", "content-length" => "3"
    resp[2].to_enum.to_a.must_equal []
    body.wont_be :closed?
    resp[2].close
    body.must_be :closed?
  end
end

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