mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
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:
parent
dbdf7b95a2
commit
1cf6e5aac7
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
@ -33,6 +33,6 @@ class ErrorResponseTest < Minitest::Test
|
||||
private
|
||||
|
||||
def request_mock
|
||||
Request.new(:get, "/")
|
||||
Request.new(:get, "http://example.com/")
|
||||
end
|
||||
end
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user