@@ -20,13 +20,10 @@ import (
2020 "net/http"
2121 "net/http/httptest"
2222 "os"
23- "regexp"
2423 "strings"
2524 "sync"
2625 "testing"
2726 "time"
28-
29- "github.com/google/go-cmp/cmp"
3027)
3128
3229// RecordedRequest captures details of an HTTP request received by the mock server
@@ -245,89 +242,6 @@ func assertLogContains(t *testing.T, output string, expected []string) {
245242 }
246243}
247244
248- // assertLogNotContains checks if the output does NOT contain specified strings
249- //
250- //nolint:unused
251- func assertLogNotContains (t * testing.T , output string , forbidden []string ) {
252- t .Helper ()
253- for _ , forb := range forbidden {
254- if strings .Contains (output , forb ) {
255- t .Errorf ("Expected output to NOT contain %q, but it did.\n Output:\n %s" , forb , output )
256- }
257- }
258- }
259-
260- // assertLogMatchesRegex checks if the output matches all provided regex patterns
261- //
262- // Parameters:
263- // - t: testing.T instance
264- // - output: log output to match against
265- // - patterns: slice of regex pattern strings
266- //
267- // Example:
268- //
269- // assertLogMatchesRegex(t, stdout, []string{`\d+ secrets found`, `Scan completed in \d+\.\d+s`})
270- //
271- //nolint:unused
272- func assertLogMatchesRegex (t * testing.T , output string , patterns []string ) {
273- t .Helper ()
274- for _ , pattern := range patterns {
275- matched , err := regexp .MatchString (pattern , output )
276- if err != nil {
277- t .Fatalf ("Invalid regex pattern %q: %v" , pattern , err )
278- }
279- if ! matched {
280- t .Errorf ("Expected output to match pattern %q, but it didn't.\n Output:\n %s" , pattern , output )
281- }
282- }
283- }
284-
285- // compareJSON compares two JSON strings for structural equality
286- //
287- // This function unmarshals both strings and uses go-cmp to compare them,
288- // providing detailed diff output on failure.
289- //
290- // Parameters:
291- // - t: testing.T instance
292- // - got: actual JSON string
293- // - want: expected JSON string
294- //
295- // Example:
296- //
297- // compareJSON(t, stdout, `{"status":"success","count":5}`)
298- //
299- //nolint:unused
300- func compareJSON (t * testing.T , got , want string ) {
301- t .Helper ()
302-
303- var gotData , wantData interface {}
304-
305- if err := json .Unmarshal ([]byte (got ), & gotData ); err != nil {
306- t .Fatalf ("Failed to unmarshal 'got' JSON: %v\n JSON:\n %s" , err , got )
307- }
308-
309- if err := json .Unmarshal ([]byte (want ), & wantData ); err != nil {
310- t .Fatalf ("Failed to unmarshal 'want' JSON: %v\n JSON:\n %s" , err , want )
311- }
312-
313- if diff := cmp .Diff (wantData , gotData ); diff != "" {
314- t .Errorf ("JSON mismatch (-want +got):\n %s" , diff )
315- }
316- }
317-
318- // assertRequestCount verifies the number of HTTP requests received
319- //
320- //nolint:unused
321- func assertRequestCount (t * testing.T , requests []RecordedRequest , expected int ) {
322- t .Helper ()
323- if len (requests ) != expected {
324- t .Errorf ("Expected %d requests, got %d" , expected , len (requests ))
325- for i , req := range requests {
326- t .Logf ("Request %d: %s %s" , i + 1 , req .Method , req .Path )
327- }
328- }
329- }
330-
331245// assertRequestMethodAndPath verifies a request has the expected method and path
332246func assertRequestMethodAndPath (t * testing.T , req RecordedRequest , method , path string ) {
333247 t .Helper ()
@@ -348,40 +262,7 @@ func assertRequestHeader(t *testing.T, req RecordedRequest, header, expected str
348262 }
349263}
350264
351- // assertRequestHeaderContains verifies a request header contains a substring
352- //
353- //nolint:unused
354- func assertRequestHeaderContains (t * testing.T , req RecordedRequest , header , substring string ) {
355- t .Helper ()
356- actual := req .Headers .Get (header )
357- if ! strings .Contains (actual , substring ) {
358- t .Errorf ("Expected header %s to contain %q, got %q" , header , substring , actual )
359- }
360- }
361-
362- // assertRequestBody verifies the request body matches expected content
363- //
364- //nolint:unused
365- func assertRequestBody (t * testing.T , req RecordedRequest , expected string ) {
366- t .Helper ()
367- actual := string (req .Body )
368- if actual != expected {
369- t .Errorf ("Request body mismatch:\n Expected: %s\n Got: %s" , expected , actual )
370- }
371- }
372-
373- // assertRequestBodyJSON compares request body as JSON
374- //
375- //nolint:unused
376- //nolint:unused
377- func assertRequestBodyJSON (t * testing.T , req RecordedRequest , expected string ) {
378- t .Helper ()
379- compareJSON (t , string (req .Body ), expected )
380- }
381-
382265// dumpRequests prints all recorded requests for debugging
383- //
384- //nolint:unused
385266func dumpRequests (t * testing.T , requests []RecordedRequest ) {
386267 t .Helper ()
387268 t .Log ("Recorded HTTP requests:" )
@@ -402,47 +283,6 @@ func dumpRequests(t *testing.T, requests []RecordedRequest) {
402283 }
403284}
404285
405- // mockGitLabHandler returns a handler for common GitLab API endpoints
406- //
407- //nolint:unused
408- func mockGitLabHandler (t * testing.T , responses map [string ]interface {}) http.HandlerFunc {
409- return func (w http.ResponseWriter , r * http.Request ) {
410- // Set common headers
411- w .Header ().Set ("Content-Type" , "application/json" )
412-
413- // Route to appropriate response
414- key := r .Method + " " + r .URL .Path
415- if response , ok := responses [key ]; ok {
416- if statusCode , ok := response .(int ); ok && statusCode >= 400 {
417- w .WriteHeader (statusCode )
418- _ = json .NewEncoder (w ).Encode (map [string ]string {
419- "error" : "API error" ,
420- })
421- return
422- }
423- w .WriteHeader (http .StatusOK )
424- _ = json .NewEncoder (w ).Encode (response )
425- return
426- }
427-
428- // Default 404 response
429- w .WriteHeader (http .StatusNotFound )
430- _ = json .NewEncoder (w ).Encode (map [string ]string {
431- "error" : "not found" ,
432- })
433- }
434- }
435-
436- // withTimeout wraps a handler with a delay for testing timeout scenarios
437- //
438- //nolint:unused
439- func withTimeout (handler http.HandlerFunc , delay time.Duration ) http.HandlerFunc {
440- return func (w http.ResponseWriter , r * http.Request ) {
441- time .Sleep (delay )
442- handler (w , r )
443- }
444- }
445-
446286// withError returns a handler that always returns an error status
447287func withError (statusCode int , message string ) http.HandlerFunc {
448288 return func (w http.ResponseWriter , r * http.Request ) {
@@ -466,26 +306,3 @@ func mockSuccessResponse() http.HandlerFunc {
466306 })
467307 }
468308}
469-
470- // createTempConfigFile creates a temporary config file for testing
471- //
472- //nolint:unused
473- func createTempConfigFile (t * testing.T , content string ) string {
474- t .Helper ()
475- tmpDir := t .TempDir ()
476- configPath := fmt .Sprintf ("%s/config.yaml" , tmpDir )
477- err := os .WriteFile (configPath , []byte (content ), 0644 )
478- if err != nil {
479- t .Fatalf ("Failed to create temp config file: %v" , err )
480- }
481- return configPath
482- }
483-
484- // skipIfShort skips the test if running in short mode
485- //
486- //nolint:unused
487- func skipIfShort (t * testing.T , reason string ) {
488- if testing .Short () {
489- t .Skipf ("Skipping in short mode: %s" , reason )
490- }
491- }
0 commit comments