Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lloesche committed Jul 25, 2024
1 parent 38f4e8b commit 379103e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
8 changes: 2 additions & 6 deletions search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/sirupsen/logrus"
"github.com/someengineering/fixctl/utils"
)

type SearchRequest struct {
Query string `json:"query"`
WithEdges bool `json:"with_edges"`
}

func escapeSingleQuotes(s string) string {
return strings.ReplaceAll(s, "'", "'\\''")
}

func SearchGraph(apiEndpoint, fixJWT, workspaceID, searchStr string, withEdges bool) (<-chan interface{}, <-chan error) {
results := make(chan interface{})
errs := make(chan error, 1)
Expand Down Expand Up @@ -54,7 +50,7 @@ func SearchGraph(apiEndpoint, fixJWT, workspaceID, searchStr string, withEdges b
Secure: true,
})

escapedRequestBody := escapeSingleQuotes(string(requestBody))
escapedRequestBody := utils.EscapeSingleQuotes(string(requestBody))
curlCommand := fmt.Sprintf("curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/ndjson' -H 'Cookie: session_token=%s' -d '%s' %s", fixJWT, escapedRequestBody, url)
logrus.Debugln("Equivalent curl command:", curlCommand)

Expand Down
4 changes: 4 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ func SanitizeOutputFormat(format string) (string, error) {
return "", fmt.Errorf("unsupported output format")
}
}

func EscapeSingleQuotes(s string) string {
return strings.ReplaceAll(s, "'", "'\\''")
}
22 changes: 22 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,25 @@ func TestSanitizeCSVHeaders(t *testing.T) {
}
}
}

func TestEscapeSingleQuotes(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"", ""},
{"no single quotes", "no single quotes"},
{"single ' quote", "single '\\'' quote"},
{"multiple ' single ' quotes", "multiple '\\'' single '\\'' quotes"},
{"escaped \\ backslash ' and single quote", "escaped \\ backslash '\\'' and single quote"},
{"'leading", "'\\''leading"},
{"trailing'", "trailing'\\''"},
}

for _, test := range tests {
result := EscapeSingleQuotes(test.input)
if result != test.expected {
t.Errorf("EscapeSingleQuotes(%q) = %q; want %q", test.input, result, test.expected)
}
}
}

0 comments on commit 379103e

Please sign in to comment.