This is an automated email from the ASF dual-hosted git repository.

nickva pushed a commit to branch wh/connect_to_w_tests
in repository https://gitbox.apache.org/repos/asf/couchdb-ibrowse.git

commit 165b3c6852e4e37ec77e2d5a7c6a3f48cddc6c1a
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Tue Apr 28 01:06:12 2026 -0400

    Add connect_to tests
    
    These are for Will's "feat: add connect_to option" feature
---
 test/ibrowse_test.erl        | 55 ++++++++++++++++++++++++++++++++++++++++++++
 test/ibrowse_test_server.erl | 17 ++++++++++++++
 2 files changed, 72 insertions(+)

diff --git a/test/ibrowse_test.erl b/test/ibrowse_test.erl
index 4e64acc..5c87539 100644
--- a/test/ibrowse_test.erl
+++ b/test/ibrowse_test.erl
@@ -34,6 +34,9 @@
          test_preserve_status_line/0,
          test_binary_headers/0,
          test_binary_headers/1,
+         test_connect_to_overrides_target/0,
+         test_connect_to_unreachable_fails/0,
+         test_connect_to_preserves_host_header/0,
          test_dead_lb_pid/0,
          test_generate_body_0/0,
          test_retry_of_requests/0,
@@ -62,6 +65,9 @@
                       {local_test_fun, test_303_response_with_a_body, []},
                      {local_test_fun, test_303_response_with_no_body, []},
                       {local_test_fun, test_binary_headers, []},
+                      {local_test_fun, test_connect_to_overrides_target, []},
+                      {local_test_fun, test_connect_to_unreachable_fails, []},
+                      {local_test_fun, test_connect_to_preserves_host_header, 
[]},
                       {local_test_fun, test_dead_lb_pid, []},
                       {local_test_fun, test_retry_of_requests, []},
                      {local_test_fun, verify_chunked_streaming, []},
@@ -546,6 +552,55 @@ test_binary_headers(Url) ->
             {test_failed, Res}
     end.
 
+%%------------------------------------------------------------------------------
+%% url host is bogus, request only succeeds if connect_to actually
+%% redirects
+%%------------------------------------------------------------------------------
+test_connect_to_overrides_target() ->
+    clear_msg_q(),
+    Url = "http://no.such.host.invalid:8181/";,
+    case ibrowse:send_req(Url, [], get, [], [{connect_to, "localhost"}], 5000) 
of
+        {ok, "200", _, _} ->
+            success;
+        Res ->
+            {test_failed, Res}
+    end.
+
+%%------------------------------------------------------------------------------
+%% url host reachable, but connect_to points at an unroutable
+%% address
+%%------------------------------------------------------------------------------
+test_connect_to_unreachable_fails() ->
+    clear_msg_q(),
+    case ibrowse:send_req("http://localhost:8181/";, [], get, [],
+                          [{connect_to, "192.0.2.1"},
+                           {connect_timeout, 500}],
+                          5000) of
+        {error, _} ->
+            success;
+        Res ->
+            {test_failed, Res}
+    end.
+
+%%------------------------------------------------------------------------------
+%% connect_to shouldn't bleed into the Host header, server should see the
+%% original url host
+%%------------------------------------------------------------------------------
+test_connect_to_preserves_host_header() ->
+    clear_msg_q(),
+    Url = "http://example.invalid:8181/ibrowse_echo_host";,
+    case ibrowse:send_req(Url, [], get, [], [{connect_to, "localhost"}], 5000) 
of
+        {ok, "200", Headers, _} ->
+            case proplists:get_value("x-host", Headers) of
+                "example.invalid:8181" ->
+                    success;
+                V ->
+                    {fail, V}
+            end;
+        Res ->
+            {test_failed, Res}
+    end.
+
 
%%------------------------------------------------------------------------------
 %% Test what happens when the response to a HEAD request is a
 %% Chunked-Encoding response with a non-empty body. Issue #67 on
diff --git a/test/ibrowse_test_server.erl b/test/ibrowse_test_server.erl
index 17af695..7a5bb01 100644
--- a/test/ibrowse_test_server.erl
+++ b/test/ibrowse_test_server.erl
@@ -229,6 +229,16 @@ process_request(Sock, Sock_type,
        false ->
            collect_body
     end;
+process_request(Sock, Sock_type,
+                #request{method='GET',
+                         headers = Headers,
+                         uri = {abs_path, "/ibrowse_echo_host"}}) ->
+    Host = get_host_header(Headers),
+    Resp = [<<"HTTP/1.1 200 OK\r\n">>,
+            <<"Server: ibrowse_test\r\n">>,
+            "x-host: ", Host, "\r\n",
+            <<"Content-Length: 0\r\n\r\n">>],
+    do_send(Sock, Sock_type, Resp);
 process_request(Sock, Sock_type,
                 #request{method='GET',
                          headers = Headers,
@@ -366,3 +376,10 @@ get_content_length([{http_header, _, _X, _, _Y} | T]) ->
     get_content_length(T);
 get_content_length([]) ->
     undefined.
+
+get_host_header([{http_header, _, 'Host', _, V} | _]) ->
+    V;
+get_host_header([_ | T]) ->
+    get_host_header(T);
+get_host_header([]) ->
+    "not_found".

Reply via email to