From cd1a7be3e405f077de4b90ba4d05337a679f0606 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 25 May 2015 17:40:46 +0200 Subject: [PATCH] Added tests --- font_test.go | 31 ++++++++++++ main.go | 8 +++- main_test.go | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 font_test.go create mode 100644 main_test.go diff --git a/font_test.go b/font_test.go new file mode 100644 index 0000000..aba90f8 --- /dev/null +++ b/font_test.go @@ -0,0 +1,31 @@ +package main + +import ( + "crypto/sha256" + "fmt" + "testing" +) + +func TestEmbeddedFontHash(t *testing.T) { + // Check the embedded font did not change + font, err := Asset("assets/DejaVuSans.ttf") + if err != nil { + t.Errorf("Could not load embedded font: %s", err) + } + + hash := fmt.Sprintf("%x", sha256.Sum256(font)) + if hash != "3fdf69cabf06049ea70a00b5919340e2ce1e6d02b0cc3c4b44fb6801bd1e0d22" { + t.Errorf("Embedded font changed: %s", hash) + } +} + +func TestStringLength(t *testing.T) { + // As the font is embedded into the source the length calculation should not change + w, err := calculateTextWidth("Test 123 öäüß … !@#%&") + if err != nil { + t.Errorf("Text length errored: %s", err) + } + if w != 138 { + t.Errorf("Text length changed and is now %d", w) + } +} diff --git a/main.go b/main.go index d21765d..8f2f525 100644 --- a/main.go +++ b/main.go @@ -23,11 +23,15 @@ func main() { port = ":3000" } + http.Handle("/", generateMux()) + http.ListenAndServe(port, nil) +} + +func generateMux() *mux.Router { r := mux.NewRouter() r.HandleFunc("/v1/badge", generateBadge).Methods("GET") - http.Handle("/", r) - http.ListenAndServe(port, nil) + return r } func generateBadge(res http.ResponseWriter, r *http.Request) { diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..2c32173 --- /dev/null +++ b/main_test.go @@ -0,0 +1,132 @@ +package main + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" +) + +func TestCreateBadge(t *testing.T) { + badge := string(createBadge("API", "Documentation", "4c1")) + + if !strings.Contains(badge, ">API") { + t.Error("Did not found node with text 'API'") + } + + if !strings.Contains(badge, "Documentation") { + t.Error("Did not found node with text 'Documentation'") + } +} + +func TestHttpResponseMissingParameters(t *testing.T) { + resp := httptest.NewRecorder() + + uri := "/v1/badge" + + req, err := http.NewRequest("GET", uri, nil) + if err != nil { + t.Fatal(err) + } + + generateMux().ServeHTTP(resp, req) + if p, err := ioutil.ReadAll(resp.Body); err != nil { + t.Fail() + } else { + if resp.Code != http.StatusInternalServerError { + t.Errorf("Response code should be %d, is %d", http.StatusInternalServerError, resp.Code) + } + + if string(p) != "You must specify parameters 'title' and 'text'.\n" { + t.Error("Response message did not match test") + } + } + +} + +func TestHttpResponseWithoutColor(t *testing.T) { + resp := httptest.NewRecorder() + + uri := "/v1/badge?" + params := url.Values{ + "title": []string{"API"}, + "text": []string{"Documentation"}, + } + + req, err := http.NewRequest("GET", uri+params.Encode(), nil) + if err != nil { + t.Fatal(err) + } + + generateMux().ServeHTTP(resp, req) + if p, err := ioutil.ReadAll(resp.Body); err != nil { + t.Fail() + } else { + if resp.Code != http.StatusOK { + t.Errorf("Response code should be %d, is %d", http.StatusInternalServerError, resp.Code) + } + + if resp.Header().Get("Content-Type") != "image/svg+xml" { + t.Errorf("Response had wrong Content-Type: %s", resp.Header().Get("Content-Type")) + } + + // Check whether there is a SVG in the response, format checks are in other checks + if !strings.Contains(string(p), "") { + t.Error("Response message did not match test") + } + + if !strings.Contains(string(p), "#4c1") { + t.Error("Default color was not set") + } + } + +} + +func TestHttpResponseWithColor(t *testing.T) { + resp := httptest.NewRecorder() + + uri := "/v1/badge?" + params := url.Values{ + "title": []string{"API"}, + "text": []string{"Documentation"}, + "color": []string{"572"}, + } + + req, err := http.NewRequest("GET", uri+params.Encode(), nil) + if err != nil { + t.Fatal(err) + } + + generateMux().ServeHTTP(resp, req) + if p, err := ioutil.ReadAll(resp.Body); err != nil { + t.Fail() + } else { + if resp.Code != http.StatusOK { + t.Errorf("Response code should be %d, is %d", http.StatusInternalServerError, resp.Code) + } + + if resp.Header().Get("Content-Type") != "image/svg+xml" { + t.Errorf("Response had wrong Content-Type: %s", resp.Header().Get("Content-Type")) + } + + // Check whether there is a SVG in the response, format checks are in other checks + if !strings.Contains(string(p), "") { + t.Error("Response message did not match test") + } + + if strings.Contains(string(p), "#4c1") { + t.Error("Default color is present with color given") + } + + if !strings.Contains(string(p), "#572") { + t.Error("Given color is not present in SVG") + } + } + +}