Class: Curl::WebMockCurlEasy
- Inherits:
-
Easy
- Object
- Easy
- Curl::WebMockCurlEasy
- Defined in:
- lib/webmock/http_lib_adapters/curb_adapter.rb
Instance Method Summary collapse
- #basic_auth_headers ⇒ Object
- #body_str ⇒ Object (also: #body)
- #build_curb_response(webmock_response) ⇒ Object
- #build_request_signature ⇒ Object
- #build_webmock_response ⇒ Object
- #chunked_response? ⇒ Boolean
- #content_type ⇒ Object
- #curb_or_webmock ⇒ Object
- #delete=(value) ⇒ Object
- #head=(value) ⇒ Object
- #header_str ⇒ Object (also: #head)
- #headers_as_hash(headers) ⇒ Object
-
#http(method) ⇒ Object
Mocks of Curl::Easy methods below here.
- #http_post(*data) ⇒ Object (also: #post)
- #http_put(data = nil) ⇒ Object (also: #put)
- #invoke_curb_callbacks ⇒ Object
- #last_effective_url ⇒ Object
- #perform ⇒ Object
- #post_body=(data) ⇒ Object
- #put_data=(data) ⇒ Object
- #reset ⇒ Object
- #reset_webmock_method ⇒ Object
- #response_code ⇒ Object (also: #code)
- #verbose=(verbose) ⇒ Object
- #verbose? ⇒ Boolean
- #webmock_follow_location(location) ⇒ Object
Instance Method Details
#basic_auth_headers ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 148 def basic_auth_headers if self.username {'Authorization' => WebMock::Util::Headers.basic_auth_header(self.username, self.password)} else {} end end |
#body_str ⇒ Object Also known as: body
314 315 316 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 314 def body_str @body_str ||= super end |
#build_curb_response(webmock_response) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 156 def build_curb_response(webmock_response) raise Curl::Err::TimeoutError if webmock_response.should_timeout webmock_response.raise_error_if_any @body_str = webmock_response.body @response_code = webmock_response.status[0] @header_str = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}\r\n".dup @on_debug.call(@header_str, 1) if defined?( @on_debug ) if webmock_response.headers @header_str << webmock_response.headers.map do |k,v| header = "#{k}: #{v.is_a?(Array) ? v.join(", ") : v}" @on_debug.call(header + "\r\n", 1) if defined?( @on_debug ) header end.join("\r\n") @on_debug.call("\r\n", 1) if defined?( @on_debug ) location = webmock_response.headers['Location'] if self.follow_location? && location @last_effective_url = location webmock_follow_location(location) end @content_type = webmock_response.headers["Content-Type"] @transfer_encoding = webmock_response.headers["Transfer-Encoding"] end @last_effective_url ||= self.url end |
#build_request_signature ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 89 def build_request_signature method = @webmock_method.to_s.downcase.to_sym uri = WebMock::Util::URI.heuristic_parse(self.url) uri.path = uri.normalized_path.gsub("[^:]//","/") headers = headers_as_hash(self.headers).merge(basic_auth_headers) request_body = case method when :post, :patch self.post_body || @post_body when :put @put_data else nil end if defined?( @on_debug ) @on_debug.call("Trying 127.0.0.1...\r\n", 0) @on_debug.call('Connected to ' + uri.hostname + "\r\n", 0) @debug_method = method.upcase @debug_path = uri.path @debug_host = uri.hostname http_request = ["#{@debug_method} #{@debug_path} HTTP/1.1"] http_request << "Host: #{uri.hostname}" headers.each do |name, value| http_request << "#{name}: #{value}" end @on_debug.call(http_request.join("\r\n") + "\r\n\r\n", 2) if request_body @on_debug.call(request_body + "\r\n", 4) @on_debug.call( "upload completely sent off: #{request_body.bytesize}"\ " out of #{request_body.bytesize} bytes\r\n", 0 ) end end request_signature = WebMock::RequestSignature.new( method, uri.to_s, body: request_body, headers: headers ) request_signature end |
#build_webmock_response ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 227 def build_webmock_response status, headers = WebMock::HttpLibAdapters::CurbAdapter.parse_header_string(self.header_str) if defined?( @on_debug ) http_response = ["HTTP/1.0 #{@debug_method} #{@debug_path}"] headers.each do |name, value| http_response << "#{name}: #{value}" end http_response << self.body_str @on_debug.call(http_response.join("\r\n") + "\r\n", 3) @on_debug.call("Connection #0 to host #{@debug_host} left intact\r\n", 0) end webmock_response = WebMock::Response.new webmock_response.status = [self.response_code, status] webmock_response.body = self.body_str webmock_response.headers = headers webmock_response end |
#chunked_response? ⇒ Boolean
223 224 225 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 223 def chunked_response? defined?( @transfer_encoding ) && @transfer_encoding == 'chunked' && self.body_str.respond_to?(:each) end |
#content_type ⇒ Object
333 334 335 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 333 def content_type @content_type ||= super end |
#curb_or_webmock ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 65 def curb_or_webmock request_signature = build_request_signature WebMock::RequestRegistry.instance.requested_signatures.put(request_signature) if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature) build_curb_response(webmock_response) WebMock::CallbackRegistry.invoke_callbacks( {lib: :curb}, request_signature, webmock_response) invoke_curb_callbacks true elsif WebMock.net_connect_allowed?(request_signature.uri) res = yield if WebMock::CallbackRegistry.any_callbacks? webmock_response = build_webmock_response WebMock::CallbackRegistry.invoke_callbacks( {lib: :curb, real_request: true}, request_signature, webmock_response) end res else raise WebMock::NetConnectNotAllowedError.new(request_signature) end end |
#delete=(value) ⇒ Object
296 297 298 299 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 296 def delete= value @webmock_method = :delete if value super end |
#head=(value) ⇒ Object
301 302 303 304 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 301 def head= value @webmock_method = :head if value super end |
#header_str ⇒ Object Also known as: head
324 325 326 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 324 def header_str @header_str ||= super end |
#headers_as_hash(headers) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 136 def headers_as_hash(headers) if headers.is_a?(Array) headers.inject({}) {|hash, header| name, value = header.split(":", 2).map(&:strip) hash[name] = value hash } else headers end end |
#http(method) ⇒ Object
Mocks of Curl::Easy methods below here.
252 253 254 255 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 252 def http(method) @webmock_method = method super end |
#http_post(*data) ⇒ Object Also known as: post
271 272 273 274 275 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 271 def http_post *data @webmock_method = :post @post_body = data.join('&') if data && !data.empty? super end |
#http_put(data = nil) ⇒ Object Also known as: put
264 265 266 267 268 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 264 def http_put data = nil @webmock_method = :put @put_data = data if data super end |
#invoke_curb_callbacks ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 199 def invoke_curb_callbacks @on_progress.call(0.0,1.0,0.0,1.0) if defined?( @on_progress ) self.header_str.lines.each { |header_line| @on_header.call header_line } if defined?( @on_header ) if defined?( @on_body ) if chunked_response? self.body_str.each do |chunk| @on_body.call(chunk) end else @on_body.call(self.body_str) end end @on_complete.call(self) if defined?( @on_complete ) case response_code when 200..299 @on_success.call(self) if defined?( @on_success ) when 400..499 @on_missing.call(self, self.response_code) if defined?( @on_missing ) when 500..599 @on_failure.call(self, self.response_code) if defined?( @on_failure ) end end |
#last_effective_url ⇒ Object
329 330 331 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 329 def last_effective_url @last_effective_url ||= super end |
#perform ⇒ Object
278 279 280 281 282 283 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 278 def perform @webmock_method ||= :get curb_or_webmock { super } ensure reset_webmock_method end |
#post_body=(data) ⇒ Object
291 292 293 294 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 291 def post_body= data @webmock_method = :post super end |
#put_data=(data) ⇒ Object
285 286 287 288 289 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 285 def put_data= data @webmock_method = :put @put_data = data super end |
#reset ⇒ Object
350 351 352 353 354 355 356 357 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 350 def reset instance_variable_set(:@body_str, nil) instance_variable_set(:@content_type, nil) instance_variable_set(:@header_str, nil) instance_variable_set(:@last_effective_url, nil) instance_variable_set(:@response_code, nil) super end |
#reset_webmock_method ⇒ Object
346 347 348 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 346 def reset_webmock_method @webmock_method = :get end |
#response_code ⇒ Object Also known as: code
319 320 321 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 319 def response_code @response_code ||= super end |
#verbose=(verbose) ⇒ Object
306 307 308 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 306 def verbose=(verbose) @verbose = verbose end |
#verbose? ⇒ Boolean
310 311 312 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 310 def verbose? @verbose ||= false end |
#webmock_follow_location(location) ⇒ Object
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/webmock/http_lib_adapters/curb_adapter.rb', line 188 def webmock_follow_location(location) first_url = self.url self.url = location curb_or_webmock do send( :http, {'method' => @webmock_method} ) end self.url = first_url end |