rack.git  about / heads / tags
a modular Ruby webserver interface
blob 2813ecd6fee0cb96ccb5a4eb7969aef531f832b0 9583 bytes (raw)
$ git show chunk:test/spec_mock_response.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
 
# frozen_string_literal: true

require_relative 'helper'
require 'yaml'
require_relative 'psych_fix'

separate_testing do
  require_relative '../lib/rack/mock_request'
  require_relative '../lib/rack/mock_response'
  require_relative '../lib/rack/lint'
  require_relative '../lib/rack/request'
end

app = Rack::Lint.new(lambda { |env|
  req = Rack::Request.new(env)

  env["mock.postdata"] = env["rack.input"].read
  if req.GET["error"]
    env["rack.errors"].puts req.GET["error"]
    env["rack.errors"].flush
  end

  body = req.head? ? "" : env.to_yaml
  response = Rack::Response.new(
    body,
    req.GET["status"] || 200,
    "content-type" => "text/yaml"
  )
  response.set_cookie("session_test", { value: "session_test", domain: ".test.com", path: "/" })
  response.set_cookie("secure_test", { value: "secure_test", domain: ".test.com",  path: "/", secure: true })
  response.set_cookie("persistent_test", { value: "persistent_test", max_age: 15552000, path: "/" })
  response.set_cookie("persistent_with_expires_test", { value: "persistent_with_expires_test", expires: Time.httpdate("Thu, 31 Oct 2021 07:28:00 GMT"), path: "/" })
  response.set_cookie("expires_and_max-age_test", { value: "expires_and_max-age_test", expires: Time.now + 15552000 * 2, max_age: 15552000, path: "/" })
  response.finish
})

describe Rack::MockResponse do
  it 'has standard constructor' do
    headers = { "header" => "value" }
    body = ["body"]

    response = Rack::MockResponse[200, headers, body]

    response.status.must_equal 200
    response.headers.must_equal headers
    response.body.must_equal body.join
  end

  it "provide access to the HTTP status" do
    res = Rack::MockRequest.new(app).get("")
    res.must_be :successful?
    res.must_be :ok?

    res = Rack::MockRequest.new(app).get("/?status=404")
    res.wont_be :successful?
    res.must_be :client_error?
    res.must_be :not_found?

    res = Rack::MockRequest.new(app).get("/?status=501")
    res.wont_be :successful?
    res.must_be :server_error?

    res = Rack::MockRequest.new(app).get("/?status=307")
    res.must_be :redirect?

    res = Rack::MockRequest.new(app).get("/?status=201", lint: true)
    res.must_be :empty?
  end

  it "provide access to the HTTP headers" do
    res = Rack::MockRequest.new(app).get("")
    res.must_include "content-type"
    res.headers["content-type"].must_equal "text/yaml"
    res.original_headers["content-type"].must_equal "text/yaml"
    res["content-type"].must_equal "text/yaml"
    res.content_type.must_equal "text/yaml"
    res.content_length.wont_equal 0
    res.location.must_be_nil
  end

  it "provide access to session cookies" do
    res = Rack::MockRequest.new(app).get("")
    session_cookie = res.cookie("session_test")
    session_cookie.value[0].must_equal "session_test"
    session_cookie.domain.must_equal ".test.com"
    session_cookie.path.must_equal "/"
    session_cookie.secure.must_equal false
    session_cookie.expires.must_be_nil
  end

  it "provides access to persistent cookies set with max-age" do
    res = Rack::MockRequest.new(app).get("")
    persistent_cookie = res.cookie("persistent_test")
    persistent_cookie.value[0].must_equal "persistent_test"
    persistent_cookie.domain.must_be_nil
    persistent_cookie.path.must_equal "/"
    persistent_cookie.secure.must_equal false
    persistent_cookie.expires.wont_be_nil
    persistent_cookie.expires.must_be :<, (Time.now + 15552000)
  end

  it "provides access to persistent cookies set with expires" do
    res = Rack::MockRequest.new(app).get("")
    persistent_cookie = res.cookie("persistent_with_expires_test")
    persistent_cookie.value[0].must_equal "persistent_with_expires_test"
    persistent_cookie.domain.must_be_nil
    persistent_cookie.path.must_equal "/"
    persistent_cookie.secure.must_equal false
    persistent_cookie.expires.wont_be_nil
    persistent_cookie.expires.must_equal Time.httpdate("Thu, 31 Oct 2021 07:28:00 GMT")
  end

  it "parses cookies giving max-age precedence over expires" do
    res = Rack::MockRequest.new(app).get("")
    persistent_cookie = res.cookie("expires_and_max-age_test")
    persistent_cookie.value[0].must_equal "expires_and_max-age_test"
    persistent_cookie.expires.wont_be_nil
    persistent_cookie.expires.must_be :<, (Time.now + 15552000)
  end

  it "provide access to secure cookies" do
    res = Rack::MockRequest.new(app).get("")
    secure_cookie = res.cookie("secure_test")
    secure_cookie.value[0].must_equal "secure_test"
    secure_cookie.domain.must_equal ".test.com"
    secure_cookie.path.must_equal "/"
    secure_cookie.secure.must_equal true
    secure_cookie.expires.must_be_nil
  end

  it "parses cookie headers with equals sign at the end" do
    res = Rack::MockRequest.new(->(env) { [200, { "Set-Cookie" => "__cf_bm=_somebase64encodedstringwithequalsatthened=; array=awesome" }, [""]] }).get("")
    cookie = res.cookie("__cf_bm")
    cookie.value[0].must_equal "_somebase64encodedstringwithequalsatthened="
  end

  it "return nil if a non existent cookie is requested" do
    res = Rack::MockRequest.new(app).get("")
    res.cookie("i_dont_exist").must_be_nil
  end

  deprecated "parses cookie headers provided as an array" do
    res = Rack::MockRequest.new(->(env) { [200, [["set-cookie", "array=awesome"]], [""]] }).get("")
    array_cookie = res.cookie("array")
    array_cookie.value[0].must_equal "awesome"
  end

  deprecated "parses multiple set-cookie headers provided as an array" do
    cookie_headers = [["set-cookie", "array=awesome\nmultiple=times"]]
    res = Rack::MockRequest.new(->(env) { [200, cookie_headers, [""]] }).get("")
    array_cookie = res.cookie("array")
    array_cookie.value[0].must_equal "awesome"
    second_cookie = res.cookie("multiple")
    second_cookie.value[0].must_equal "times"
  end

  it "parses multiple set-cookie headers provided as hash with array value" do
    cookie_headers = { "set-cookie" => ["array=awesome", "multiple=times"]}
    res = Rack::MockRequest.new(->(env) { [200, cookie_headers, [""]] }).get("")
    array_cookie = res.cookie("array")
    array_cookie.value[0].must_equal "awesome"
    second_cookie = res.cookie("multiple")
    second_cookie.value[0].must_equal "times"
  end

  it "provide access to the HTTP body" do
    res = Rack::MockRequest.new(app).get("")
    res.body.must_match(/rack/)
    assert_match(res, /rack/)

    res.match('rack')[0].must_equal 'rack'
    res.match('banana').must_be_nil
  end

  it "provide access to the Rack errors" do
    res = Rack::MockRequest.new(app).get("/?error=foo", lint: true)
    res.must_be :ok?
    res.errors.wont_be :empty?
    res.errors.must_include "foo"
  end

  deprecated "handle enumerable headers that are not a hash" do
    # this is exactly what rack-test does
    res = Rack::MockResponse.new(200, [], [])
    res.cookies.must_equal({})
  end

  it "allow calling body.close afterwards" do
    # this is exactly what rack-test does
    body = StringIO.new("hi")
    res = Rack::MockResponse.new(200, {}, body)
    body.close if body.respond_to?(:close)
    res.body.must_equal 'hi'
  end

  it "ignores plain strings passed as errors" do
    Rack::MockResponse.new(200, {}, [], 'e').errors.must_be_nil
  end

  it "optionally make Rack errors fatal" do
    lambda {
      Rack::MockRequest.new(app).get("/?error=foo", fatal: true)
    }.must_raise Rack::MockRequest::FatalWarning

    lambda {
      Rack::MockRequest.new(lambda { |env| env['rack.errors'].write(env['rack.errors'].string) }).get("/", fatal: true)
    }.must_raise(Rack::MockRequest::FatalWarning).message.must_equal ''
  end
end

describe Rack::MockResponse, 'headers' do
  before do
    @res = Rack::MockRequest.new(app).get('')
    @res.set_header 'FOO', '1'
  end

  it 'has_header?' do
    lambda { @res.has_header? nil }.must_raise ArgumentError

    @res.has_header?('FOO').must_equal true
    @res.has_header?('Foo').must_equal true
  end

  it 'get_header' do
    lambda { @res.get_header nil }.must_raise ArgumentError

    @res.get_header('FOO').must_equal '1'
    @res.get_header('Foo').must_equal '1'
  end

  it 'set_header' do
    lambda { @res.set_header nil, '1' }.must_raise ArgumentError

    @res.set_header('FOO', '2').must_equal '2'
    @res.get_header('FOO').must_equal '2'

    @res.set_header('Foo', '3').must_equal '3'
    @res.get_header('Foo').must_equal '3'
    @res.get_header('FOO').must_equal '3'

    @res.set_header('FOO', nil).must_be_nil
    @res.get_header('FOO').must_be_nil
    @res.has_header?('FOO').must_equal true
  end

  it 'add_header' do
    lambda { @res.add_header nil, '1' }.must_raise ArgumentError

    # Sets header on first addition
    @res.add_header('FOO', '1').must_equal ['1', '1']
    @res.get_header('FOO').must_equal ['1', '1']

    # Ignores nil additions
    @res.add_header('FOO', nil).must_equal ['1', '1']
    @res.get_header('FOO').must_equal ['1', '1']

    # Converts additions to strings
    @res.add_header('FOO', 2).must_equal ['1', '1', '2']
    @res.get_header('FOO').must_equal ['1', '1', '2']

    # Respects underlying case-sensitivity
    @res.add_header('Foo', 'yep').must_equal ['1', '1', '2', 'yep']
    @res.get_header('Foo').must_equal ['1', '1', '2', 'yep']
    @res.get_header('FOO').must_equal ['1', '1', '2', 'yep']
  end

  it 'delete_header' do
    lambda { @res.delete_header nil }.must_raise ArgumentError

    @res.delete_header('FOO').must_equal '1'
    @res.has_header?('FOO').must_equal false

    @res.has_header?('Foo').must_equal false
    @res.delete_header('Foo').must_be_nil
  end
end

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