aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/html
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/html')
-rw-r--r--pkg/html/html.go29
-rw-r--r--pkg/html/html_test.go49
2 files changed, 76 insertions, 2 deletions
diff --git a/pkg/html/html.go b/pkg/html/html.go
index 72c2956e0..930fdd923 100644
--- a/pkg/html/html.go
+++ b/pkg/html/html.go
@@ -194,6 +194,30 @@ func commitLink(repo, commit string) string {
}
func AmendURL(baseURL, key, value string) string {
+ return TransformURL(baseURL, key, func(_ []string) []string {
+ if value == "" {
+ return nil
+ }
+ return []string{value}
+ })
+}
+
+func DropParam(baseURL, key, value string) string {
+ return TransformURL(baseURL, key, func(oldValues []string) []string {
+ if value == "" {
+ return nil
+ }
+ var newValues []string
+ for _, iterVal := range oldValues {
+ if iterVal != value {
+ newValues = append(newValues, iterVal)
+ }
+ }
+ return newValues
+ })
+}
+
+func TransformURL(baseURL, key string, f func([]string) []string) string {
if baseURL == "" {
return ""
}
@@ -202,10 +226,11 @@ func AmendURL(baseURL, key, value string) string {
return ""
}
values := parsed.Query()
- if value == "" {
+ ret := f(values[key])
+ if len(ret) == 0 {
values.Del(key)
} else {
- values.Set(key, value)
+ values[key] = ret
}
parsed.RawQuery = values.Encode()
return parsed.String()
diff --git a/pkg/html/html_test.go b/pkg/html/html_test.go
new file mode 100644
index 000000000..d824e0246
--- /dev/null
+++ b/pkg/html/html_test.go
@@ -0,0 +1,49 @@
+// Copyright 2023 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+package html
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestDropParam(t *testing.T) {
+ tests := []struct {
+ in string
+ key string
+ value string
+ out string
+ }{
+ {
+ in: `/upstream?first=a&second=b`,
+ key: `first`,
+ value: ``,
+ out: `/upstream?second=b`,
+ },
+ {
+ in: `/upstream?first=a&first=b&second=c`,
+ key: `second`,
+ value: ``,
+ out: `/upstream?first=a&first=b`,
+ },
+ {
+ in: `/upstream?first=a&first=b&second=c`,
+ key: `first`,
+ value: ``,
+ out: `/upstream?second=c`,
+ },
+ {
+ in: `/upstream?first=a&first=b&second=c`,
+ key: `first`,
+ value: `b`,
+ out: `/upstream?first=a&second=c`,
+ },
+ }
+
+ for _, test := range tests {
+ got := DropParam(test.in, test.key, test.value)
+ assert.Equal(t, test.out, got)
+ }
+}