internal/test: remove redundant API interface

test.API is actually just http.Handler, let's use this interface directly
instead of defining our own one.

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
This commit is contained in:
Ondřej Budai 2020-11-25 13:38:37 +01:00 committed by Tom Gundersen
parent dfe748265d
commit 04c239246a

View file

@ -22,10 +22,6 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type API interface {
ServeHTTP(writer http.ResponseWriter, request *http.Request)
}
func externalRequest(method, path, body string) *http.Response { func externalRequest(method, path, body string) *http.Response {
client := http.Client{ client := http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
@ -52,7 +48,7 @@ func externalRequest(method, path, body string) *http.Response {
return resp return resp
} }
func internalRequest(api API, method, path, body string) *http.Response { func internalRequest(api http.Handler, method, path, body string) *http.Response {
req := httptest.NewRequest(method, path, bytes.NewReader([]byte(body))) req := httptest.NewRequest(method, path, bytes.NewReader([]byte(body)))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
@ -61,7 +57,7 @@ func internalRequest(api API, method, path, body string) *http.Response {
return resp.Result() return resp.Result()
} }
func SendHTTP(api API, external bool, method, path, body string) *http.Response { func SendHTTP(api http.Handler, external bool, method, path, body string) *http.Response {
if len(os.Getenv("OSBUILD_COMPOSER_TEST_EXTERNAL")) > 0 { if len(os.Getenv("OSBUILD_COMPOSER_TEST_EXTERNAL")) > 0 {
if !external { if !external {
return nil return nil
@ -102,7 +98,7 @@ func dropFields(obj interface{}, fields ...string) {
} }
} }
func TestRoute(t *testing.T, api API, external bool, method, path, body string, expectedStatus int, expectedJSON string, ignoreFields ...string) { func TestRoute(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedJSON string, ignoreFields ...string) {
t.Helper() t.Helper()
resp := SendHTTP(api, external, method, path, body) resp := SendHTTP(api, external, method, path, body)
@ -140,7 +136,7 @@ func TestRoute(t *testing.T, api API, external bool, method, path, body string,
require.Equal(t, expected, reply) require.Equal(t, expected, reply)
} }
func TestTOMLRoute(t *testing.T, api API, external bool, method, path, body string, expectedStatus int, expectedTOML string, ignoreFields ...string) { func TestTOMLRoute(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedTOML string, ignoreFields ...string) {
t.Helper() t.Helper()
resp := SendHTTP(api, external, method, path, body) resp := SendHTTP(api, external, method, path, body)
@ -174,7 +170,7 @@ func TestTOMLRoute(t *testing.T, api API, external bool, method, path, body stri
require.Equal(t, expected, reply) require.Equal(t, expected, reply)
} }
func TestNonJsonRoute(t *testing.T, api API, external bool, method, path, body string, expectedStatus int, expectedResponse string) { func TestNonJsonRoute(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedResponse string) {
response := SendHTTP(api, external, method, path, body) response := SendHTTP(api, external, method, path, body)
assert.Equalf(t, expectedStatus, response.StatusCode, "%s: status mismatch", path) assert.Equalf(t, expectedStatus, response.StatusCode, "%s: status mismatch", path)