mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-18 15:34:38 +01:00
v0.5: (BREAKING) replacing path with path_patterns, improved docker monitoring mechanism, bug fixes
This commit is contained in:
@@ -1,32 +1,37 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func yamlParser[T any](value string) (any, E.NestedError) {
|
||||
var data T
|
||||
func yamlListParser(value string) (any, E.NestedError) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return []string{}, E.Nil()
|
||||
}
|
||||
var data []string
|
||||
err := E.From(yaml.Unmarshal([]byte(value), &data))
|
||||
return data, err
|
||||
}
|
||||
|
||||
func setHeadersParser(value string) (any, E.NestedError) {
|
||||
func yamlStringMappingParser(value string) (any, E.NestedError) {
|
||||
value = strings.TrimSpace(value)
|
||||
lines := strings.Split(value, "\n")
|
||||
h := make(http.Header)
|
||||
h := make(map[string]string)
|
||||
for _, line := range lines {
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
if len(parts) != 2 {
|
||||
return nil, E.Invalid("set header statement", line)
|
||||
}
|
||||
key := strings.TrimSpace(parts[0])
|
||||
vals := strings.Split(parts[1], ",")
|
||||
for i := range vals {
|
||||
h.Add(key, strings.TrimSpace(vals[i]))
|
||||
val := strings.TrimSpace(parts[1])
|
||||
if existing, ok := h[key]; ok {
|
||||
h[key] = existing + ", " + val
|
||||
} else {
|
||||
h[key] = val
|
||||
}
|
||||
}
|
||||
return h, E.Nil()
|
||||
@@ -56,8 +61,9 @@ const NSProxy = "proxy"
|
||||
var _ = func() int {
|
||||
RegisterNamespace(NSProxy, ValueParserMap{
|
||||
"aliases": commaSepParser,
|
||||
"set_headers": setHeadersParser,
|
||||
"hide_headers": yamlParser[[]string],
|
||||
"path_patterns": yamlListParser,
|
||||
"set_headers": yamlStringMappingParser,
|
||||
"hide_headers": yamlListParser,
|
||||
"no_tls_verify": boolParser,
|
||||
})
|
||||
return 0
|
||||
|
||||
@@ -2,7 +2,6 @@ package docker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -82,26 +81,22 @@ X-Custom-Header1: foo, bar
|
||||
X-Custom-Header1: baz
|
||||
X-Custom-Header2: boo`
|
||||
v = strings.TrimPrefix(v, "\n")
|
||||
h := make(http.Header, 0)
|
||||
h.Set("X-Custom-Header1", "foo")
|
||||
h.Add("X-Custom-Header1", "bar")
|
||||
h.Add("X-Custom-Header1", "baz")
|
||||
h.Set("X-Custom-Header2", "boo")
|
||||
h := map[string]string{
|
||||
"X-Custom-Header1": "foo, bar, baz",
|
||||
"X-Custom-Header2": "boo",
|
||||
}
|
||||
|
||||
pl, err := ParseLabel(makeLabel(NSProxy, "foo", "set_headers"), v)
|
||||
if err.IsNotNil() {
|
||||
t.Errorf("expected err=nil, got %s", err.Error())
|
||||
}
|
||||
hGot, ok := pl.Value.(http.Header)
|
||||
hGot, ok := pl.Value.(map[string]string)
|
||||
if !ok {
|
||||
t.Error("value is not http.Header")
|
||||
t.Errorf("value is not a map[string]string, but %T", pl.Value)
|
||||
return
|
||||
}
|
||||
for k, vWant := range h {
|
||||
vGot := hGot[k]
|
||||
if !reflect.DeepEqual(vGot, vWant) {
|
||||
t.Errorf("expected %s=%q, got %q", k, vWant, vGot)
|
||||
}
|
||||
if !reflect.DeepEqual(h, hGot) {
|
||||
t.Errorf("expected %v, got %v", h, hGot)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user