Added function to sanitize strings by escaping quotes (#2360)
This commit is contained in:
@@ -205,3 +205,9 @@ func DownloadUrl(ctx context.Context, httpClient *http.Client, url string, local
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func EscapeQuotes(str string) string {
|
||||
replacer := strings.NewReplacer("\n", "", "\r", "", "\t", "", `"`, `\"`)
|
||||
str = replacer.Replace(str)
|
||||
return str
|
||||
}
|
||||
|
||||
@@ -81,3 +81,40 @@ func TestGetChecksum(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEscapeQuotes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
src string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Testing tab escape sequence",
|
||||
src: "This\tis\ta\ttest\t string.",
|
||||
want: "Thisisatest string.",
|
||||
},
|
||||
{
|
||||
name: "Testing carriage return",
|
||||
src: "This is a \rtest string. \r This is the second test string\r.",
|
||||
want: "This is a test string. This is the second test string.",
|
||||
},
|
||||
{
|
||||
name: "Testing next line escape sequence",
|
||||
src: "This is a \ntest string. \n This is the second test string\n.",
|
||||
want: "This is a test string. This is the second test string.",
|
||||
},
|
||||
{
|
||||
name: "Testing quotes",
|
||||
src: `This is a "test string"". This is" the second test string "."`,
|
||||
want: `This is a \"test string\"\". This is\" the second test string \".\"`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := EscapeQuotes(tt.src)
|
||||
if got != tt.want {
|
||||
t.Errorf("EscapeQuotes() got = %v, want = %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user