Use predefined http constants. Add tests to http backend.

This commit is contained in:
Ignacio Gómez 2022-06-05 18:39:10 -04:00
parent 963a5ccb85
commit 065ec97ee3
No known key found for this signature in database
GPG Key ID: 15A77C6BEC604B06
3 changed files with 38 additions and 8 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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
}
}