mirror of
https://github.com/Luzifer/badge-gen.git
synced 2024-11-08 13:20:02 +00:00
Added tests
This commit is contained in:
parent
350c858160
commit
cd1a7be3e4
3 changed files with 169 additions and 2 deletions
31
font_test.go
Normal file
31
font_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
8
main.go
8
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) {
|
||||
|
|
132
main_test.go
Normal file
132
main_test.go
Normal file
|
@ -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</text>") {
|
||||
t.Error("Did not found node with text 'API'")
|
||||
}
|
||||
|
||||
if !strings.Contains(badge, "<path fill=\"#4c1\"") {
|
||||
t.Error("Did not find color coding for path")
|
||||
}
|
||||
|
||||
if !strings.Contains(badge, ">Documentation</text>") {
|
||||
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), "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"133\" height=\"20\">") {
|
||||
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), "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"133\" height=\"20\">") {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue