Skip to content
Closed
Show file tree
Hide file tree
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 lib/http/chainable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ def persistent(host, timeout: 5)
p_client.close if p_client
end

def to(host)
branch default_options.with_origin(host)
end

# Make a request through an HTTP proxy
# @param [Array] proxy
# @raise [Request::Error] if HTTP proxy is invalid
Expand Down
4 changes: 2 additions & 2 deletions lib/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def verify_connection!(uri)
def make_request_uri(uri, opts)
uri = uri.to_s

if default_options.persistent? && uri !~ HTTP_OR_HTTPS_RE
uri = "#{default_options.persistent}#{uri}"
if default_options.origin && uri !~ HTTP_OR_HTTPS_RE
uri = "#{default_options.origin}#{uri}"
end

uri = HTTP::URI.parse uri
Expand Down
8 changes: 7 additions & 1 deletion lib/http/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,16 @@ def follow=(value)
end
end

def_option :origin, :reader_only => true

def origin=(value)
@origin = value ? HTTP::URI.parse(value).origin : nil
end

def_option :persistent, :reader_only => true

def persistent=(value)
@persistent = value ? HTTP::URI.parse(value).origin : nil
@persistent = self.origin = value
end

def persistent?
Expand Down
1 change: 1 addition & 0 deletions spec/lib/http/options/merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
:form => {:bar => "bar"},
:body => "body-bar",
:json => {:bar => "bar"},
:origin => "https://www.googe.com",
:persistent => "https://www.googe.com",
:keep_alive_timeout => 10,
:ssl => {:foo => "bar"},
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@
end
end

describe ".to" do
subject(:client) { HTTP.to dummy.endpoint }

it { is_expected.to be_an HTTP::Client }
it { is_expected.to_not be_persistent }

it "accepts just a path as uri" do
response = client.post("/echo-body", body: "a body")
expect(response.to_s).to eq("a body")
end
end

describe ".timeout" do
context "specifying a null timeout" do
subject(:client) { HTTP.timeout :null }
Expand Down