rack.git  about / heads / tags
a modular Ruby webserver interface
blob ca23134e0598ed424b5d68e8a3fa808755342b57 2835 bytes (raw)
$ git show no-unicorn:test/spec_show_status.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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
 
# frozen_string_literal: true

require 'minitest/global_expectations/autorun'
require 'rack/show_status'
require 'rack/lint'
require 'rack/mock'
require 'rack/utils'

describe Rack::ShowStatus do
  def show_status(app)
    Rack::Lint.new Rack::ShowStatus.new(app)
  end

  it "provide a default status message" do
    req = Rack::MockRequest.new(
      show_status(lambda{|env|
        [404, { "Content-Type" => "text/plain", "Content-Length" => "0" }, []]
    }))

    res = req.get("/", lint: true)
    res.must_be :not_found?
    res.wont_be_empty

    res["Content-Type"].must_equal "text/html"
    assert_match(res, /404/)
    assert_match(res, /Not Found/)
  end

  it "let the app provide additional information" do
    req = Rack::MockRequest.new(
      show_status(
        lambda{|env|
          env["rack.showstatus.detail"] = "gone too meta."
          [404, { "Content-Type" => "text/plain", "Content-Length" => "0" }, []]
    }))

    res = req.get("/", lint: true)
    res.must_be :not_found?
    res.wont_be_empty

    res["Content-Type"].must_equal "text/html"
    assert_match(res, /404/)
    assert_match(res, /Not Found/)
    assert_match(res, /too meta/)
  end

  it "escape error" do
    detail = "<script>alert('hi \"')</script>"
    req = Rack::MockRequest.new(
      show_status(
        lambda{|env|
          env["rack.showstatus.detail"] = detail
          [500, { "Content-Type" => "text/plain", "Content-Length" => "0" }, []]
    }))

    res = req.get("/", lint: true)
    res.wont_be_empty

    res["Content-Type"].must_equal "text/html"
    assert_match(res, /500/)
    res.wont_include detail
    res.body.must_include Rack::Utils.escape_html(detail)
  end

  it "not replace existing messages" do
    req = Rack::MockRequest.new(
      show_status(
        lambda{|env|
          [404, { "Content-Type" => "text/plain", "Content-Length" => "4" }, ["foo!"]]
    }))

    res = req.get("/", lint: true)
    res.must_be :not_found?

    res.body.must_equal "foo!"
  end

  it "pass on original headers" do
    headers = { "WWW-Authenticate" => "Basic blah" }

    req = Rack::MockRequest.new(
      show_status(lambda{|env| [401, headers, []] }))
    res = req.get("/", lint: true)

    res["WWW-Authenticate"].must_equal "Basic blah"
  end

  it "replace existing messages if there is detail" do
    req = Rack::MockRequest.new(
      show_status(
        lambda{|env|
          env["rack.showstatus.detail"] = "gone too meta."
          [404, { "Content-Type" => "text/plain", "Content-Length" => "4" }, ["foo!"]]
    }))

    res = req.get("/", lint: true)
    res.must_be :not_found?
    res.wont_be_empty

    res["Content-Type"].must_equal "text/html"
    res["Content-Length"].wont_equal "4"
    assert_match(res, /404/)
    assert_match(res, /too meta/)
    res.body.wont_match(/foo/)
  end
end

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