-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add HTTP adapter. * Change list of arguments for overridden Net::HTTP#request.
- Loading branch information
Showing
7 changed files
with
81 additions
and
1 deletion.
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
24 changes: 24 additions & 0 deletions
24
lib/io_to_response_payload_ratio/adapters/net_http_adapter.rb
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require "net/http" | ||
require "io_to_response_payload_ratio/patches/net_http_adapter_patch" | ||
|
||
module IoToResponsePayloadRatio | ||
class NetHttpAdapter < BaseAdapter | ||
def self.kind | ||
:net_http | ||
end | ||
|
||
def initialize! | ||
ActiveSupport.on_load(:after_initialize) do | ||
Net::HTTP.prepend(NetHttpAdapterPatch) | ||
|
||
if defined?(::WebMock) | ||
WebMock::HttpLibAdapters::NetHttpAdapter | ||
.instance_variable_get(:@webMockNetHTTP) | ||
.prepend(NetHttpAdapterPatch) | ||
end | ||
end | ||
end | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
lib/io_to_response_payload_ratio/patches/net_http_adapter_patch.rb
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module IoToResponsePayloadRatio | ||
module NetHttpAdapterPatch | ||
def request(*args, &block) | ||
super do |response| | ||
if response.body && IoToResponsePayloadRatio.aggregator.active? | ||
IoToResponsePayloadRatio.aggregator.increment(NetHttpAdapter.kind, response.body.bytesize) | ||
end | ||
end | ||
end | ||
end | ||
end |
39 changes: 39 additions & 0 deletions
39
spec/io_to_response_payload_ratio/adapters/net_http_adapter_spec.rb
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe IoToResponsePayloadRatio::NetHttpAdapter do | ||
let(:aggregator) { IoToResponsePayloadRatio.aggregator } | ||
let(:body) { "Hello, World!" } | ||
let(:url) { "www.example.com" } | ||
|
||
before do | ||
stub_request(:get, url).to_return body: body | ||
end | ||
|
||
before do | ||
aggregator.start! | ||
end | ||
|
||
after do | ||
aggregator.stop! | ||
end | ||
|
||
context "when aggregator is inactive" do | ||
before do | ||
aggregator.stop! | ||
end | ||
|
||
it "does nothing" do | ||
expect(aggregator).not_to receive(:increment) | ||
|
||
Net::HTTP.get url, "/" | ||
end | ||
end | ||
|
||
it "increments aggregator by request's body bytesize" do | ||
allow(aggregator).to receive(:increment) | ||
|
||
Net::HTTP.get url, "/" | ||
|
||
expect(aggregator).to have_received(:increment).with(described_class.kind, body.bytesize) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
require "webmock/rspec" | ||
require "simplecov" | ||
SimpleCov.start if ENV["COVERAGE"] | ||
|
||
|