Skip to content

Commit

Permalink
Make stubbed client in spec more predictable
Browse files Browse the repository at this point in the history
- use stub_const
- consistently normalize URIs
  • Loading branch information
ixti committed Dec 27, 2020
1 parent 6a7228f commit 42e30d1
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions spec/lib/http/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,50 @@
RSpec.describe HTTP::Client do
run_server(:dummy) { DummyServer.new }

StubbedClient = Class.new(HTTP::Client) do
def perform(request, options)
stubbed = stubs[request.uri]
stubbed ? stubbed.call(request) : super(request, options)
end

def stubs
@stubs ||= {}
end
before do
stubbed_client = Class.new(HTTP::Client) do
def perform(request, options)
stubbed = stubs[HTTP::URI::NORMALIZER.call(request.uri).to_s]
stubbed ? stubbed.call(request) : super(request, options)
end

def stub(stubs)
@stubs = stubs.each_with_object({}) do |(k, v), o|
o[HTTP::URI.parse k] = v
def stubs
@stubs ||= {}
end

self
def stub(stubs)
@stubs = stubs.each_with_object({}) do |(k, v), o|
o[HTTP::URI::NORMALIZER.call(k).to_s] = v
end

self
end
end
end

def redirect_response(location, status = 302)
lambda do |request|
HTTP::Response.new(
:status => status,
:version => "1.1",
:headers => {"Location" => location},
:body => "",
:request => request
)
def redirect_response(location, status = 302)
lambda do |request|
HTTP::Response.new(
:status => status,
:version => "1.1",
:headers => {"Location" => location},
:body => "",
:request => request
)
end
end
end

def simple_response(body, status = 200)
lambda do |request|
HTTP::Response.new(
:status => status,
:version => "1.1",
:body => body,
:request => request
)
def simple_response(body, status = 200)
lambda do |request|
HTTP::Response.new(
:status => status,
:version => "1.1",
:body => body,
:request => request
)
end
end

stub_const("StubbedClient", stubbed_client)
end

describe "following redirects" do
Expand Down

0 comments on commit 42e30d1

Please sign in to comment.