From 065ec97ee3749a81810e0e264fc9e5e632386640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20G=C3=B3mez?= Date: Sun, 5 Jun 2022 18:39:10 -0400 Subject: [PATCH] Use predefined http constants. Add tests to http backend. --- backends/http.go | 12 ++++++------ backends/http_test.go | 29 +++++++++++++++++++++++++++++ backends/jwt_remote.go | 5 +++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/backends/http.go b/backends/http.go index 63c1509..13257e0 100644 --- a/backends/http.go +++ b/backends/http.go @@ -46,7 +46,7 @@ func NewHTTP(authOpts map[string]string, logLevel log.Level, version string) (HT VerifyPeer: false, ResponseMode: "status", ParamsMode: "json", - httpMethod: "POST", + httpMethod: h.MethodPost, } missingOpts := "" @@ -65,7 +65,8 @@ func NewHTTP(authOpts map[string]string, logLevel log.Level, version string) (HT } if httpMethod, ok := authOpts["http_method"]; ok { - if httpMethod == "POST" || httpMethod == "GET" || httpMethod == "PUT" { + switch httpMethod { + case h.MethodGet, h.MethodPut: http.httpMethod = httpMethod } } @@ -218,10 +219,9 @@ func (o HTTP) httpRequest(uri, username string, dataMap map[string]interface{}, var err error if o.ParamsMode == "form" { - if o.httpMethod != "POST" { - log.Errorf("error form param only supported for POST.") - err = fmt.Errorf("form only supported for POST, error code: %d", - 500) + if o.httpMethod != h.MethodPost && o.httpMethod != h.MethodPut { + log.Errorf("error form param only supported for POST/PUT.") + err = fmt.Errorf("form only supported for POST/PUT, error code: %d", 500) return false, err } diff --git a/backends/http_test.go b/backends/http_test.go index e007e0d..6a3fa56 100644 --- a/backends/http_test.go +++ b/backends/http_test.go @@ -103,6 +103,7 @@ func TestHTTPAllJsonServer(t *testing.T) { hb, err := NewHTTP(authOpts, log.DebugLevel, version) So(err, ShouldBeNil) So(hb.UserAgent, ShouldEqual, "mosquitto-2.0.0") + So(hb.httpMethod, ShouldEqual, http.MethodPost) Convey("Given custom user agent, it should override default one", func() { customAuthOpts := make(map[string]string) @@ -118,6 +119,34 @@ func TestHTTPAllJsonServer(t *testing.T) { So(customHb.UserAgent, ShouldEqual, "custom-user-agent") }) + Convey("Given http method GET, it should override the default POST one", func() { + customAuthOpts := make(map[string]string) + + for k, v := range authOpts { + customAuthOpts[k] = v + } + + customAuthOpts["http_method"] = "GET" + + customHb, err := NewHTTP(customAuthOpts, log.DebugLevel, version) + So(err, ShouldBeNil) + So(customHb.httpMethod, ShouldEqual, http.MethodGet) + }) + + Convey("Given http method PUT, it should override the default POST one", func() { + customAuthOpts := make(map[string]string) + + for k, v := range authOpts { + customAuthOpts[k] = v + } + + customAuthOpts["http_method"] = "PUT" + + customHb, err := NewHTTP(customAuthOpts, log.DebugLevel, version) + So(err, ShouldBeNil) + So(customHb.httpMethod, ShouldEqual, http.MethodPut) + }) + Convey("Given correct password/username, get user should return true", func() { authenticated, err := hb.GetUser(username, password, clientId) diff --git a/backends/jwt_remote.go b/backends/jwt_remote.go index d1e857c..f59aa69 100644 --- a/backends/jwt_remote.go +++ b/backends/jwt_remote.go @@ -46,7 +46,7 @@ func NewRemoteJWTChecker(authOpts map[string]string, options tokenOptions, versi verifyPeer: false, responseMode: "status", paramsMode: "json", - httpMethod: "POST", + httpMethod: h.MethodPost, options: options, } @@ -66,7 +66,8 @@ func NewRemoteJWTChecker(authOpts map[string]string, options tokenOptions, versi } if httpMethod, ok := authOpts["jwt_http_method"]; ok { - if httpMethod == "POST" || httpMethod == "GET" || httpMethod == "PUT" { + switch httpMethod { + case h.MethodGet, h.MethodPut: checker.httpMethod = httpMethod } }