-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from AlanFoster/add-additional-request-tracking
Add additional request tracking
- Loading branch information
Showing
8 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Billy | ||
class RequestLog | ||
attr_reader :requests | ||
|
||
def initialize | ||
@requests = [] | ||
end | ||
|
||
def reset | ||
@requests = [] | ||
end | ||
|
||
def record(method, url, headers, body) | ||
return unless Billy.config.record_requests | ||
|
||
request = { | ||
status: :inflight, | ||
handler: nil, | ||
method: method, | ||
url: url, | ||
headers: headers, | ||
body: body | ||
} | ||
@requests.push(request) | ||
|
||
request | ||
end | ||
|
||
def complete(request, handler) | ||
return unless Billy.config.record_requests | ||
|
||
request.merge! status: :complete, | ||
handler: handler | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'spec_helper' | ||
|
||
describe Billy::RequestLog do | ||
let(:request_log) { Billy::RequestLog.new } | ||
|
||
describe '#record' do | ||
it 'returns the request details if record_requests is enabled' do | ||
allow(Billy::config).to receive(:record_requests).and_return(true) | ||
expected_request = { | ||
status: :inflight, | ||
handler: nil, | ||
method: :method, | ||
url: :url, | ||
headers: :headers, | ||
body: :body | ||
} | ||
expect(request_log.record(:method, :url, :headers, :body)).to eql(expected_request) | ||
end | ||
|
||
it 'returns nil if record_requests is disabled' do | ||
allow(Billy::config).to receive(:record_requests).and_return(false) | ||
expect(request_log.record(:method, :url, :headers, :body)).to be_nil | ||
end | ||
end | ||
|
||
describe '#complete' do | ||
it 'marks the request as complete if record_requests is enabled' do | ||
allow(Billy::config).to receive(:record_requests).and_return(true) | ||
|
||
request = request_log.record(:method, :url, :headers, :body) | ||
expected_request = { | ||
status: :complete, | ||
handler: :handler, | ||
method: :method, | ||
url: :url, | ||
headers: :headers, | ||
body: :body | ||
} | ||
expect(request_log.complete(request, :handler)).to eql(expected_request) | ||
end | ||
|
||
it 'marks the request as complete if record_requests is disabled' do | ||
allow(Billy::config).to receive(:record_requests).and_return(false) | ||
expect(request_log.complete(nil, :handler)).to be_nil | ||
end | ||
end | ||
|
||
describe '#requests' do | ||
it 'returns an empty array when there are no requests' do | ||
expect(request_log.requests).to be_empty | ||
end | ||
|
||
it 'returns the currently known requests' do | ||
allow(Billy::config).to receive(:record_requests).and_return(true) | ||
|
||
request1 = request_log.record(:method, :url, :headers, :body) | ||
request2 = request_log.record(:method, :url, :headers, :body) | ||
expect(request_log.requests).to eql([request1, request2]) | ||
end | ||
end | ||
|
||
describe '#reset' do | ||
it 'resets known requests' do | ||
allow(Billy::config).to receive(:record_requests).and_return(true) | ||
|
||
request1 = request_log.record(:method, :url, :headers, :body) | ||
request2 = request_log.record(:method, :url, :headers, :body) | ||
expect(request_log.requests).to eql([request1, request2]) | ||
|
||
request_log.reset | ||
expect(request_log.requests).to be_empty | ||
end | ||
end | ||
end |