1.3.0 does not recognize registered uri for post method
Opened this issue · 0 comments
There is an issue with fakeweb 1.3.0 not being able to recognize a registered uri. I've made a patch on my local system but would love to see you incorporate the fix into a released version.
My application is using koala 0.8.0 to talk to Facebook's graph api. My unit test is basically trying to send a delete request to Facebook, but I'm trying to use fakeweb to intercept the uri.
Here's the test code:
setup fakeweb and register the facebook graph uri
FakeWeb.allow_net_connect = false
FakeWeb.register_uri(:post, %r{https://graph.facebook.com/[0-9,_]+}, :status => 200, :body => "")
create a koala client, then delete the object.
fb_graph_client_session = Koala::Facebook::GraphAPI.new(oauth_token)
fb_graph_client_session.delete_object("#{fb_id}")
...
Fakeweb 1.3.0 fails to recognize the uri. I tracked it down to the function request_uri_as_string, which builds an incorrect url.
Failing uri generated by utility.rb, line 35:
https://graph.facebook.com:44398765_7
The uri should be this:
https://graph.facebook.com:443/98765_7
My fix was to insert '/' before the path, unless it's already there.
fakeweb-1.3.0/lib/fake_web/utility.rb:
def self.request_uri_as_string(net_http, request)
...
#added by lkang - to make it work with gem koala (0.8.0) delete_object( )
#path.insert(0, '/') unless path.start_with?('/')
uri = "#{protocol}://#{userinfo}#{net_http.address}:#{net_http.port}#{path}"
puts "****** #{FILE} uri: #{uri}"
uri
end
FYI, fakeweb appears to work with Koala for get methods, but post is a problem. Looking at the Koala code, it is using Net::HTTP#post to create the http post request (line 35)
koala-0.8.0/lib/koala/http_services.rb:
...
25 server = options[:rest_api] ? Facebook::REST_SERVER : Facebook::GRAPH_SERVER
26 http = Net::HTTP.new(server, 443)
27 http.use_ssl = true
28 # we turn off certificate validation to avoid the
29 # "warning: peer certificate won't be verified in this SSL session" warning
30 # not sure if this is the right way to handle it
31 # see http://redcorundum.blogspot.com/2008/03/ssl-certificates-and-nethttps.html
32 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
33
34 result = http.start { |http|
35 response, body = (verb == "post" ? http.post(path, encode_params(args)) : http.get("#{path}?#{encode_params(args)}"))
36 Koala::Response.new(response.code.to_i, body, response)
37 }
...