tests: use TestHTTPEndpoints helper more

This commit is contained in:
Vincent Bernat
2022-04-14 09:17:38 +02:00
parent ce9bd6a4da
commit 2bbeacec84
4 changed files with 80 additions and 96 deletions

View File

@@ -37,6 +37,7 @@ func Diff(a, b interface{}) string {
type HTTPEndpointCases []struct { type HTTPEndpointCases []struct {
URL string URL string
ContentType string ContentType string
StatusCode int
FirstLines []string FirstLines []string
} }
@@ -51,8 +52,12 @@ func TestHTTPEndpoints(t *testing.T, serverAddr net.Addr, cases HTTPEndpointCase
t.Fatalf("GET %s:\n%+v", tc.URL, err) t.Fatalf("GET %s:\n%+v", tc.URL, err)
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != 200 { if tc.StatusCode == 0 {
t.Fatalf("GET %s: got status code %d, not 200", tc.URL, resp.StatusCode) tc.StatusCode = 200
}
if resp.StatusCode != tc.StatusCode {
t.Fatalf("GET %s: got status code %d, not %d", tc.URL,
resp.StatusCode, tc.StatusCode)
} }
gotContentType := resp.Header.Get("Content-Type") gotContentType := resp.Header.Get("Content-Type")
if gotContentType != tc.ContentType { if gotContentType != tc.ContentType {

View File

@@ -2,7 +2,6 @@ package http_test
import ( import (
"fmt" "fmt"
"io/ioutil"
netHTTP "net/http" netHTTP "net/http"
"testing" "testing"
@@ -23,14 +22,13 @@ func TestHandler(t *testing.T) {
})) }))
// Check the HTTP server is running and answering metrics // Check the HTTP server is running and answering metrics
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/test", h.Address)) helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
if err != nil { {
t.Fatalf("GET /test:\n%+v", err) URL: "/test",
} ContentType: "text/plain; charset=utf-8",
defer resp.Body.Close() FirstLines: []string{"Hello !"},
if resp.StatusCode != 200 { },
t.Fatalf("GET /test: got status code %d, not 200", resp.StatusCode) })
}
gotMetrics := r.GetMetrics("akvorado_common_http_", "inflight_", "requests_total", "response_size") gotMetrics := r.GetMetrics("akvorado_common_http_", "inflight_", "requests_total", "response_size")
expectedMetrics := map[string]string{ expectedMetrics := map[string]string{
@@ -60,19 +58,13 @@ func TestGinRouter(t *testing.T) {
}) })
}) })
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/test", h.Address)) helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
if err != nil { {
t.Fatalf("GET /api/v0/test:\n%+v", err) URL: "/api/v0/test",
} ContentType: "application/json; charset=utf-8",
defer resp.Body.Close() FirstLines: []string{`{"message":"ping"}`},
if resp.StatusCode != 200 { },
t.Errorf("GET /api/v0/test: got status code %d, not 200", resp.StatusCode) })
}
body, _ := ioutil.ReadAll(resp.Body)
expected := `{"message":"ping"}`
if diff := helpers.Diff(string(body), expected); diff != "" {
t.Errorf("GET /api/v0/test (-got, +want):\n%s", diff)
}
} }
func TestGinRouterPanic(t *testing.T) { func TestGinRouterPanic(t *testing.T) {
@@ -83,17 +75,12 @@ func TestGinRouterPanic(t *testing.T) {
panic("heeeelp") panic("heeeelp")
}) })
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/test", h.Address)) helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
if err != nil { {
t.Fatalf("GET /api/v0/test:\n%+v", err) URL: "/api/v0/test",
} StatusCode: 500,
defer resp.Body.Close() ContentType: "",
if resp.StatusCode != 500 { FirstLines: []string{},
t.Errorf("GET /api/v0/test: got status code %d, not 500", resp.StatusCode) },
} })
body, _ := ioutil.ReadAll(resp.Body)
expected := ""
if diff := helpers.Diff(string(body), expected); diff != "" {
t.Errorf("GET /api/v0/test (-got, +want):\n%s", diff)
}
} }

View File

@@ -1,32 +1,21 @@
package console package console
import ( import (
"fmt"
netHTTP "net/http"
"testing" "testing"
"akvorado/common/helpers"
"akvorado/common/http" "akvorado/common/http"
"akvorado/common/reporter" "akvorado/common/reporter"
) )
func TestServeAssets(t *testing.T) { func TestServeAssets(t *testing.T) {
for _, live := range []bool{false, true} { for _, live := range []bool{false, true} {
cases := []struct {
Path string
Code int
}{
{"", 200},
{"something", 200},
{"assets/akvorado.399701ee.svg", 200},
{"assets/somethingelse.svg", 404},
}
for _, tc := range cases {
var name string var name string
switch live { switch live {
case true: case true:
name = fmt.Sprintf("livefs-%s", tc.Path) name = "livefs"
case false: case false:
name = fmt.Sprintf("embeddedfs-%s", tc.Path) name = "embeddedfs"
} }
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
r := reporter.NewMock(t) r := reporter.NewMock(t)
@@ -40,15 +29,26 @@ func TestServeAssets(t *testing.T) {
t.Fatalf("New() error:\n%+v", err) t.Fatalf("New() error:\n%+v", err)
} }
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/%s", h.Address, tc.Path)) helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
if err != nil { {
t.Fatalf("GET /%s:\n%+v", tc.Path, err) URL: "/",
} ContentType: "text/html; charset=utf-8",
defer resp.Body.Close() FirstLines: []string{"<!DOCTYPE html>"},
if resp.StatusCode != tc.Code { }, {
t.Errorf("GET /%s: got status code %d, not %d", tc.Path, resp.StatusCode, tc.Code) URL: "/something",
} ContentType: "text/html; charset=utf-8",
FirstLines: []string{"<!DOCTYPE html>"},
}, {
URL: "/assets/akvorado.399701ee.svg",
ContentType: "image/svg+xml",
FirstLines: []string{`<?xml version="1.0" encoding="UTF-8" standalone="no"?>`},
}, {
URL: "/assets/somethingelse.svg",
StatusCode: 404,
ContentType: "text/plain; charset=utf-8",
FirstLines: []string{"404 page not found"},
},
})
}) })
} }
} }
}

View File

@@ -1,9 +1,6 @@
package broker package broker
import ( import (
"fmt"
"io/ioutil"
netHTTP "net/http"
"testing" "testing"
"akvorado/common/helpers" "akvorado/common/helpers"
@@ -25,21 +22,16 @@ func TestConfigurationEndpoint(t *testing.T) {
"bye": "Goodbye world!", "bye": "Goodbye world!",
}) })
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/orchestrator/broker/configuration/inlet", h.Address)) helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
if err != nil { {
t.Fatalf("GET /api/v0/orchestrator/broker/configuration/inlet:\n%+v", err) URL: "/api/v0/orchestrator/broker/configuration/inlet",
} ContentType: "application/json; charset=utf-8",
defer resp.Body.Close() FirstLines: []string{
if resp.StatusCode != 200 { "{",
t.Errorf("GET /api/v0/orchestrator/broker/configuration/inlet: got status code %d, not 200", ` "bye": "Goodbye world!",`,
resp.StatusCode) ` "hello": "Hello world!"`,
} "}",
body, _ := ioutil.ReadAll(resp.Body) },
expected := `{ },
"bye": "Goodbye world!", })
"hello": "Hello world!"
}`
if diff := helpers.Diff(string(body), expected); diff != "" {
t.Errorf("GET /api/v0/orchestrator/broker/configuration/inlet (-got, +want):\n%s", diff)
}
} }