Plugins: Refactor call resource API handling (#67234)
Moving call resource API stream handling within plugin management as a utility/wrapper. Closes #66889 Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
This commit is contained in:
co-authored by
Will Browne
parent
b5fbce50b3
commit
4cbda914bd
@@ -0,0 +1,45 @@
|
||||
package httpresponsesender
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestHTTPResponseSender(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
sender := New(w)
|
||||
require.NotNil(t, sender)
|
||||
|
||||
headers := http.Header{}
|
||||
headers.Add("X-Custom", "custom")
|
||||
err := sender.Send(&backend.CallResourceResponse{
|
||||
Status: http.StatusOK,
|
||||
Headers: headers,
|
||||
Body: []byte("Hello world"),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
headers2 := http.Header{}
|
||||
headers2.Add("X-Custom-Two", "custom two")
|
||||
err = sender.Send(&backend.CallResourceResponse{
|
||||
Status: http.StatusNotFound,
|
||||
Headers: headers2,
|
||||
Body: []byte("Hello world again"),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
resp := w.Result()
|
||||
require.NotNil(t, resp)
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
require.Equal(t, "custom", resp.Header.Get("X-Custom"))
|
||||
require.Empty(t, resp.Header.Get("X-Custom-Two"))
|
||||
bytes, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "Hello worldHello world again", string(bytes))
|
||||
}
|
||||
Reference in New Issue
Block a user