Class: WebMock::HttpLibAdapters::PatronAdapter

Inherits:
WebMock::HttpLibAdapter show all
Defined in:
lib/webmock/http_lib_adapters/patron_adapter.rb

Defined Under Namespace

Classes: WebMockPatronSession

Constant Summary collapse

OriginalPatronSession =
::Patron::Session

Class Method Summary collapse

Methods inherited from WebMock::HttpLibAdapter

adapter_for

Class Method Details

.build_patron_response(webmock_response, default_response_charset) ⇒ Object

Raises:

  • (::Patron::TimeoutError)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/webmock/http_lib_adapters/patron_adapter.rb', line 100

def self.build_patron_response(webmock_response, default_response_charset)
  raise ::Patron::TimeoutError if webmock_response.should_timeout
  webmock_response.raise_error_if_any

  header_fields = (webmock_response.headers || []).map { |(k, vs)| Array(vs).map { |v| "#{k}: #{v}" } }.flatten
  status_line   = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}"
  header_data   = ([status_line] + header_fields).join("\r\n")

  ::Patron::Response.new(
    "".dup,
    webmock_response.status[0],
    0,
    header_data,
    webmock_response.body.dup,
    default_response_charset
  )
end

.build_request_signature(req) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/webmock/http_lib_adapters/patron_adapter.rb', line 68

def self.build_request_signature(req)
  uri = WebMock::Util::URI.heuristic_parse(req.url)
  uri.path = uri.normalized_path.gsub("[^:]//","/")

  if [:put, :post, :patch].include?(req.action)
    if req.file_name
      if !File.exist?(req.file_name) || !File.readable?(req.file_name)
        raise ArgumentError.new("Unable to open specified file.")
      end
      request_body = File.read(req.file_name)
    elsif req.upload_data
      request_body = req.upload_data
    else
      raise ArgumentError.new("Must provide either data or a filename when doing a PUT or POST")
    end
  end

  headers = req.headers

  if req.credentials
    headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(req.credentials)
  end

  request_signature = WebMock::RequestSignature.new(
    req.action,
    uri.to_s,
    body: request_body,
    headers: headers
  )
  request_signature
end

.build_webmock_response(patron_response) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/webmock/http_lib_adapters/patron_adapter.rb', line 118

def self.build_webmock_response(patron_response)
  webmock_response = WebMock::Response.new
  reason = patron_response.status_line.
    scan(%r(\AHTTP/(\d+(?:\.\d+)?)\s+(\d\d\d)\s*([^\r\n]+)?))[0][2]
  webmock_response.status = [patron_response.status, reason]
  webmock_response.body = patron_response.body
  webmock_response.headers = patron_response.headers
  webmock_response
end

.disable!Object



51
52
53
54
# File 'lib/webmock/http_lib_adapters/patron_adapter.rb', line 51

def self.disable!
  Patron.send(:remove_const, :Session)
  Patron.send(:const_set, :Session, OriginalPatronSession)
end

.enable!Object



46
47
48
49
# File 'lib/webmock/http_lib_adapters/patron_adapter.rb', line 46

def self.enable!
  Patron.send(:remove_const, :Session)
  Patron.send(:const_set, :Session, WebMockPatronSession)
end

.handle_file_name(req, webmock_response) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/webmock/http_lib_adapters/patron_adapter.rb', line 56

def self.handle_file_name(req, webmock_response)
  if req.action == :get && req.file_name
    begin
      File.open(req.file_name, "w") do |f|
        f.write webmock_response.body
      end
    rescue Errno::EACCES
      raise ArgumentError.new("Unable to open specified file.")
    end
  end
end