diff --git a/go.mod b/go.mod index 7332cec..715dff4 100644 --- a/go.mod +++ b/go.mod @@ -16,13 +16,13 @@ require ( require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/sagikazarmark/locafero v0.3.0 // indirect diff --git a/go.sum b/go.sum index 2ecd4e4..acd0b97 100644 --- a/go.sum +++ b/go.sum @@ -59,8 +59,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= -github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= -github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk= github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -153,8 +153,9 @@ github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3v github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= @@ -342,7 +343,6 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= diff --git a/httputil/wrap_writer.go b/httputil/wrap_writer.go index d4042c3..54af9a4 100644 --- a/httputil/wrap_writer.go +++ b/httputil/wrap_writer.go @@ -26,7 +26,7 @@ type WriterProxy interface { // Writes will be sent to the proxy before being written to this // io.Writer. It is illegal for the tee'd writer to be modified // concurrently with writes. - Tee(io.Writer) + Tee(w io.Writer) // Unwrap returns the original proxied target. Unwrap() http.ResponseWriter } diff --git a/params/params.go b/params/params.go index 6d539bc..c83bd89 100644 --- a/params/params.go +++ b/params/params.go @@ -16,15 +16,21 @@ var ErrNotFound = errors.New("not found") func GetString(r *http.Request, name string, required bool) (string, bool, error) { param := chi.URLParam(r, name) - if len(param) == 0 { + + if param == "" { param = r.URL.Query().Get(name) } + // fallback to a header lookup + if param == "" { + param = r.Header.Get(name) + } + if required && len(param) == 0 { return "", false, fmt.Errorf("%s: %w", name, ErrNotFound) } - return param, true, nil + return param, param != "", nil } func GetInt32(r *http.Request, name string, required bool) (int32, bool, error) { diff --git a/params/params_test.go b/params/params_test.go index 1f1cc2c..c35c980 100644 --- a/params/params_test.go +++ b/params/params_test.go @@ -12,6 +12,41 @@ import ( "github.com/stretchr/testify/assert" ) +func TestGetString(t *testing.T) { + newHeaderRequest := func(key, value string) *http.Request { + r := httptest.NewRequest("GET", "/ping", nil) + if key != "" { + r.Header.Set(key, value) + } + + return r + } + + tests := []struct { + name string + r *http.Request + param string + required bool + want string + wantErr bool + wantOk bool + }{ + {" header required present", newHeaderRequest("foo", "123"), "foo", true, "123", false, true}, + {" header required missing", newHeaderRequest("", ""), "foo", true, "", true, false}, + {" header not required present", newHeaderRequest("foo", "123"), "foo", false, "123", false, true}, + {" header not required missing", newHeaderRequest("", ""), "foo", false, "", false, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok, err := GetString(tt.r, tt.param, tt.required) + + assert.Equal(t, tt.wantErr, err != nil, "error") + assert.Equal(t, tt.want, got, "value") + assert.Equal(t, tt.wantOk, ok, "ok") + }) + } +} + func TestGetInt32(t *testing.T) { tests := []struct { name string