diff --git a/pkg/api/plugin_resource.go b/pkg/api/plugin_resource.go index bccbb034ea8..25e2089090e 100644 --- a/pkg/api/plugin_resource.go +++ b/pkg/api/plugin_resource.go @@ -193,7 +193,7 @@ func (hs *HTTPServer) flushStream(stream callResourceClientResponseStream, w htt // Expected that headers and status are only part of first stream if processedStreams == 0 && resp.Headers != nil { // Make sure a content type always is returned in response - if _, exists := resp.Headers["Content-Type"]; !exists { + if _, exists := resp.Headers["Content-Type"]; !exists && resp.Status != http.StatusNoContent { resp.Headers["Content-Type"] = []string{"application/json"} } diff --git a/pkg/api/plugins_test.go b/pkg/api/plugins_test.go index a0871d07d92..ab9a5f977c2 100644 --- a/pkg/api/plugins_test.go +++ b/pkg/api/plugins_test.go @@ -331,10 +331,35 @@ func TestMakePluginResourceRequest(t *testing.T) { } } + require.Equal(t, resp.Header().Get("Content-Type"), "application/json") require.Equal(t, "sandbox", resp.Header().Get("Content-Security-Policy")) require.Empty(t, req.Header.Get(customHeader)) } +func TestMakePluginResourceRequestContentTypeEmpty(t *testing.T) { + pluginClient := &fakePluginClient{ + statusCode: http.StatusNoContent, + } + hs := HTTPServer{ + Cfg: setting.NewCfg(), + log: log.New(), + pluginClient: pluginClient, + } + req := httptest.NewRequest(http.MethodGet, "/", nil) + resp := httptest.NewRecorder() + pCtx := backend.PluginContext{} + err := hs.makePluginResourceRequest(resp, req, pCtx) + require.NoError(t, err) + + for { + if resp.Flushed { + break + } + } + + require.Zero(t, resp.Header().Get("Content-Type")) +} + func callGetPluginAsset(sc *scenarioContext) { sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec() } @@ -366,6 +391,8 @@ type fakePluginClient struct { req *backend.CallResourceRequest backend.QueryDataHandlerFunc + + statusCode int } func (c *fakePluginClient) CallResource(_ context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { @@ -377,8 +404,13 @@ func (c *fakePluginClient) CallResource(_ context.Context, req *backend.CallReso return err } + statusCode := http.StatusOK + if c.statusCode != 0 { + statusCode = c.statusCode + } + return sender.Send(&backend.CallResourceResponse{ - Status: http.StatusOK, + Status: statusCode, Headers: make(map[string][]string), Body: bytes, })