class Rack::TestApp::Result

Attributes

body[RW]
cookies[RW]
headers[RW]
status[RW]

Public Class Methods

new(status, headers, body) click to toggle source
# File lib/rack/test_app.rb, line 315
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.parse_set_cookie(s)
      @cookies[c[:name]] = c
    end
  end if raw_str
end

Public Instance Methods

body_binary() click to toggle source
# File lib/rack/test_app.rb, line 333
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() click to toggle source
# File lib/rack/test_app.rb, line 359
def body_json
  #; [!qnic1] returns Hash object representing JSON string.
  return JSON.parse(body_text())
end
body_text() click to toggle source
# File lib/rack/test_app.rb, line 341
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() click to toggle source
# File lib/rack/test_app.rb, line 369
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() click to toggle source
# File lib/rack/test_app.rb, line 364
def content_type
  #; [!40hcz] returns 'Content-Type' header value.
  return @headers['Content-Type'] || @headers['content-type']
end
location() click to toggle source
# File lib/rack/test_app.rb, line 376
def location
  #; [!8y8lg] returns 'Location' header value.
  return @headers['Location'] || @headers['location']
end