fix(http): Revert d51447fc34.

pull/17121/head
Brett Buddin 2020-03-06 12:18:50 -05:00
parent 148b55b9f6
commit de0b92ebc9
No known key found for this signature in database
GPG Key ID: C51265E441C4C5AC
3 changed files with 12 additions and 60 deletions

View File

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

View File

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

View File

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