Module: Artifice::Passthru

Defined in:
lib/artifice-passthru/core.rb,
lib/artifice-passthru/version.rb

Overview

Artifice Passthru

Defined Under Namespace

Classes: RequestInfo

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

When Artifice::Passthru is included into Artifice::Net::HTTP, it uses “alias_method_chain” to override the #request method so we can get the arguments that were passed.



37
38
39
40
41
42
43
44
# File 'lib/artifice-passthru/core.rb', line 37

def self.included base
  unless base.instance_methods.map(&:to_s).include?('request_without_passthru_argument_tracking')
    base.class_eval do
      alias_method :request_without_passthru_argument_tracking, :request
      alias_method :request, :request_with_passthru_argument_tracking
    end
  end
end

.last_request_infoObject

Returns the last information that were passed to Net::HTTP#request (which Artifice hijacked) so we can use these arguments to fire off a real request, if necessary.



48
49
50
# File 'lib/artifice-passthru/core.rb', line 48

def self.last_request_info
  Thread.current[:artifice_passthru_arguments]
end

.last_request_info=(request_info) ⇒ Object

Accepts and stores the last information that were passed to Net::HTTP#request (which Artifice hijacked) so we can use these arguments to fire off a real request, if necessary.



54
55
56
# File 'lib/artifice-passthru/core.rb', line 54

def self.last_request_info= request_info
  Thread.current[:artifice_passthru_arguments] = request_info
end

.make_real_request(request_info) ⇒ Object

Given the last_request_info (that would normally be passed to Net::HTTP#request), makes a real request and returns the Net::HTTPResponse



73
74
75
76
# File 'lib/artifice-passthru/core.rb', line 73

def self.make_real_request request_info
  http = Artifice::NET_HTTP.new request_info.address, request_info.port
  http.request request_info.request, request_info.body, &request_info.block
end

.make_real_request_and_return_response!Object

Makes a real Net::HTTP request and returns the response, converted to a Rack response



59
60
61
62
# File 'lib/artifice-passthru/core.rb', line 59

def self.make_real_request_and_return_response!
  raise 'Artifice.passthru! was called but no previous Artifice request was found to make via real Net::HTTP' if last_request_info.nil?
  to_rack_response make_real_request(last_request_info)
end

.setup_to_use_real_net_http(class_or_module) ⇒ Object

Given a constant (class or module), this gives it access to the real Net::HTTP so every request made from within this class/module will use the real Net::HTTP

Taken from: matschaffer.com/2011/04/net-http-mock-cucumber/



82
83
84
85
86
87
88
89
# File 'lib/artifice-passthru/core.rb', line 82

def self.setup_to_use_real_net_http class_or_module
  class_or_module.class_eval %{
    Net = ::Net.dup
    module Net
      HTTP = Artifice::NET_HTTP
    end
  }
end

.to_rack_response(net_http_response) ⇒ Object

Given a Net::HTTPResponse, returns a Rack response



65
66
67
68
69
# File 'lib/artifice-passthru/core.rb', line 65

def self.to_rack_response net_http_response
  # There's some voodoo magic going on that makes the real Net::HTTP#request method populate our response body for us?
  headers = net_http_response.instance_variable_get('@header').inject({}){|all,this| all[this.first] = this.last.join("\n"); all }
  [ net_http_response.code, headers, [net_http_response.body] ]
end

Instance Method Details

#request_with_passthru_argument_tracking(req, body = nil, &block) ⇒ Object

Simply stores the arguments that are passed and then calls “super” (request_without_passthru_argument_tracking)



92
93
94
95
# File 'lib/artifice-passthru/core.rb', line 92

def request_with_passthru_argument_tracking req, body = nil, &block
  Artifice::Passthru.last_request_info = RequestInfo.new address, port, req, body, block
  request_without_passthru_argument_tracking req, body, &block
end