mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
added support for expect 100-continue requests (Fixes #2)
This commit is contained in:
parent
368b207c50
commit
808c351d72
@ -84,10 +84,12 @@ module HTTPX
|
||||
|
||||
def on_message_complete
|
||||
log { "parsing complete" }
|
||||
request = @requests.shift
|
||||
response = request.response
|
||||
@parser.reset!
|
||||
request = @requests.first
|
||||
return handle(request) if request.expects?
|
||||
|
||||
@requests.shift
|
||||
response = request.response
|
||||
emit(:response, request, response)
|
||||
|
||||
send(@pending.shift) unless @pending.empty?
|
||||
|
@ -37,6 +37,9 @@ module HTTPX
|
||||
unless stream = @streams[request]
|
||||
stream = @connection.new_stream
|
||||
stream.on(:close) do |error|
|
||||
if request.expects?
|
||||
return handle(request, stream)
|
||||
end
|
||||
response = request.response || ErrorResponse.new(error, retries)
|
||||
emit(:response, request, response)
|
||||
log(stream.id) { "closing stream" }
|
||||
|
@ -155,11 +155,23 @@ module HTTPX
|
||||
when :headers
|
||||
return unless @state == :idle
|
||||
when :body
|
||||
return unless @state == :headers
|
||||
return unless @state == :headers ||
|
||||
@state == :expect
|
||||
|
||||
if @headers.key?("expect")
|
||||
return unless @response
|
||||
expect_status, _ = @headers["expect"].split("-")
|
||||
return unless @response.status == expect_status.to_i
|
||||
unless @response
|
||||
@state = :expect
|
||||
return
|
||||
end
|
||||
|
||||
case @response.status
|
||||
when 100
|
||||
# deallocate
|
||||
@response = nil
|
||||
when 417
|
||||
@response = ErrorResponse.new("Expectation Failed", 0)
|
||||
return
|
||||
end
|
||||
end
|
||||
when :done
|
||||
return unless @state == :body ||
|
||||
@ -169,6 +181,11 @@ module HTTPX
|
||||
nil
|
||||
end
|
||||
|
||||
def expects?
|
||||
@headers["expect"] == "100-continue" &&
|
||||
@response && @response.status == 100
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
class ProcIO
|
||||
|
@ -25,16 +25,6 @@ module Requests
|
||||
assert body["form"]["foo"] == "bar"
|
||||
end
|
||||
|
||||
define_method :"test_#{meth}_form_file_params" do
|
||||
uri = build_uri("/#{meth}")
|
||||
response = HTTPX.send(meth, uri, form: {image: HTTP::FormData::File.new(fixture_file_path)})
|
||||
assert response.status == 200, "status is unexpected"
|
||||
body = json_body(response)
|
||||
assert body["headers"]["Content-Type"].start_with?("multipart/form-data")
|
||||
assert body.key?("files")
|
||||
assert body["files"].key?("image")
|
||||
end
|
||||
|
||||
define_method :"test_#{meth}_json_params" do
|
||||
uri = build_uri("/#{meth}")
|
||||
response = HTTPX.send(meth, uri, json: {"foo" => "bar"})
|
||||
@ -55,6 +45,28 @@ module Requests
|
||||
assert body.key?("data")
|
||||
assert body["data"] == "data"
|
||||
end
|
||||
|
||||
define_method :"test_#{meth}_form_file_params" do
|
||||
uri = build_uri("/#{meth}")
|
||||
response = HTTPX.send(meth, uri, form: {image: HTTP::FormData::File.new(fixture_file_path)})
|
||||
assert response.status == 200, "status is unexpected"
|
||||
body = json_body(response)
|
||||
assert body["headers"]["Content-Type"].start_with?("multipart/form-data")
|
||||
assert body.key?("files")
|
||||
assert body["files"].key?("image")
|
||||
end
|
||||
|
||||
define_method :"test_#{meth}_expect_100_form_file_params" do
|
||||
uri = build_uri("/#{meth}")
|
||||
response = HTTPX.headers("expect" => "100-continue")
|
||||
.send(meth, uri, form: {image: HTTP::FormData::File.new(fixture_file_path)})
|
||||
assert response.status == 200, "status is unexpected"
|
||||
body = json_body(response)
|
||||
assert body["headers"]["Content-Type"].start_with?("multipart/form-data")
|
||||
assert body["headers"]["Expect"].start_with?("100-continue")
|
||||
assert body.key?("files")
|
||||
assert body["files"].key?("image")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
Loading…
x
Reference in New Issue
Block a user