1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-24 13:48:49 +08:00

NOISSUE - Make application/json content-type valid in http-adapter (#1606)

* NOISSUE - Make application/json content-type valid in http-adapter

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add test

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add CBOR content-type

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix naming

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix naming

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix CI

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix CI flag

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix CI install

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Upgrade grpc version

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* rm cli

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
This commit is contained in:
Manuel Imperiale 2022-06-13 12:04:48 +02:00 committed by GitHub
parent 6c59184d3f
commit 5b8780de4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 12 deletions

View File

@ -63,10 +63,14 @@ func (tr testRequest) make() (*http.Response, error) {
func TestPublish(t *testing.T) {
chanID := "1"
contentType := "application/senml+json"
ctSenmlJSON := "application/senml+json"
ctSenmlCBOR := "application/senml+cbor"
ctJSON := "application/json"
thingKey := "thing_key"
invalidKey := "invalid_key"
msg := `[{"n":"current","t":-1,"v":1.6}]`
msgJSON := `{"field1":"val1","field2":"val2"}`
msgCBOR := `81A3616E6763757272656E746174206176FB3FF999999999999A`
thingsClient := mocks.NewThingsClient(map[string]string{thingKey: chanID})
svc := newService(thingsClient)
ts := newHTTPServer(svc)
@ -83,21 +87,35 @@ func TestPublish(t *testing.T) {
"publish message": {
chanID: chanID,
msg: msg,
contentType: contentType,
contentType: ctSenmlJSON,
key: thingKey,
status: http.StatusAccepted,
},
"publish message with application/senml+cbor content-type": {
chanID: chanID,
msg: msgCBOR,
contentType: ctSenmlCBOR,
key: thingKey,
status: http.StatusAccepted,
},
"publish message with application/json content-type": {
chanID: chanID,
msg: msgJSON,
contentType: ctJSON,
key: thingKey,
status: http.StatusAccepted,
},
"publish message with empty key": {
chanID: chanID,
msg: msg,
contentType: contentType,
contentType: ctSenmlJSON,
key: "",
status: http.StatusUnauthorized,
},
"publish message with basic auth": {
chanID: chanID,
msg: msg,
contentType: contentType,
contentType: ctSenmlJSON,
key: thingKey,
basicAuth: true,
status: http.StatusAccepted,
@ -105,14 +123,14 @@ func TestPublish(t *testing.T) {
"publish message with invalid key": {
chanID: chanID,
msg: msg,
contentType: contentType,
contentType: ctSenmlJSON,
key: invalidKey,
status: http.StatusUnauthorized,
},
"publish message with invalid basic auth": {
chanID: chanID,
msg: msg,
contentType: contentType,
contentType: ctSenmlJSON,
key: invalidKey,
basicAuth: true,
status: http.StatusUnauthorized,
@ -127,14 +145,14 @@ func TestPublish(t *testing.T) {
"publish message to invalid channel": {
chanID: "",
msg: msg,
contentType: contentType,
contentType: ctSenmlJSON,
key: thingKey,
status: http.StatusBadRequest,
},
"publish message unable to authorize": {
chanID: chanID,
msg: msg,
contentType: contentType,
contentType: ctSenmlJSON,
key: mocks.ServiceErrToken,
status: http.StatusInternalServerError,
},

View File

@ -30,7 +30,9 @@ import (
const (
protocol = "http"
contentType = "application/senml+json"
ctSenmlJSON = "application/senml+json"
ctSenmlCBOR = "application/senml+cbor"
ctJSON = "application/json"
)
var (
@ -96,7 +98,8 @@ func parseSubtopic(subtopic string) (string, error) {
}
func decodeRequest(ctx context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
ct := r.Header.Get("Content-Type")
if ct != ctSenmlJSON && ct != ctJSON && ct != ctSenmlCBOR {
return nil, errors.ErrUnsupportedContentType
}
@ -176,7 +179,7 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
}
if errorVal, ok := err.(errors.Error); ok {
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Type", ctJSON)
if err := json.NewEncoder(w).Encode(apiutil.ErrorRes{Err: errorVal.Msg()}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}

View File

@ -21,7 +21,7 @@ func (sdk mfSDK) SendMessage(chanName, msg, key string) error {
subtopicPart = fmt.Sprintf("/%s", strings.Replace(chanNameParts[1], ".", "/", -1))
}
url := fmt.Sprintf("%s/channels/%s/messages%s", sdk.httpAdapterURL, chanID, subtopicPart)
url := fmt.Sprintf("%s/channels/%s/messages/%s", sdk.httpAdapterURL, chanID, subtopicPart)
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(msg))
if err != nil {