Class: R4A::Test::Response

Inherits:
Message
  • Object
show all
Defined in:
lib/core/r4a/test.rb

Overview

This class represents a HTTP response.

Instance Attribute Summary (collapse)

Attributes inherited from Message

#content, #headers, #status

Instance Method Summary (collapse)

Methods inherited from Message

#backtrace, #queryLine, #urlEncode

Constructor Details

- (Response) initialize(text = nil)

Constructor. Arguments:

text

HTTP response text (optional)



246
247
248
249
250
251
252
253
# File 'lib/core/r4a/test.rb', line 246

def initialize(text=nil)
  super()
  
  if text != nil
    @text = text
    parseResponse(text)
  end
end

Instance Attribute Details

- (Object) text (readonly)

The full HTTP text of the response



240
241
242
# File 'lib/core/r4a/test.rb', line 240

def text
  @text
end

Instance Method Details

- (Object) getCookies

Parses the cookies header and return a hash containing all cookies.



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/core/r4a/test.rb', line 256

def getCookies()

  cookies = {}

  @headers.each do |k,v|
    if k == 'Set-Cookie'
      # Extract cookie value
      key, value = v.split(';')[0].split('=')
      
      # Store
      cookies[key] = value
    end
  end
    
  return cookies
end

- (Object) parseResponse(text)

Parses a HTTP response text and fills out members appropriately.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/core/r4a/test.rb', line 274

def parseResponse(text)
  idx = text.index("\r\n\r\n")

  @content = text[idx + 4 ..-1]
  @headers = {}
  @status  = nil

  # Parse headers
  text[0..idx].split("\r\n").each do |line|
    
    if @status == nil
      @status = line
      next
    end

    k,v = line.split(':')
    @headers[k] = v ? v.strip() : v
  end

  # Parse status line
  @status = @status.split(' ')[1].to_i
end