Skip to content

Instantly share code, notes, and snippets.

@nbibler
Created October 9, 2009 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbibler/206226 to your computer and use it in GitHub Desktop.
Save nbibler/206226 to your computer and use it in GitHub Desktop.
From 33be3517c2f8c0ec2c1d519ea40acb1e704c485a Mon Sep 17 00:00:00 2001
From: Nathaniel Bibler <git@nathanielbibler.com>
Date: Fri, 9 Oct 2009 14:22:28 -0400
Subject: [PATCH] Patch for httpclient with soap4r support
---
lib/fake_web.rb | 1 +
lib/fake_web/ext/httpclient.rb | 58 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+), 0 deletions(-)
create mode 100644 lib/fake_web/ext/httpclient.rb
diff --git a/lib/fake_web.rb b/lib/fake_web.rb
index fbc0b2f..914d790 100644
--- a/lib/fake_web.rb
+++ b/lib/fake_web.rb
@@ -1,6 +1,7 @@
require 'singleton'
require 'fake_web/ext/net_http'
+require 'fake_web/ext/httpclient'
require 'fake_web/registry'
require 'fake_web/response'
require 'fake_web/responder'
diff --git a/lib/fake_web/ext/httpclient.rb b/lib/fake_web/ext/httpclient.rb
new file mode 100644
index 0000000..f049571
--- /dev/null
+++ b/lib/fake_web/ext/httpclient.rb
@@ -0,0 +1,58 @@
+require 'httpclient'
+
+class Net::HTTPResponse
+
+ def contenttype
+ self.content_type.to_s
+ end
+
+end
+
+module Net::HTTPHeader
+
+ def [](key)
+ if key.downcase == 'content-encoding' # fix for soap4r
+ @header[key.downcase] || []
+ else
+ a = @header[key.downcase]
+ return unless a
+ a.join(', ')
+ end
+ end
+
+end
+
+
+class HTTPClient
+
+ module FakeWebHTTPClientResponseWrapper
+
+ def status
+ self.code.to_i
+ end
+
+ def content
+ self.body
+ end
+
+ end
+
+ def do_get_block_with_fakeweb(req, proxy, conn, &block)
+ method = req.header.request_method.downcase.to_sym
+ uri = req.header.request_uri
+
+ if FakeWeb.registered_uri?(method, uri)
+ response = FakeWeb.response_for(method, uri, &block).extend(FakeWebHTTPClientResponseWrapper)
+ conn.push(response)
+ elsif FakeWeb.allow_net_connect?
+ do_get_block_without_fakeweb(req, proxy, conn, &block)
+ else
+ uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
+ raise FakeWeb::NetConnectNotAllowedError,
+ "Real HTTP connections are disabled. Unregistered request: #{method} #{uri}"
+ end
+ end
+ alias_method :do_get_block_without_fakeweb, :do_get_block
+ alias_method :do_get_block, :do_get_block_with_fakeweb
+
+end
--
1.6.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment