From 5b8780de4a6a8f04c7523886fe6bf7ee3728187c Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Mon, 13 Jun 2022 12:04:48 +0200 Subject: [PATCH] NOISSUE - Make application/json content-type valid in http-adapter (#1606) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * NOISSUE - Make application/json content-type valid in http-adapter Signed-off-by: Manuel Imperiale * Add test Signed-off-by: Manuel Imperiale * Add CBOR content-type Signed-off-by: Manuel Imperiale * Fix naming Signed-off-by: Manuel Imperiale * Fix naming Signed-off-by: Manuel Imperiale * Fix CI Signed-off-by: Manuel Imperiale * Fix CI flag Signed-off-by: Manuel Imperiale * Fix CI install Signed-off-by: Manuel Imperiale * Upgrade grpc version Signed-off-by: Manuel Imperiale * Fix typo Signed-off-by: Manuel Imperiale * rm cli Signed-off-by: Manuel Imperiale Co-authored-by: Dušan Borovčanin --- http/api/endpoint_test.go | 34 ++++++++++++++++++++++++++-------- http/api/transport.go | 9 ++++++--- pkg/sdk/go/message.go | 2 +- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/http/api/endpoint_test.go b/http/api/endpoint_test.go index cdfb29c0..74bd55bf 100644 --- a/http/api/endpoint_test.go +++ b/http/api/endpoint_test.go @@ -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, }, diff --git a/http/api/transport.go b/http/api/transport.go index f13a9a9c..31846549 100644 --- a/http/api/transport.go +++ b/http/api/transport.go @@ -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) } diff --git a/pkg/sdk/go/message.go b/pkg/sdk/go/message.go index 3dcf1106..a8a2298f 100644 --- a/pkg/sdk/go/message.go +++ b/pkg/sdk/go/message.go @@ -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 {