Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean out an specific uri from registry #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -131,6 +131,10 @@ registry and remove all registered URIs:

FakeWeb.clean_registry

If you want clean out an specific uri, you can remove it from registry:

FakeWeb.clean_uri(:get, "http://example.com")

=== Blocking all real requests

When you're using FakeWeb to replace _all_ of your requests, it's useful to
5 changes: 5 additions & 0 deletions lib/fake_web.rb
Original file line number Diff line number Diff line change
@@ -21,6 +21,11 @@ def self.clean_registry
Registry.instance.clean_registry
end

# Remove added uri from registry.
def self.clean_uri(method, uri)
Registry.instance.clean_uri(method, uri)
end

# Enables or disables real HTTP connections for requests that don't match
# registered URIs.
#
6 changes: 6 additions & 0 deletions lib/fake_web/registry.rb
Original file line number Diff line number Diff line change
@@ -12,6 +12,12 @@ def clean_registry
self.uri_map = Hash.new { |hash, key| hash[key] = {} }
end

def clean_uri(method, uri)
n_uri = normalize_uri(uri)
self.uri_map[n_uri].delete(method)
self.uri_map.delete(n_uri) unless self.uri_map[n_uri].size > 0
end

def register_uri(method, uri, options)
uri_map[normalize_uri(uri)][method] = [*[options]].flatten.collect do |option|
FakeWeb::Responder.new(method, uri, option, option[:times])
10 changes: 10 additions & 0 deletions test/test_fake_web.rb
Original file line number Diff line number Diff line change
@@ -111,6 +111,16 @@ def test_clean_registry_affects_registered_uri
assert !FakeWeb.registered_uri?(:get, "http://example.com")
end

def test_clean_uri_affects_registered_uri
FakeWeb.register_uri(:get, "http://example.com", :body => "registered")
FakeWeb.register_uri(:post, "http://example.com", :body => "registered")
assert FakeWeb.registered_uri?(:get, "http://example.com")
assert FakeWeb.registered_uri?(:post, "http://example.com")
FakeWeb.clean_uri(:get, "http://example.com")
assert !FakeWeb.registered_uri?(:get, "http://example.com")
assert FakeWeb.registered_uri?(:post, "http://example.com")
end

def test_clean_registry_affects_net_http_requests
FakeWeb.register_uri(:get, "http://example.com", :body => "registered")
response = Net::HTTP.start("example.com") { |query| query.get("/") }