Skip to content

Commit

Permalink
simplicity is always good
Browse files Browse the repository at this point in the history
  • Loading branch information
kashifkhan0771 committed Oct 15, 2024
1 parent caf62d1 commit b854936
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 91 deletions.
39 changes: 3 additions & 36 deletions pkg/custom_detectors/custom_detectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
Expand Down Expand Up @@ -182,28 +181,13 @@ func (c *CustomRegexWebhook) createResults(ctx context.Context, match map[string
// mark the result as verified
result.Verified = true

// read the Content-Type header and response body
respContentType := resp.Header.Get("Content-Type")
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
continue
}

var responseStr string

// determine the response content type and process accordingly
switch respContentType {
case "application/json":
responseStr, err = handleJSONResponse(body)
if err != nil {
return err
}
case "text/plain":
responseStr = string(body)
default:
// TODO: handle other content types (HTML, XML, etc.)
responseStr = string(body)
}
// TODO: handle different content-type responses seperatly when implement custom detector configurations
responseStr := string(body)

// store the processed response in ExtraData
result.ExtraData["response"] = responseStr
Expand Down Expand Up @@ -297,20 +281,3 @@ func (c *CustomRegexWebhook) Description() string {
}
return c.GetDescription()
}

// helper function to handle JSON response
func handleJSONResponse(body []byte) (string, error) {
var respBody interface{}
err := json.Unmarshal(body, &respBody)
if err != nil {
return "", fmt.Errorf("failed to unmarshal JSON: %v", err)
}

// convert JSON map to a formatted string
jsonString, err := json.MarshalIndent(respBody, "", " ")
if err != nil {
return "", fmt.Errorf("failed to marshal JSON: %v", err)
}

return strings.TrimSpace(string(jsonString)), nil
}
55 changes: 0 additions & 55 deletions pkg/custom_detectors/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,58 +232,3 @@ func TestCustomDetectorsVerifyRegexVarsValidation(t *testing.T) {
})
}
}

func TestCustomDetectorsHandleJSONResponse(t *testing.T) {
tests := []struct {
name string
body []byte
want string
wantErr bool
}{
{
name: "success - convert JSON object to string",
body: []byte(`{"id":"123","name":"test"}`),
// adjust the expected 'want' string with correct indentation (2 spaces per level)
want: `{
"id": "123",
"name": "test"
}`,
wantErr: false,
},
{
name: "success - convert JSON array to string",
body: []byte(`[{"id":"123","name":"test"},{"id":"456","name":"example"}]`),
// adjust the expected 'want' string with correct indentation (2 spaces per level)
want: `[
{
"id": "123",
"name": "test"
},
{
"id": "456",
"name": "example"
}
]`,
wantErr: false,
},
{
name: "fail - convert JSON to string",
body: []byte(`{id:"123","name":"test"}`),
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := handleJSONResponse(tt.body)
if err != nil && !tt.wantErr {
t.Errorf("handleJSONResponse() error = %v, wantErr %v", err, tt.wantErr)
}

if got != "" && got != tt.want {
t.Errorf("handleJSONResponse() got = %v, want %v", got, tt.want)
}

})
}
}

0 comments on commit b854936

Please sign in to comment.