From de0b92ebc92e085215b73fb7f4963da3dba3f1b8 Mon Sep 17 00:00:00 2001 From: Brett Buddin Date: Fri, 6 Mar 2020 12:18:50 -0500 Subject: [PATCH] fix(http): Revert d51447fc3495b2c84e3b6599b0c76ab7c9cb31fc. --- http/query.go | 18 +++++--------- http/query_handler_test.go | 6 +---- http/query_test.go | 48 ++++---------------------------------- 3 files changed, 12 insertions(+), 60 deletions(-) diff --git a/http/query.go b/http/query.go index 129921e719..d4ab3e4505 100644 --- a/http/query.go +++ b/http/query.go @@ -366,21 +366,15 @@ func QueryRequestFromProxyRequest(req *query.ProxyRequest) (*QueryRequest, error return qr, nil } -var ( - errNoContentType = errors.New("no Content-Type header provided") - errUnsupportedContentType = errors.New("unsupported Content-Type header value") -) - func decodeQueryRequest(ctx context.Context, r *http.Request, svc influxdb.OrganizationService) (*QueryRequest, int, error) { var req QueryRequest body := &countReader{Reader: r.Body} - ct := r.Header.Get("Content-Type") - if ct == "" { - return nil, body.bytesRead, errNoContentType + var contentType = "application/json" + if ct := r.Header.Get("Content-Type"); ct != "" { + contentType = ct } - - mt, _, err := mime.ParseMediaType(ct) + mt, _, err := mime.ParseMediaType(contentType) if err != nil { return nil, body.bytesRead, err } @@ -392,11 +386,11 @@ func decodeQueryRequest(ctx context.Context, r *http.Request, svc influxdb.Organ } req.Query = string(octets) case "application/json": + fallthrough + default: if err := json.NewDecoder(body).Decode(&req); err != nil { return nil, body.bytesRead, err } - default: - return nil, body.bytesRead, errUnsupportedContentType } switch hv := r.Header.Get(query.PreferHeaderKey); hv { diff --git a/http/query_handler_test.go b/http/query_handler_test.go index 746e6b9b39..f838f3e326 100644 --- a/http/query_handler_test.go +++ b/http/query_handler_test.go @@ -243,11 +243,7 @@ func TestFluxHandler_postFluxAST(t *testing.T) { { name: "get ast from()", w: httptest.NewRecorder(), - r: func() *http.Request { - r := httptest.NewRequest("POST", "/api/v2/query/ast", bytes.NewBufferString(`{"query": "from()"}`)) - r.Header.Set("Content-Type", "application/json") - return r - }(), + r: httptest.NewRequest("POST", "/api/v2/query/ast", bytes.NewBufferString(`{"query": "from()"}`)), want: `{"ast":{"type":"Package","package":"main","files":[{"type":"File","location":{"start":{"line":1,"column":1},"end":{"line":1,"column":7},"source":"from()"},"metadata":"parser-type=rust","package":null,"imports":null,"body":[{"type":"ExpressionStatement","location":{"start":{"line":1,"column":1},"end":{"line":1,"column":7},"source":"from()"},"expression":{"type":"CallExpression","location":{"start":{"line":1,"column":1},"end":{"line":1,"column":7},"source":"from()"},"callee":{"type":"Identifier","location":{"start":{"line":1,"column":1},"end":{"line":1,"column":5},"source":"from"},"name":"from"}}}]}]}} `, status: http.StatusOK, diff --git a/http/query_test.go b/http/query_test.go index 0d6a645ffc..714e4ae842 100644 --- a/http/query_test.go +++ b/http/query_test.go @@ -400,11 +400,7 @@ func Test_decodeQueryRequest(t *testing.T) { { name: "valid query request", args: args{ - r: func() *http.Request { - r := httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"query": "from()"}`)) - r.Header.Set("Content-Type", "application/json") - return r - }(), + r: httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"query": "from()"}`)), svc: &mock.OrganizationService{ FindOrganizationF: func(ctx context.Context, filter platform.OrganizationFilter) (*platform.Organization, error) { return &platform.Organization{ @@ -458,43 +454,17 @@ func Test_decodeQueryRequest(t *testing.T) { { name: "error decoding json", args: args{ - r: func() *http.Request { - r := httptest.NewRequest("POST", "/", bytes.NewBufferString(`error`)) - r.Header.Set("Content-Type", "application/json") - return r - }(), + r: httptest.NewRequest("POST", "/", bytes.NewBufferString(`error`)), }, wantErr: true, }, { name: "error validating query", - args: args{ - r: func() *http.Request { - r := httptest.NewRequest("POST", "/", bytes.NewBufferString(`{}`)) - r.Header.Set("Content-Type", "application/json") - return r - }(), - }, - wantErr: true, - }, - { - name: "no content-type provided", args: args{ r: httptest.NewRequest("POST", "/", bytes.NewBufferString(`{}`)), }, wantErr: true, }, - { - name: "unsupported content-type", - args: args{ - r: func() *http.Request { - r := httptest.NewRequest("POST", "/", bytes.NewBufferString(`{}`)) - r.Header.Set("Content-Type", "plain/text") - return r - }(), - }, - wantErr: true, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -526,11 +496,7 @@ func Test_decodeProxyQueryRequest(t *testing.T) { { name: "valid post query request", args: args{ - r: func() *http.Request { - r := httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"query": "from()"}`)) - r.Header.Set("Content-Type", "application/json") - return r - }(), + r: httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"query": "from()"}`)), svc: &mock.OrganizationService{ FindOrganizationF: func(ctx context.Context, filter platform.OrganizationFilter) (*platform.Organization, error) { return &platform.Organization{ @@ -557,8 +523,7 @@ func Test_decodeProxyQueryRequest(t *testing.T) { { name: "valid query including extern definition", args: args{ - r: func() *http.Request { - r := httptest.NewRequest("POST", "/", bytes.NewBufferString(` + r: httptest.NewRequest("POST", "/", bytes.NewBufferString(` { "extern": { "type": "File", @@ -581,10 +546,7 @@ func Test_decodeProxyQueryRequest(t *testing.T) { }, "query": "from(bucket: \"mybucket\")" } -`)) - r.Header.Set("Content-Type", "application/json") - return r - }(), +`)), svc: &mock.OrganizationService{ FindOrganizationF: func(ctx context.Context, filter platform.OrganizationFilter) (*platform.Organization, error) { return &platform.Organization{