Skip to content

Commit

Permalink
Use predefined http constants. Add tests to http backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
iegomez committed Jun 5, 2022
1 parent 963a5cc commit 065ec97
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
12 changes: 6 additions & 6 deletions backends/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}

Expand Down
29 changes: 29 additions & 0 deletions backends/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions backends/jwt_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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
}
}
Expand Down

0 comments on commit 065ec97

Please sign in to comment.