new option: origin

by setting the origin, one can pass relative paths to httpx, which will
be appended when building the request.
This commit is contained in:
HoneyryderChuck 2021-04-17 18:51:10 +01:00
parent dbdf7b95a2
commit 1cf6e5aac7
7 changed files with 31 additions and 9 deletions

View File

@ -88,6 +88,10 @@ module HTTPX
end
end
def_option(:origin, <<-OUT)
URI(value)
OUT
def_option(:headers, <<-OUT)
if self.headers
self.headers.merge(value)

View File

@ -41,8 +41,13 @@ module HTTPX
def initialize(verb, uri, options = {})
@verb = verb.to_s.downcase.to_sym
@uri = Utils.uri(uri)
@options = Options.new(options)
@uri = Utils.uri(uri)
if @uri.relative?
raise(Error, "invalid URI: #{@uri}") unless @options.origin
@uri = @options.origin.merge(@uri)
end
raise(Error, "unknown method: #{verb}") unless METHODS.include?(@verb)

View File

@ -11,6 +11,10 @@ module HTTPX
def self.new: (options) -> instance
| () -> instance
# headers
attr_reader uri: URI?
def uri=: (uri) -> void
# headers
attr_reader headers: Headers?
def headers=: (headers) -> void

View File

@ -33,6 +33,6 @@ class ErrorResponseTest < Minitest::Test
private
def request_mock
Request.new(:get, "/")
Request.new(:get, "http://example.com/")
end
end

View File

@ -6,14 +6,14 @@ class RequestTest < Minitest::Test
include HTTPX
def test_request_unsupported_body
ex = assert_raises(HTTPX::Error) { Request.new(:post, "/", body: Object.new) }
ex = assert_raises(HTTPX::Error) { Request.new(:post, "http://example.com/", body: Object.new) }
assert ex.message =~ /cannot determine size of body/
end
def test_request_verb
r1 = Request.new(:get, "/")
r1 = Request.new(:get, "http://example.com/")
assert r1.verb == :get, "unexpected verb (#{r1.verb})"
r2 = Request.new("GET", "/")
r2 = Request.new("GET", "http://example.com/")
assert r2.verb == :get, "unexpected verb (#{r1.verb})"
end
@ -57,21 +57,21 @@ class RequestTest < Minitest::Test
end
def test_request_body_raw
req = Request.new(:post, "/", body: "bang")
req = Request.new(:post, "http://example.com/", body: "bang")
assert !req.body.empty?, "body should exist"
assert req.headers["content-type"] == "application/octet-stream", "content type is wrong"
assert req.headers["content-length"] == "4", "content length is wrong"
end
def test_request_body_form
req = Request.new(:post, "/", form: { "foo" => "bar" })
req = Request.new(:post, "http://example.com/", form: { "foo" => "bar" })
assert !req.body.empty?, "body should exist"
assert req.headers["content-type"] == "application/x-www-form-urlencoded", "content type is wrong"
assert req.headers["content-length"] == "7", "content length is wrong"
end
def test_request_body_json
req = Request.new(:post, "/", json: { "foo" => "bar" })
req = Request.new(:post, "http://example.com/", json: { "foo" => "bar" })
assert !req.body.empty?, "body should exist"
assert req.headers["content-type"] == "application/json; charset=utf-8", "content type is wrong"
assert req.headers["content-length"] == "13", "content length is wrong"

View File

@ -27,7 +27,7 @@ class SessionTest < Minitest::Test
assert session.options.respond_to?(:foo), "options methods weren't added"
assert session.options.foo == "options-foo", "option method is unexpected"
request = session.options.request_class.new(:get, "/", session.options)
request = session.options.request_class.new(:get, "http://example.com/", session.options)
assert request.respond_to?(:foo), "request methods haven't been added"
assert request.foo == "request-foo", "request method is unexpected"
assert request.headers.respond_to?(:foo), "headers methods haven't been added"

View File

@ -3,6 +3,8 @@
require "time"
module Requests
using HTTPX::URIExtensions
module Get
def test_http_get
uri = build_uri("/get")
@ -11,6 +13,13 @@ module Requests
verify_body_length(response)
end
def test_http_get_option_origin
uri = URI(build_uri("/get"))
response = HTTPX.with(origin: uri.origin).get(uri.path)
verify_status(response, 200)
verify_body_length(response)
end
def test_http_request
uri = build_uri("/get")
response = HTTPX.request(:get, uri)