mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 23:42:51 +08:00
Chore: Disable default golangci-lint filter (#29751)
* Disable default golangci-lint filter Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Chore: Fix linter warnings Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
@ -14,8 +14,9 @@ import (
|
||||
|
||||
func TestHandleResponse(t *testing.T) {
|
||||
t.Run("Returns body if status == 200", func(t *testing.T) {
|
||||
resp := makeResponse(200, "test")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 200, "test")
|
||||
bodyReader, err := handleResponse(resp)
|
||||
require.NoError(t, err)
|
||||
body, err := ioutil.ReadAll(bodyReader)
|
||||
@ -24,54 +25,68 @@ func TestHandleResponse(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Returns ErrorNotFound if status == 404", func(t *testing.T) {
|
||||
resp := makeResponse(404, "")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 404, "")
|
||||
_, err := handleResponse(resp)
|
||||
assert.Equal(t, ErrNotFoundError, err)
|
||||
})
|
||||
|
||||
t.Run("Returns message from body if status == 400", func(t *testing.T) {
|
||||
resp := makeResponse(400, "{ \"message\": \"error_message\" }")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 400, "{ \"message\": \"error_message\" }")
|
||||
_, err := handleResponse(resp)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "error_message", asBadRequestError(t, err).Message)
|
||||
})
|
||||
|
||||
t.Run("Returns body if status == 400 and no message key", func(t *testing.T) {
|
||||
resp := makeResponse(400, "{ \"test\": \"test_message\"}")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 400, "{ \"test\": \"test_message\"}")
|
||||
_, err := handleResponse(resp)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "{ \"test\": \"test_message\"}", asBadRequestError(t, err).Message)
|
||||
})
|
||||
|
||||
t.Run("Returns Bad request error if status == 400 and no body", func(t *testing.T) {
|
||||
resp := makeResponse(400, "")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 400, "")
|
||||
_, err := handleResponse(resp)
|
||||
require.Error(t, err)
|
||||
_ = asBadRequestError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Returns error with invalid status if status == 500", func(t *testing.T) {
|
||||
resp := makeResponse(500, "")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 500, "")
|
||||
_, err := handleResponse(resp)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid status")
|
||||
})
|
||||
}
|
||||
|
||||
func makeResponse(status int, body string) *http.Response {
|
||||
func makeResponse(t *testing.T, status int, body string) *http.Response {
|
||||
t.Helper()
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: status,
|
||||
Body: makeBody(body),
|
||||
Body: makeBody(t, body),
|
||||
}
|
||||
}
|
||||
|
||||
func makeBody(body string) io.ReadCloser {
|
||||
return ioutil.NopCloser(bytes.NewReader([]byte(body)))
|
||||
func makeBody(t *testing.T, body string) io.ReadCloser {
|
||||
t.Helper()
|
||||
|
||||
reader := ioutil.NopCloser(bytes.NewReader([]byte(body)))
|
||||
t.Cleanup(func() {
|
||||
err := reader.Close()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
return reader
|
||||
}
|
||||
|
||||
func asBadRequestError(t *testing.T, err error) *BadRequestError {
|
||||
|
Reference in New Issue
Block a user