Compare commits

..

No commits in common. "290da6f1fe4005bab5d1997cbfd6a7b52fd1bd22" and "6437b4b5fbcbe045481bc860a9e1e9d8af6bccbe" have entirely different histories.

10 changed files with 43 additions and 98 deletions

View File

@ -26,7 +26,6 @@ class WebmockTest < Minitest::Test
end
def teardown
super
WebMock.reset!
WebMock.allow_net_connect!
WebMock.disable!
@ -215,49 +214,17 @@ class WebmockTest < Minitest::Test
assert_not_requested(:get, "http://#{httpbin}")
end
def test_webmock_follow_redirects_with_stream_plugin_each
def test_webmock_follow_redirects_with_stream_plugin
session = HTTPX.plugin(:follow_redirects).plugin(:stream)
redirect_url = "#{MOCK_URL_HTTP}/redirect"
initial_request = stub_request(:get, MOCK_URL_HTTP).to_return(status: 302, headers: { location: redirect_url }, body: "redirecting")
redirect_request = stub_request(:get, redirect_url).to_return(status: 200, body: "body")
initial_request = stub_request(:get, MOCK_URL_HTTP).to_return(status: 302, headers: { location: redirect_url })
redirect_request = stub_request(:get, redirect_url)
response = session.get(MOCK_URL_HTTP, stream: true)
body = "".b
response.each do |chunk|
next if (300..399).cover?(response.status)
body << chunk
end
assert_equal("body", body)
session.get(MOCK_URL_HTTP, stream: true).each.to_a.join
assert_requested(initial_request)
assert_requested(redirect_request)
end
def test_webmock_with_stream_plugin_each
session = HTTPX.plugin(:stream)
request = stub_request(:get, MOCK_URL_HTTP).to_return(body: "body")
body = "".b
response = session.get(MOCK_URL_HTTP, stream: true)
response.each do |chunk|
next if (300..399).cover?(response.status)
body << chunk
end
assert_equal("body", body)
assert_requested(request)
end
def test_webmock_with_stream_plugin_each_line
session = HTTPX.plugin(:stream)
request = stub_request(:get, MOCK_URL_HTTP).to_return(body: "First line\nSecond line")
response = session.get(MOCK_URL_HTTP, stream: true)
assert_equal(["First line", "Second line"], response.each_line.to_a)
assert_requested(request)
end
private
def assert_raise_with_message(e, message, &block)

View File

@ -38,10 +38,12 @@ module WebMock
return build_error_response(request, webmock_response.exception) if webmock_response.exception
request.options.response_class.new(request,
webmock_response.status[0],
"2.0",
webmock_response.headers)
response = request.options.response_class.new(request,
webmock_response.status[0],
"2.0",
webmock_response.headers)
response << webmock_response.body.dup
response
end
def build_error_response(request, exception)
@ -88,7 +90,6 @@ module WebMock
log { "mocking #{request.uri} with #{mock_response.inspect}" }
request.response = response
request.emit(:response, response)
response << mock_response.body.dup unless response.is_a?(HTTPX::ErrorResponse)
elsif WebMock.net_connect_allowed?(request_signature.uri)
if WebMock::CallbackRegistry.any_callbacks?
request.on(:response) do |resp|

View File

@ -11,7 +11,6 @@ module HTTPX
@response = response
@decoder = ->(z) { z }
@consumed = false
@grpc_response = nil
end
def inspect
@ -35,7 +34,9 @@ module HTTPX
private
def grpc_response
@grpc_response ||= if @response.respond_to?(:each)
return @grpc_response if defined?(@grpc_response)
@grpc_response = if @response.respond_to?(:each)
Enumerator.new do |y|
Message.stream(@response).each do |message|
y << @decoder.call(message)

View File

@ -37,7 +37,6 @@ module HTTPX
class Inflater
def initialize(response)
@response = response
@grpc_encodings = nil
end
def call(message, &blk)

View File

@ -5,7 +5,6 @@ module HTTPX
def initialize(request, session)
@request = request
@session = session
@response = nil
end
def each(&block)
@ -26,7 +25,7 @@ module HTTPX
def each_line
return enum_for(__method__) unless block_given?
line = "".b
line = +""
each do |chunk|
line << chunk
@ -37,8 +36,6 @@ module HTTPX
line = line.byteslice(idx + 1..-1)
end
end
yield line unless line.empty?
end
# This is a ghost method. It's to be used ONLY internally, when processing streams
@ -61,10 +58,8 @@ module HTTPX
private
def response
return @response if @response
@request.response || begin
@response = @session.request(@request)
@response ||= begin
@request.response || @session.request(@request)
end
end

View File

@ -119,21 +119,10 @@ module HTTPX
def response=(response)
return unless response
if response.is_a?(Response) && response.status < 200
# deal with informational responses
if response.status == 100 && @headers.key?("expect")
@informational_status = response.status
return
end
if response.status >= 103
# 103 Early Hints advertises resources in document to browsers.
# not very relevant for an HTTP client, discard.
return
end
if response.is_a?(Response) && response.status == 100 && @headers.key?("expect")
@informational_status = response.status
return
end
@response = response
emit(:response_started, response)

View File

@ -264,4 +264,4 @@ end
require_relative "response/body"
require_relative "response/buffer"
require_relative "pmatch_extensions" if RUBY_VERSION >= "2.7.0"
require_relative "pmatch_extensions" if RUBY_VERSION >= "3.0.0"

View File

@ -1,4 +1,26 @@
module HTTPX
class StreamResponse
include _ToS
@request: Request & RequestMethods
@session: sessionStream
@on_chunk: ^(String) -> void | nil
def each: () { (String) -> void } -> void
| () -> Enumerable[String]
def each_line: () { (String) -> void } -> void
| () -> Enumerable[String]
def on_chunk: (string) -> void
def initialize: (Request, Session) -> void
private
def response: () -> response
end
module Plugins
module Stream
module InstanceMethods
@ -20,28 +42,4 @@ module HTTPX
type sessionStream = Session & Stream::InstanceMethods
end
class StreamResponse
include _ToS
type streamRequest = Request & Plugins::Stream::RequestMethods
@request: streamRequest
@session: Plugins::sessionStream
@on_chunk: ^(String) -> void | nil
def each: () { (String) -> void } -> void
| () -> Enumerable[String]
def each_line: () { (String) -> void } -> void
| () -> Enumerable[String]
def on_chunk: (string) -> void
def initialize: (streamRequest, Plugins::sessionStream) -> void
private
def response: () -> response
end
end

View File

@ -6,7 +6,7 @@ class ResponseTest < Minitest::Test
include HTTPX
include ResponseHelpers
if RUBY_VERSION >= "2.7.0"
if RUBY_VERSION >= "3.0.0"
begin
eval("case 1; in 1 ;then true; end") # rubocop:disable Style/EvalWithLocation
require_relative "extensions/response_pattern_match"

View File

@ -76,11 +76,6 @@ fi
PARALLEL=1 bundle exec rake test
if [[ "$RUBY_ENGINE" = "ruby" ]] && [[ ${RUBY_VERSION:0:1} = "3" ]] && [[ ! $RUBYOPT =~ "jit" ]]; then
# https://github.com/ruby/rbs/issues/1636
unset RUBYOPT
fi
# third party modules
# Testing them only with main ruby, as some of them work weird with other variants.
if [[ "$RUBY_ENGINE" = "ruby" ]]; then