@@ -116,25 +116,60 @@ defmodule Hex.HTTPTest do
116116 end )
117117 end
118118
119- test "request includes identifier header when available" , % { bypass: bypass } do
120- in_tmp ( fn ->
121- # Initialize a git repository with a commit
122- System . cmd ( "git" , [ "init" , "--initial-branch=main" ] )
123- System . cmd ( "git" , [ "config" , "user.email" , "test@example.com" ] )
124- System . cmd ( "git" , [ "config" , "user.name" , "Test User" ] )
125- File . write! ( "test.txt" , "test content" )
126- System . cmd ( "git" , [ "add" , "test.txt" ] )
127- System . cmd ( "git" , [ "commit" , "-m" , "Initial commit" ] )
119+ test "request with Expect 100-continue receives body after 100 response" , % { bypass: bypass } do
120+ # Test that httpc handles 100-continue flow correctly
121+ body_content = "test request body"
128122
129- Bypass . expect ( bypass , fn conn ->
130- assert [ client_id ] = Plug.Conn . get_req_header ( conn , "x-hex-repo-id" )
131- assert client_id =~ ~r / ^[a-f0-9]{64}$ /
123+ Bypass . expect ( bypass , fn conn ->
124+ # Verify the Expect header is present
125+ assert [ "100-continue" ] = Plug.Conn . get_req_header ( conn , "expect" )
132126
133- Plug.Conn . resp ( conn , 200 , "" )
134- end )
127+ # Send 100 Continue informational response
128+ conn = Plug.Conn . inform ( conn , 100 , [ ] )
135129
136- Hex.RepoIdentifier . clear ( )
137- Hex.HTTP . request ( :get , "http://localhost:#{ bypass . port } " , % { } , nil )
130+ { :ok , body , conn } = Plug.Conn . read_body ( conn )
131+ assert body == body_content
132+
133+ Plug.Conn . resp ( conn , 201 , "success" )
138134 end )
135+
136+ { :ok , { status , _headers , response_body } } =
137+ Hex.HTTP . request (
138+ :post ,
139+ "http://localhost:#{ bypass . port } " ,
140+ % { "expect" => "100-continue" } ,
141+ { "text/plain" , body_content }
142+ )
143+
144+ assert status == 201
145+ assert response_body == "success"
146+ end
147+
148+ test "request with Expect 100-continue stops sending body on error response" , % {
149+ bypass: bypass
150+ } do
151+ # Test that when server responds with error before 100, body is not sent
152+ # Note: This is handled by httpc automatically - if server responds with
153+ # error status instead of 100 Continue, httpc won't send the body
154+
155+ Bypass . expect ( bypass , fn conn ->
156+ # Verify the Expect header is present
157+ assert [ "100-continue" ] = Plug.Conn . get_req_header ( conn , "expect" )
158+
159+ # Immediately respond with 401 Unauthorized without reading body
160+ # httpc should NOT send the body when it receives this error
161+ Plug.Conn . resp ( conn , 401 , "unauthorized" )
162+ end )
163+
164+ { :ok , { status , _headers , response_body } } =
165+ Hex.HTTP . request (
166+ :post ,
167+ "http://localhost:#{ bypass . port } " ,
168+ % { "expect" => "100-continue" } ,
169+ { "text/plain" , "this body should not be sent" }
170+ )
171+
172+ assert status == 401
173+ assert response_body == "unauthorized"
139174 end
140175end
0 commit comments