Class: Rack::TestApp::Result
- Inherits:
-
Object
- Object
- Rack::TestApp::Result
- Defined in:
- lib/rack/test_app.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #body_binary ⇒ Object
- #body_json ⇒ Object
- #body_text ⇒ Object
- #content_length ⇒ Object
- #content_type ⇒ Object
- #cookie_value(name) ⇒ Object
-
#initialize(status, headers, body) ⇒ Result
constructor
A new instance of Result.
- #location ⇒ Object
Constructor Details
#initialize(status, headers, body) ⇒ Result
Returns a new instance of Result.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/rack/test_app.rb', line 316 def initialize(status, headers, body) #; [!3lcsj] accepts response status, headers and body. @status = status @headers = headers @body = body #; [!n086q] parses 'Set-Cookie' header. @cookies = {} raw_str = @headers['Set-Cookie'] || @headers['set-cookie'] raw_str.split(/\r?\n/).each do |s| if s && ! s.empty? c = Util.(s) @cookies[c[:name]] = c end end if raw_str end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
332 333 334 |
# File 'lib/rack/test_app.rb', line 332 def body @body end |
#cookies ⇒ Object
Returns the value of attribute cookies.
332 333 334 |
# File 'lib/rack/test_app.rb', line 332 def @cookies end |
#headers ⇒ Object
Returns the value of attribute headers.
332 333 334 |
# File 'lib/rack/test_app.rb', line 332 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
332 333 334 |
# File 'lib/rack/test_app.rb', line 332 def status @status end |
Instance Method Details
#body_binary ⇒ Object
334 335 336 337 338 339 340 |
# File 'lib/rack/test_app.rb', line 334 def body_binary #; [!mb0i4] returns body as binary string. buf = []; @body.each {|x| buf << x } s = buf.join() @body.close() if @body.respond_to?(:close) return s end |
#body_json ⇒ Object
360 361 362 363 |
# File 'lib/rack/test_app.rb', line 360 def body_json #; [!qnic1] returns Hash object representing JSON string. return JSON.parse(body_text()) end |
#body_text ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/rack/test_app.rb', line 342 def body_text #; [!rr18d] error when 'Content-Type' header is missing. ctype = self.content_type or raise TypeError.new("body_text(): missing 'Content-Type' header.") #; [!dou1n] converts body text according to 'charset' in 'Content-Type' header. if ctype =~ /; *charset=(\w[-\w]*)/ charset = $1 #; [!cxje7] assumes charset as 'utf-8' when 'Content-Type' is json. elsif ctype == "application/json" charset = 'utf-8' #; [!n4c71] error when non-json 'Content-Type' header has no 'charset'. else raise TypeError.new("body_text(): missing 'charset' in 'Content-Type' header.") end #; [!vkj9h] returns body as text string, according to 'charset' in 'Content-Type'. return body_binary().force_encoding(charset) end |
#content_length ⇒ Object
370 371 372 373 374 375 |
# File 'lib/rack/test_app.rb', line 370 def content_length #; [!5lb19] returns 'Content-Length' header value as integer. #; [!qjktz] returns nil when 'Content-Length' is not set. len = @headers['Content-Length'] || @headers['content-length'] return len ? Integer(len) : len end |
#content_type ⇒ Object
365 366 367 368 |
# File 'lib/rack/test_app.rb', line 365 def content_type #; [!40hcz] returns 'Content-Type' header value. return @headers['Content-Type'] || @headers['content-type'] end |
#cookie_value(name) ⇒ Object
382 383 384 385 386 387 |
# File 'lib/rack/test_app.rb', line 382 def (name) #; [!neaf8] returns cookie value if exists. #; [!oapns] returns nil if cookie not exists. c = @cookies[name] return c ? c[:value] : nil end |
#location ⇒ Object
377 378 379 380 |
# File 'lib/rack/test_app.rb', line 377 def location #; [!8y8lg] returns 'Location' header value. return @headers['Location'] || @headers['location'] end |