Class: Rack::TestApp::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/test_app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.parse_set_cookie(s)
      @cookies[c[:name]] = c
    end
  end if raw_str
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



332
333
334
# File 'lib/rack/test_app.rb', line 332

def body
  @body
end

#cookiesObject

Returns the value of attribute cookies.



332
333
334
# File 'lib/rack/test_app.rb', line 332

def cookies
  @cookies
end

#headersObject

Returns the value of attribute headers.



332
333
334
# File 'lib/rack/test_app.rb', line 332

def headers
  @headers
end

#statusObject

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_binaryObject



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_jsonObject



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_textObject



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_lengthObject



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_typeObject



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


382
383
384
385
386
387
# File 'lib/rack/test_app.rb', line 382

def cookie_value(name)
  #; [!neaf8] returns cookie value if exists.
  #; [!oapns] returns nil if cookie not exists.
  c = @cookies[name]
  return c ? c[:value] : nil
end

#locationObject



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