Class: Zena::Remote::Mock::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/zena/remote/mock.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Http

Returns a new instance of Http.



5
6
7
# File 'lib/zena/remote/mock.rb', line 5

def initialize(options)
  @options = options
end

Instance Method Details

#request(raw_request) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/zena/remote/mock.rb', line 9

def request(raw_request)
  # body should contain xml data for post and put (@raw_request.body ?)
  method  = raw_request.method.downcase
  path    = raw_request.path
  body    = raw_request.body

  response = $test_connection.test_request(method, path, body, @options[:headers], false)
  transform_response(response)
end

#transform_response(ac_res) ⇒ Object

Transform an ActionController::Response into a Net::HTTP response. Based on code from fakeweb (thanks Chrisk !)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zena/remote/mock.rb', line 21

def transform_response(ac_res)
  code, msg = 200, 'OK'
  response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
  response.instance_variable_set(:@body, ac_res.body)
  ac_res.headers.each do |name, value|
    if value.respond_to?(:each)
      value.each { |v| response.add_field(name, v) }
    else
      response[name] = value
    end
  end

  response.instance_variable_set(:@read, true)

  class << response
    def read_body(*args, &block)
      yield @body if block_given?
      @body
    end
  end

  response
end