|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +# Test that async_http adapter is properly registered and can be configured. |
| 6 | +# Note: Real async HTTP tests with network calls are problematic with VCR/WebMock |
| 7 | +# because Minitest parallel threads conflict with Async's event loop. |
| 8 | +class AsyncHttpAdapterTest < ActiveSupport::TestCase |
| 9 | + test "async_http adapter is registered with Faraday" do |
| 10 | + # Verify the adapter is available |
| 11 | + assert Faraday::Adapter.lookup_middleware(:async_http), |
| 12 | + "async_http adapter should be registered" |
| 13 | + end |
| 14 | + |
| 15 | + test "async_http adapter can be selected via environment variable" do |
| 16 | + original = ENV["AP_DEFAULT_ADAPTER"] |
| 17 | + ENV["AP_DEFAULT_ADAPTER"] = "async_http" |
| 18 | + |
| 19 | + # Build a minimal connection to test adapter selection |
| 20 | + connection = Faraday.new(url: "https://example.com") do |conn| |
| 21 | + conn.request :retry |
| 22 | + conn.response :raise_error |
| 23 | + default_adapter = ENV.fetch("AP_DEFAULT_ADAPTER", "net_http").to_sym |
| 24 | + conn.adapter default_adapter |
| 25 | + end |
| 26 | + |
| 27 | + # Verify async_http was selected - adapter returns a Handler with klass attribute |
| 28 | + adapter_handler = connection.builder.adapter |
| 29 | + assert_equal Async::HTTP::Faraday::Adapter, adapter_handler.klass, |
| 30 | + "Connection should use async_http adapter when AP_DEFAULT_ADAPTER is set" |
| 31 | + ensure |
| 32 | + if original |
| 33 | + ENV["AP_DEFAULT_ADAPTER"] = original |
| 34 | + else |
| 35 | + ENV.delete("AP_DEFAULT_ADAPTER") |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + test "default adapter is net_http when env not set" do |
| 40 | + original = ENV["AP_DEFAULT_ADAPTER"] |
| 41 | + ENV.delete("AP_DEFAULT_ADAPTER") |
| 42 | + |
| 43 | + connection = Faraday.new(url: "https://example.com") do |conn| |
| 44 | + conn.request :retry |
| 45 | + conn.response :raise_error |
| 46 | + default_adapter = ENV.fetch("AP_DEFAULT_ADAPTER", "net_http").to_sym |
| 47 | + conn.adapter default_adapter |
| 48 | + end |
| 49 | + |
| 50 | + adapter_handler = connection.builder.adapter |
| 51 | + assert_equal Faraday::Adapter::NetHttp, adapter_handler.klass, |
| 52 | + "Connection should default to net_http when AP_DEFAULT_ADAPTER is not set" |
| 53 | + ensure |
| 54 | + ENV["AP_DEFAULT_ADAPTER"] = original if original |
| 55 | + end |
| 56 | + |
| 57 | + test "async_http adapter class is properly defined" do |
| 58 | + # Verify the adapter class exists and has the expected interface |
| 59 | + adapter_class = Async::HTTP::Faraday::Adapter |
| 60 | + assert adapter_class < Faraday::Adapter, |
| 61 | + "Async::HTTP::Faraday::Adapter should inherit from Faraday::Adapter" |
| 62 | + |
| 63 | + # Verify connection can be built with the adapter (without making requests) |
| 64 | + connection = Faraday.new(url: "https://example.com") do |conn| |
| 65 | + conn.adapter :async_http |
| 66 | + end |
| 67 | + |
| 68 | + assert_kind_of Faraday::Connection, connection |
| 69 | + assert_equal Async::HTTP::Faraday::Adapter, connection.builder.adapter.klass |
| 70 | + end |
| 71 | +end |
0 commit comments