Skip to content

Commit

Permalink
Merge pull request #89 from icyleaf/refactor/uri-resolve-with-endpoint
Browse files Browse the repository at this point in the history
Improve resolve the URI #88
  • Loading branch information
icyleaf authored Nov 24, 2020
2 parents ba22d2a + 42fb9a1 commit 1b1b872
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
10 changes: 10 additions & 0 deletions spec/halite/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ describe Halite::Client do
response = client.get("anything", params: {"foo" => "bar"})
response.parse["url"].should eq("/anything?foo=bar")
end

it "should resolves uri" do
client = Halite::Client.new do
endpoint SERVER.endpoint
end

response = Halite.get("#{SERVER.endpoint}/params", params: {foo: "bar"})
response.status_code.should eq(200)
response.to_s.should eq("Params!")
end
end

describe "#request" do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/mock_server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ class MockServer < HTTP::Server
end

def api(path : String)
File.join(endpoint, path)
URI.parse(endpoint).resolve(path).to_s
end
end
11 changes: 5 additions & 6 deletions src/halite/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,11 @@ module Halite

# Merges query params if needed
private def make_request_uri(url : String, options : Halite::Options) : String
if endpoint = options.endpoint
uri = endpoint.dup
uri.path = url[0] == '/' ? url : File.join(uri.path, url) unless url.empty? || uri.path == url
else
uri = URI.parse(url)
end
uri = if endpoint = options.endpoint
endpoint.resolve(url)
else
URI.parse(url)
end

if params = options.params
query = HTTP::Params.encode(params)
Expand Down

0 comments on commit 1b1b872

Please sign in to comment.