@@ -6,12 +6,15 @@ import (
66 "errors"
77 "io"
88 "net/http"
9+ "net/http/httptest"
910 "net/url"
11+ "strings"
1012 "testing"
1113
1214 "github.com/ozontech/allure-go/pkg/framework/core/common"
13- "github.com/ozontech/cute/internal/utils"
1415 "github.com/stretchr/testify/require"
16+
17+ "github.com/ozontech/cute/internal/utils"
1518)
1619
1720func TestCreateRequest (t * testing.T ) {
@@ -163,3 +166,109 @@ func TestValidateResponseWithErrors(t *testing.T) {
163166
164167 require .Len (t , errs , 2 )
165168}
169+
170+ type mockRoundTripper struct {}
171+
172+ func (m * mockRoundTripper ) RoundTrip (req * http.Request ) (* http.Response , error ) {
173+ return & http.Response {
174+ StatusCode : 200 ,
175+ Request : req ,
176+ Body : io .NopCloser (strings .NewReader ("mock response" )),
177+ }, nil
178+ }
179+
180+ func TestSanitizeURLHook (t * testing.T ) {
181+ client := & http.Client {
182+ Transport : & mockRoundTripper {},
183+ }
184+
185+ test := & Test {
186+ httpClient : client ,
187+ Retry : & Retry {
188+ currentCount : 0 ,
189+ MaxAttempts : 0 ,
190+ Delay : 0 ,
191+ },
192+ Request : & Request {
193+ Builders : []RequestBuilder {
194+ WithMethod (http .MethodGet ),
195+ WithURI ("http://localhost/api?key=123" ),
196+ },
197+ Retry : & RequestRetryPolitic {
198+ Count : 1 ,
199+ Delay : 2 ,
200+ },
201+ },
202+ RequestSanitizer : sanitizeKeyParam ("****" ),
203+ }
204+
205+ req , err := test .createRequest (context .Background ())
206+ require .NoError (t , err )
207+ require .NotNil (t , req )
208+
209+ newT := createAllureT (t )
210+
211+ err = test .addInformationRequest (newT , req )
212+ require .NoError (t , err )
213+
214+ decodedQuery , err := url .QueryUnescape (req .URL .RawQuery )
215+ require .NoError (t , err )
216+ require .Equal (t , "key=****" , decodedQuery )
217+ }
218+
219+ func TestSanitizeURL_LastRequestURL (t * testing.T ) {
220+ client := & http.Client {
221+ Transport : & mockRoundTripper {},
222+ }
223+
224+ test := & Test {
225+ httpClient : client ,
226+ Request : & Request {
227+ Builders : []RequestBuilder {
228+ WithMethod (http .MethodGet ),
229+ WithURI ("http://localhost/api?key=123" ),
230+ },
231+ },
232+ RequestSanitizer : sanitizeKeyParam ("****" ),
233+ }
234+
235+ allureT := createAllureT (t )
236+ test .Execute (context .Background (), allureT )
237+
238+ decodedURL , err := url .QueryUnescape (test .lastRequestURL )
239+ require .NoError (t , err )
240+ require .Contains (t , decodedURL , "key=****" , "Expected masked key in lastRequestURL" )
241+ }
242+
243+ func sanitizeKeyParam (mask string ) RequestSanitizerHook {
244+ return func (req * http.Request ) {
245+ q := req .URL .Query ()
246+ q .Set ("key" , mask )
247+ req .URL .RawQuery = q .Encode ()
248+ }
249+ }
250+
251+ func TestSanitizeURL_RealRequest (t * testing.T ) {
252+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
253+ body , _ := io .ReadAll (r .Body )
254+ t .Logf ("Server received URL: %s, Body: %s" , r .URL .String (), string (body ))
255+ require .Contains (t , r .URL .String (), "key=123" , "Sanitizer must not change real request" )
256+ w .WriteHeader (200 )
257+ }))
258+ defer ts .Close ()
259+
260+ client := & http.Client {}
261+ test := & Test {
262+ httpClient : client ,
263+ Request : & Request {
264+ Builders : []RequestBuilder {
265+ WithMethod (http .MethodGet ),
266+ WithURI (ts .URL + "/api?key=123" ),
267+ },
268+ },
269+ RequestSanitizer : sanitizeKeyParam ("****" ),
270+ }
271+
272+ allureT := createAllureT (t )
273+ test .Execute (context .Background (), allureT )
274+ }
0 commit comments