mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-12 00:01:58 -04:00
added tests for request body, fixed #bytesize's
This commit is contained in:
parent
fbdd7e2dd8
commit
734a187ce1
@ -1,4 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "http/2"
|
||||
|
||||
module HTTPX
|
||||
|
@ -29,7 +29,7 @@ module HTTPX
|
||||
|
||||
attr_reader :verb, :uri, :headers, :body
|
||||
|
||||
def initialize(verb, uri, options)
|
||||
def initialize(verb, uri, options={})
|
||||
@verb = verb.to_s.downcase.to_sym
|
||||
@uri = URI(uri)
|
||||
@options = Options.new(options)
|
||||
@ -75,11 +75,13 @@ module HTTPX
|
||||
when options.json
|
||||
Transcoder.registry("json").encode(options.json)
|
||||
end
|
||||
return if @body.nil?
|
||||
@headers["content-type"] ||= @body.content_type
|
||||
@headers["content-length"] ||= @body.content_length
|
||||
@headers["content-length"] ||= @body.bytesize
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
return if @body.nil?
|
||||
if @body.respond_to?(:read)
|
||||
IO.copy_stream(@body, ProcIO.new(block))
|
||||
elsif @body.respond_to?(:each)
|
||||
@ -89,6 +91,11 @@ module HTTPX
|
||||
end
|
||||
end
|
||||
|
||||
def empty?
|
||||
return true if @body.nil?
|
||||
bytesize.zero?
|
||||
end
|
||||
|
||||
def bytesize
|
||||
return 0 if @body.nil?
|
||||
if @body.respond_to?(:bytesize)
|
||||
|
@ -13,7 +13,7 @@ module HTTPX::Transcoder
|
||||
@raw
|
||||
end
|
||||
|
||||
def content_length
|
||||
def bytesize
|
||||
if @raw.respond_to?(:bytesize)
|
||||
@raw.bytesize
|
||||
elsif @raw.respond_to?(:size)
|
||||
|
@ -6,8 +6,24 @@ module HTTPX::Transcoder
|
||||
module Form
|
||||
module_function
|
||||
|
||||
class Encoder
|
||||
extend Forwardable
|
||||
|
||||
def_delegator :@raw, :content_type
|
||||
|
||||
def_delegator :@raw, :to_str
|
||||
|
||||
def initialize(form)
|
||||
@raw = HTTP::FormData.create(form)
|
||||
end
|
||||
|
||||
def bytesize
|
||||
@raw.content_length
|
||||
end
|
||||
end
|
||||
|
||||
def encode(form)
|
||||
HTTP::FormData.create(form)
|
||||
Encoder.new(form)
|
||||
end
|
||||
end
|
||||
register "form", Form
|
||||
|
@ -16,7 +16,7 @@ module HTTPX::Transcoder
|
||||
"application/json; charset=#{@charset}"
|
||||
end
|
||||
|
||||
def content_length
|
||||
def bytesize
|
||||
@raw.bytesize
|
||||
end
|
||||
|
||||
|
@ -16,12 +16,6 @@ class RequestTest < Minitest::Test
|
||||
assert resource.headers.is_a?(Headers), "headers should have been coerced"
|
||||
end
|
||||
|
||||
def test_request_body_concat
|
||||
assert resource.body.nil?, "body should be nil after init"
|
||||
resource << "data"
|
||||
assert resource.body == "data", "body should have been updated"
|
||||
end
|
||||
|
||||
def test_request_scheme
|
||||
r1 = Request.new(:get, "http://google.com/path")
|
||||
assert r1.scheme == "http", "unexpected scheme (#{r1.scheme}"
|
||||
@ -47,6 +41,26 @@ class RequestTest < Minitest::Test
|
||||
assert r3.path == "/path?q=bang®ion=eu-west-1", "unexpected path (#{r3.path})"
|
||||
end
|
||||
|
||||
def test_request_body_raw
|
||||
req = Request.new(:post, "/", 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"})
|
||||
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"})
|
||||
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"
|
||||
end
|
||||
private
|
||||
|
||||
def resource
|
||||
|
Loading…
x
Reference in New Issue
Block a user