HoneyryderChuck 092e594a4b Request.verb is now an upcased string (ex: "GET")
The reference for a request verb is now the string which is used
everywhere else, instead of the symbol corresponding to it. This was an
artifact from the import from httprb, and there is no advantage in it,
since these strings are frozen in most use cases, and the
transformations from symbol to strings being performed everywhere are
prooof that keeping the atom isn't really bringing any benefit.
2023-04-17 16:54:31 +03:00

66 lines
2.0 KiB
Ruby

# frozen_string_literal: true
module Requests
module Plugins
module Upgrade
def test_plugin_upgrade_h2
return unless origin.start_with?("https://")
http = HTTPX.plugin(SessionWithPool)
if OpenSSL::SSL::SSLContext.method_defined?(:alpn_protocols=)
http = http.with(ssl: { alpn_protocols: %w[http/1.1] }) # disable alpn negotiation
end
http.plugin(:upgrade).wrap do |session|
uri = build_uri("/", "https://stadtschreiber.ruhr")
request = session.build_request("GET", uri)
request2 = session.build_request("GET", uri)
response = session.request(request)
verify_status(response, 200)
assert response.version == "1.1", "first request should be in HTTP/1.1"
response.close
# verifies that first request was used to upgrade the connection
verify_header(response.headers, "upgrade", "h2,h2c")
response2 = session.request(request2)
verify_status(response2, 200)
assert response2.version == "2.0", "second request should already be in HTTP/2"
response2.close
end
end unless RUBY_VERSION.start_with?("2.3")
def test_plugin_upgrade_websockets
return unless origin.start_with?("http://")
http = HTTPX.plugin(SessionWithPool).plugin(:upgrade)
response = http.get("http://ws-echo-server")
verify_status(response, 200)
http = http.plugin(WSTestPlugin)
response = http.get("http://ws-echo-server")
verify_status(response, 101)
websocket = response.websocket
assert !websocket.nil?, "websocket wasn't created"
websocket.send("ping")
websocket.send("pong")
sleep 2
echo_messages = websocket.messages
assert echo_messages.size >= 3
assert echo_messages.include?("handshake")
assert echo_messages.include?("ping")
assert echo_messages.include?("pong")
websocket.close
end
end
end
end