@@ -101,15 +101,19 @@ func TestProxyHTTP(t *testing.T) {
101
101
for _ , tc := range testCases {
102
102
tc := tc
103
103
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" )
106
110
})
107
111
}
108
112
}
109
113
110
114
// TestProxyHTTP tests that the proxy can forward HTTP requests to a backend
111
115
// service and handle L402 authentication correctly.
112
- func runHTTPTest (t * testing.T , tc * testCase ) {
116
+ func runHTTPTest (t * testing.T , tc * testCase , method string ) {
113
117
// Create a list of services to proxy between.
114
118
services := []* proxy.Service {{
115
119
Address : testTargetServiceAddress ,
@@ -148,11 +152,25 @@ func runHTTPTest(t *testing.T, tc *testCase) {
148
152
// Authorization header set.
149
153
client := & http.Client {}
150
154
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 )
152
165
require .NoError (t , err )
153
166
154
167
require .Equal (t , "402 Payment Required" , resp .Status )
155
168
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
+
156
174
authHeader := resp .Header .Get ("Www-Authenticate" )
157
175
require .Regexp (t , "(LSAT|L402)" , authHeader )
158
176
_ = resp .Body .Close ()
@@ -161,7 +179,7 @@ func runHTTPTest(t *testing.T, tc *testCase) {
161
179
// get the 402 response.
162
180
if len (tc .authWhitelist ) > 0 {
163
181
url = fmt .Sprintf ("http://%s/http/white" , testProxyAddr )
164
- req , err := http .NewRequest ("GET" , url , nil )
182
+ req , err := http .NewRequest (method , url , nil )
165
183
require .NoError (t , err )
166
184
resp , err = client .Do (req )
167
185
require .NoError (t , err )
@@ -174,11 +192,12 @@ func runHTTPTest(t *testing.T, tc *testCase) {
174
192
require .NoError (t , err )
175
193
176
194
require .Equal (t , testHTTPResponseBody , string (bodyBytes ))
195
+ require .EqualValues (t , len (bodyBytes ), resp .ContentLength )
177
196
}
178
197
179
198
// Make sure that if the Auth header is set, the client's request is
180
199
// proxied to the backend service.
181
- req , err : = http .NewRequest ("GET" , url , nil )
200
+ req , err = http .NewRequest (method , url , nil )
182
201
require .NoError (t , err )
183
202
req .Header .Add ("Authorization" , "foobar" )
184
203
@@ -193,6 +212,7 @@ func runHTTPTest(t *testing.T, tc *testCase) {
193
212
require .NoError (t , err )
194
213
195
214
require .Equal (t , testHTTPResponseBody , string (bodyBytes ))
215
+ require .EqualValues (t , len (bodyBytes ), resp .ContentLength )
196
216
}
197
217
198
218
// TestProxyHTTP tests that the proxy can forward gRPC requests to a backend
@@ -313,9 +333,7 @@ func runGRPCTest(t *testing.T, tc *testCase) {
313
333
314
334
// We expect the WWW-Authenticate header field to be set to an L402
315
335
// auth response.
316
- expectedHeaderContent , _ := mockAuth .FreshChallengeHeader (& http.Request {
317
- Header : map [string ][]string {},
318
- }, "" , 0 )
336
+ expectedHeaderContent , _ := mockAuth .FreshChallengeHeader ("" , 0 )
319
337
capturedHeader := captureMetadata .Get ("WWW-Authenticate" )
320
338
require .Len (t , capturedHeader , 2 )
321
339
require .Equal (
0 commit comments