mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 02:10:45 -06:00
3fb6319d1b
* Move files to prometheus-library * refactor core prometheus to use prometheus-library * modify client transport options * mock * have a type * import aliases * rename * call the right method * remove unrelated test from the library * update codeowners * go work sync * update go.work.sum * make swagger-clean && make openapi3-gen * add promlib to makefile * remove clilogger * Export the function * update unit test * add prometheus_test.go * fix mock type * use mapUtil from grafana-plugin-sdk-go
45 lines
1.4 KiB
Go
45 lines
1.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestEnsureHttpMethodMiddleware(t *testing.T) {
|
|
t.Run("Name should be correct", func(t *testing.T) {
|
|
finalRoundTripper := httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
|
})
|
|
mw := ForceHttpGet(backend.NewLoggerWith("logger", "test"))
|
|
rt := mw.CreateMiddleware(httpclient.Options{}, finalRoundTripper)
|
|
require.NotNil(t, rt)
|
|
middlewareName, ok := mw.(httpclient.MiddlewareName)
|
|
require.True(t, ok)
|
|
require.Equal(t, "force-http-get", middlewareName.MiddlewareName())
|
|
})
|
|
|
|
t.Run("Should force GET method", func(t *testing.T) {
|
|
finalRoundTripper := httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
|
})
|
|
|
|
mw := ForceHttpGet(backend.NewLoggerWith("logger", "test"))
|
|
rt := mw.CreateMiddleware(httpclient.Options{}, finalRoundTripper)
|
|
require.NotNil(t, rt)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, "http://example.com", nil)
|
|
require.NoError(t, err)
|
|
res, err := rt.RoundTrip(req)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, res)
|
|
require.Equal(t, res.StatusCode, http.StatusMethodNotAllowed)
|
|
if res.Body != nil {
|
|
require.NoError(t, res.Body.Close())
|
|
}
|
|
})
|
|
}
|