560be84dc8
* CloudWatch: Backport aws-sdk-go-v2 update from external plugin (#107136) (cherry picked froma18ea34688) * Datasources: Update grafana-aws-sdk for new sigv4 middleware and aws-sdk-go v1 removal (#107522) (cherry picked from commit66d9a33cc9) * CloudWatch: Fix proxy transport issue (#107807) (cherry picked fromc3eeb1fcd9) * CloudWatch: Fix http client handling + assume role bug (#107893) (cherry picked from commitf34a9fc0c2) * CloudWatch: Use default region when query region is unset (#109089) (cherry picked from commit5f4097a159) * CloudWatch: Fix handling region for legacy alerts (#109217) (cherry picked from commit2bf9aea8ef) * Update go.mod owners --------- Co-authored-by: Isabella Siu <Isabella.siu@grafana.com>
40 lines
1.3 KiB
Go
40 lines
1.3 KiB
Go
package cloudwatch
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
|
)
|
|
|
|
func Test_Middleware(t *testing.T) {
|
|
t.Run("rejects POST method", func(t *testing.T) {
|
|
rr := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", "/dimension-keys?region=us-east-1", nil)
|
|
ds := newTestDatasource()
|
|
handler := http.HandlerFunc(ds.resourceRequestMiddleware(func(_ context.Context, parameters url.Values) ([]byte, *models.HttpError) {
|
|
return []byte{}, nil
|
|
}))
|
|
handler.ServeHTTP(rr, req)
|
|
assert.Equal(t, http.StatusMethodNotAllowed, rr.Code)
|
|
})
|
|
|
|
t.Run("should propagate handler error to response", func(t *testing.T) {
|
|
rr := httptest.NewRecorder()
|
|
req := httptest.NewRequest("GET", "/some-path", nil)
|
|
ds := newTestDatasource()
|
|
handler := http.HandlerFunc(ds.resourceRequestMiddleware(func(_ context.Context, parameters url.Values) ([]byte, *models.HttpError) {
|
|
return []byte{}, models.NewHttpError("error", http.StatusBadRequest, fmt.Errorf("error from handler"))
|
|
}))
|
|
handler.ServeHTTP(rr, req)
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
assert.Equal(t, `{"Message":"error: error from handler","Error":"error from handler","StatusCode":400}`, rr.Body.String())
|
|
})
|
|
}
|