1
0
mirror of https://github.com/Luzifer/badge-gen.git synced 2024-09-18 23:02:57 +00:00
badge-gen/main_test.go

94 lines
2.6 KiB
Go
Raw Permalink Normal View History

2015-05-25 15:40:46 +00:00
package main
import (
"io"
2015-05-25 15:40:46 +00:00
"net/http"
"net/http/httptest"
"os"
2015-05-25 15:40:46 +00:00
"testing"
"github.com/Luzifer/badge-gen/cache"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
2015-05-25 15:40:46 +00:00
)
func testGenerateMux() *mux.Router {
m := mux.NewRouter()
m.HandleFunc("/v1/badge", generateBadge).Methods("GET")
m.HandleFunc("/{service}/{parameters:.*}", generateServiceBadge).Methods("GET")
m.HandleFunc("/", handleDemoPage)
return m
}
2015-05-25 15:40:46 +00:00
func TestMain(m *testing.M) {
cacheStore = cache.NewInMemCache()
os.Exit(m.Run())
}
2015-05-25 15:40:46 +00:00
func TestCreateBadge(t *testing.T) {
badgeData, _ := createBadge("API", "Documentation", "4c1")
badge := string(badgeData)
2015-05-25 15:40:46 +00:00
assert.Contains(t, badge, ">API</text>")
assert.Contains(t, badge, "<path fill=\"#4c1\"")
assert.Contains(t, badge, ">Documentation</text>")
2015-05-25 15:40:46 +00:00
}
func TestHttpResponseMissingParameters(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/v1/badge", nil)
2015-05-25 15:40:46 +00:00
if err != nil {
t.Fatal(err)
}
testGenerateMux().ServeHTTP(resp, req)
if p, err := io.ReadAll(resp.Body); err != nil {
2015-05-25 15:40:46 +00:00
t.Fail()
} else {
assert.Equal(t, http.StatusInternalServerError, resp.Code)
assert.Contains(t, string(p), "You must specify parameters 'title' and 'text'.")
2015-05-25 15:40:46 +00:00
}
}
func TestHttpResponseWithoutColor(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/static/API/Documentation", nil)
2015-05-25 15:40:46 +00:00
if err != nil {
t.Fatal(err)
}
testGenerateMux().ServeHTTP(resp, req)
if p, err := io.ReadAll(resp.Body); err != nil {
2015-05-25 15:40:46 +00:00
t.Fail()
} else {
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "image/svg+xml", resp.Header().Get("Content-Type"))
2015-05-25 15:40:46 +00:00
// Check whether there is a SVG in the response, format checks are in other checks
assert.Contains(t, string(p), "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"133\" height=\"20\">")
assert.Contains(t, string(p), "#4c1", "default color should be set")
2015-05-25 15:40:46 +00:00
}
}
func TestHttpResponseWithColor(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/static/API/Documentation/572", nil) //nolint:noctx // fine for an internal test
2015-05-25 15:40:46 +00:00
if err != nil {
t.Fatal(err)
}
testGenerateMux().ServeHTTP(resp, req)
if p, err := io.ReadAll(resp.Body); err != nil {
2015-05-25 15:40:46 +00:00
t.Fail()
} else {
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "image/svg+xml", resp.Header().Get("Content-Type"))
2015-05-25 15:40:46 +00:00
// Check whether there is a SVG in the response, format checks are in other checks
assert.Contains(t, string(p), "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"133\" height=\"20\">")
assert.NotContains(t, string(p), "#4c1", "default color should not be set")
assert.Contains(t, string(p), "#572", "given color should be set")
2015-05-25 15:40:46 +00:00
}
}