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

Add RequestStub helper methods for accessing request signatures #574

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/webmock/request_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ def response
end
end

# Fetch all requests from `WebMock::RequestRegistry` which match this stubs `@request_pattern`
# @return [Array] the requested `WebMock::RequestSignature`s which match this stubs `@request_pattern`
def requests
request_registry = WebMock::RequestRegistry.instance
signatures = request_registry.requested_signatures.ary
return signatures.select { |signature|
@request_pattern.matches?(signature)
}
end

# Fetch the first request made for this stub
# @return [WebMock::RequestSignature|nil] the first request signature for this stub, or nil if none were made
def first_request
return requests[0]
end

# Fetch the last request made for this stub
# @return [WebMock::RequestSignature|nil] the last request signature for this stub, or nil if none were made
def last_request
return requests[-1]
end

def has_responses?
!@responses_sequences.empty?
end
Expand Down
3 changes: 3 additions & 0 deletions lib/webmock/util/hash_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
module WebMock
module Util
class Util::HashCounter
attr_accessor :ary
attr_accessor :hash
def initialize
self.ary = []
self.hash = {}
@order = {}
@max = 0
Expand All @@ -13,6 +15,7 @@ def initialize
def put key, num=1
@lock.synchronize do
hash[key] = (hash[key] || 0) + num
ary << key
@order[key] = @max = @max + 1
end
end
Expand Down
65 changes: 65 additions & 0 deletions spec/unit/request_stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,71 @@
expect(@request_stub.response).to be_a(WebMock::Response)
end

describe "requests" do

it "should have no requests" do
expect(@request_stub.requests).to eq([])
end

it "should have requests" do
signature = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(signature)
expect(@request_stub.requests).to eq([signature])
end

it "should only return matching requests" do
match = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(match)
non_match = WebMock::RequestSignature.new(:get, "www.example.org")
WebMock::RequestRegistry.instance.requested_signatures.put(non_match)

expect(@request_stub.requests).to eq([match])
end

it "should not combine identical requests" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com")
signature2 = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(signature1)
WebMock::RequestRegistry.instance.requested_signatures.put(signature2)
expect(@request_stub.requests).to eq([signature1, signature2])
end

end

describe "last_request" do

it "should be nil when there are no requests" do
expect(@request_stub.last_request).to be_nil
end

it "should be the last requests when there are multiple requests" do
first = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(first)
last = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(last)

expect(@request_stub.last_request).to eq(last)
end

end

describe "first_request" do

it "should be nil when there are no requests" do
expect(@request_stub.first_request).to be_nil
end

it "should be the first requests when there are multiple requests" do
first = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(first)
last = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(last)

expect(@request_stub.last_request).to eq(first)
end

end

describe "with" do

it "should assign body to request pattern" do
Expand Down