Class: Tina4::TestResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/test_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rack_response) ⇒ TestResponse

Build from a Rack response tuple [status, headers, body_array]



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
# File 'lib/tina4/test_client.rb', line 23

def initialize(rack_response)
  @status = rack_response[0]
  raw_headers = rack_response[1] || {}

  # Rack folds a genuinely repeated header (Tina4::Response#to_rack only
  # ever repeats Set-Cookie, joined "\n" — the Rack 2/3 convention for a
  # multi-value header carried in a single hash slot) into ONE value; a
  # header value can never legitimately contain a raw newline (RFC 7230),
  # so splitting on "\n" is safe and general, not a Set-Cookie special
  # case. @header_list keeps every occurrence, in emission order, so a
  # duplicate is assertable via get_all(); @headers stays the back-compat
  # single-value view — the LAST occurrence, the "last wins" contract the
  # other three frameworks converge on — so nothing that reads a plain
  # (never-repeated) header sees any change (TC-HEADER-COLLAPSE,
  # TC-DEC-02).
  @header_list = {}
  flat = {}
  raw_headers.each do |name, value|
    key = name.to_s.downcase
    values = value.is_a?(Array) ? value.map(&:to_s) : value.to_s.split("\n")
    values = [""] if values.empty?
    @header_list[key] = values
    flat[key] = values.last
  end
  @headers = flat

  @content_type = @headers["content-type"] || ""
  raw_body = rack_response[2]
  @body = raw_body.is_a?(Array) ? raw_body.join : raw_body.to_s
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



20
21
22
# File 'lib/tina4/test_client.rb', line 20

def body
  @body
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



20
21
22
# File 'lib/tina4/test_client.rb', line 20

def content_type
  @content_type
end

#headersObject (readonly)

Returns the value of attribute headers.



20
21
22
# File 'lib/tina4/test_client.rb', line 20

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



20
21
22
# File 'lib/tina4/test_client.rb', line 20

def status
  @status
end

Instance Method Details

#get_all(name) ⇒ Object

Every value sent for name (case-insensitive), in emission order.

A header sent once returns a one-item array; a header never sent returns an empty array. This is the one place a duplicate response header (two Set-Cookie) is visible — headers always collapses to the LAST value, same as before (TC-HEADER-COLLAPSE, TC-DEC-02).



60
61
62
# File 'lib/tina4/test_client.rb', line 60

def get_all(name)
  (@header_list[name.to_s.downcase] || []).dup
end

#inspectObject



77
78
79
# File 'lib/tina4/test_client.rb', line 77

def inspect
  "<TestResponse status=#{@status} content_type=#{@content_type.inspect}>"
end

#jsonObject

Parse body as JSON.



65
66
67
68
69
70
# File 'lib/tina4/test_client.rb', line 65

def json
  return nil if @body.nil? || @body.empty?
  JSON.parse(@body)
rescue JSON::ParserError
  nil
end

#textObject

Return body as a string.



73
74
75
# File 'lib/tina4/test_client.rb', line 73

def text
  @body.to_s
end