@@ -101,15 +101,19 @@ func TestProxyHTTP(t *testing.T) {
101101 for _ , tc := range testCases {
102102 tc := tc
103103
104- t .Run (tc .name , func (t * testing.T ) {
105- runHTTPTest (t , tc )
104+ t .Run (tc .name + " GET" , func (t * testing.T ) {
105+ runHTTPTest (t , tc , "GET" )
106+ })
107+
108+ t .Run (tc .name + " POST" , func (t * testing.T ) {
109+ runHTTPTest (t , tc , "POST" )
106110 })
107111 }
108112}
109113
110114// TestProxyHTTP tests that the proxy can forward HTTP requests to a backend
111115// service and handle L402 authentication correctly.
112- func runHTTPTest (t * testing.T , tc * testCase ) {
116+ func runHTTPTest (t * testing.T , tc * testCase , method string ) {
113117 // Create a list of services to proxy between.
114118 services := []* proxy.Service {{
115119 Address : testTargetServiceAddress ,
@@ -148,11 +152,25 @@ func runHTTPTest(t *testing.T, tc *testCase) {
148152 // Authorization header set.
149153 client := & http.Client {}
150154 url := fmt .Sprintf ("http://%s/http/test" , testProxyAddr )
151- resp , err := client .Get (url )
155+
156+ req , err := http .NewRequest (method , url , nil )
157+ require .NoError (t , err )
158+
159+ if method == "POST" {
160+ req .Header .Add ("Content-Type" , "application/json" )
161+ req .Body = io .NopCloser (strings .NewReader (`{}` ))
162+ }
163+
164+ resp , err := client .Do (req )
152165 require .NoError (t , err )
153166
154167 require .Equal (t , "402 Payment Required" , resp .Status )
155168
169+ bodyContent , err := io .ReadAll (resp .Body )
170+ require .NoError (t , err )
171+ require .Equal (t , "payment required\n " , string (bodyContent ))
172+ require .EqualValues (t , len (bodyContent ), resp .ContentLength )
173+
156174 authHeader := resp .Header .Get ("Www-Authenticate" )
157175 require .Regexp (t , "(LSAT|L402)" , authHeader )
158176 _ = resp .Body .Close ()
@@ -161,7 +179,7 @@ func runHTTPTest(t *testing.T, tc *testCase) {
161179 // get the 402 response.
162180 if len (tc .authWhitelist ) > 0 {
163181 url = fmt .Sprintf ("http://%s/http/white" , testProxyAddr )
164- req , err := http .NewRequest ("GET" , url , nil )
182+ req , err := http .NewRequest (method , url , nil )
165183 require .NoError (t , err )
166184 resp , err = client .Do (req )
167185 require .NoError (t , err )
@@ -174,11 +192,12 @@ func runHTTPTest(t *testing.T, tc *testCase) {
174192 require .NoError (t , err )
175193
176194 require .Equal (t , testHTTPResponseBody , string (bodyBytes ))
195+ require .EqualValues (t , len (bodyBytes ), resp .ContentLength )
177196 }
178197
179198 // Make sure that if the Auth header is set, the client's request is
180199 // proxied to the backend service.
181- req , err : = http .NewRequest ("GET" , url , nil )
200+ req , err = http .NewRequest (method , url , nil )
182201 require .NoError (t , err )
183202 req .Header .Add ("Authorization" , "foobar" )
184203
@@ -193,6 +212,7 @@ func runHTTPTest(t *testing.T, tc *testCase) {
193212 require .NoError (t , err )
194213
195214 require .Equal (t , testHTTPResponseBody , string (bodyBytes ))
215+ require .EqualValues (t , len (bodyBytes ), resp .ContentLength )
196216}
197217
198218// TestProxyHTTP tests that the proxy can forward gRPC requests to a backend
@@ -313,9 +333,7 @@ func runGRPCTest(t *testing.T, tc *testCase) {
313333
314334 // We expect the WWW-Authenticate header field to be set to an L402
315335 // auth response.
316- expectedHeaderContent , _ := mockAuth .FreshChallengeHeader (& http.Request {
317- Header : map [string ][]string {},
318- }, "" , 0 )
336+ expectedHeaderContent , _ := mockAuth .FreshChallengeHeader ("" , 0 )
319337 capturedHeader := captureMetadata .Get ("WWW-Authenticate" )
320338 require .Len (t , capturedHeader , 2 )
321339 require .Equal (
0 commit comments