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 {
URL string
ContentType string
StatusCode int
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)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("GET %s: got status code %d, not 200", tc.URL, resp.StatusCode)
if tc.StatusCode == 0 {
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")
if gotContentType != tc.ContentType {

View File

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

View File

@@ -1,32 +1,21 @@
package console
import (
"fmt"
netHTTP "net/http"
"testing"
"akvorado/common/helpers"
"akvorado/common/http"
"akvorado/common/reporter"
)
func TestServeAssets(t *testing.T) {
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
switch live {
case true:
name = fmt.Sprintf("livefs-%s", tc.Path)
name = "livefs"
case false:
name = fmt.Sprintf("embeddedfs-%s", tc.Path)
name = "embeddedfs"
}
t.Run(name, func(t *testing.T) {
r := reporter.NewMock(t)
@@ -40,15 +29,26 @@ func TestServeAssets(t *testing.T) {
t.Fatalf("New() error:\n%+v", err)
}
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/%s", h.Address, tc.Path))
if err != nil {
t.Fatalf("GET /%s:\n%+v", tc.Path, err)
}
defer resp.Body.Close()
if resp.StatusCode != tc.Code {
t.Errorf("GET /%s: got status code %d, not %d", tc.Path, resp.StatusCode, tc.Code)
}
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
{
URL: "/",
ContentType: "text/html; charset=utf-8",
FirstLines: []string{"<!DOCTYPE html>"},
}, {
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
import (
"fmt"
"io/ioutil"
netHTTP "net/http"
"testing"
"akvorado/common/helpers"
@@ -25,21 +22,16 @@ func TestConfigurationEndpoint(t *testing.T) {
"bye": "Goodbye world!",
})
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/orchestrator/broker/configuration/inlet", h.Address))
if err != nil {
t.Fatalf("GET /api/v0/orchestrator/broker/configuration/inlet:\n%+v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("GET /api/v0/orchestrator/broker/configuration/inlet: got status code %d, not 200",
resp.StatusCode)
}
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)
}
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
{
URL: "/api/v0/orchestrator/broker/configuration/inlet",
ContentType: "application/json; charset=utf-8",
FirstLines: []string{
"{",
` "bye": "Goodbye world!",`,
` "hello": "Hello world!"`,
"}",
},
},
})
}