diff --git a/README.md b/README.md index e47029a..aea56f7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Goat [![GoDoc](https://godoc.org/github.com/bahlo/goat?status.png)](https://godoc.org/github.com/bahlo/goat) [![Build Status](https://secure.travis-ci.org/bahlo/goat.png?branch=master)](https://travis-ci.org/bahlo/goat) [![Coverage Status](https://coveralls.io/repos/bahlo/goat/badge.png?branch=master)](https://coveralls.io/r/bahlo/goat?branch=master) -Goat is a Go REST server. You can pronounce it like the _goat_, or -_go-at_. Depends on how you like goats. +Goat is a minimalistic REST API server in Go. You can pronounce it like the +_goat_, or _go-at_. Depends on how you like goats. ## Contents - [Usage](#usage) diff --git a/index_test.go b/index_test.go index e616e64..9515321 100644 --- a/index_test.go +++ b/index_test.go @@ -1,6 +1,7 @@ package goat import ( + "net/http/httptest" "reflect" "testing" ) @@ -18,6 +19,27 @@ func TestIndex(t *testing.T) { "foo_bar_url": "/foo/bar", } if !reflect.DeepEqual(out, expected) { - t.Errorf("Index should regurn %v, but did return %v", expected, out) + t.Errorf("Index should return %v, but did return %v", expected, out) + } +} + +func TestIndexHandler(t *testing.T) { + r := New() + r.Get("/foo/bar", "foo_bar_url", emptyHandler) + r.Post("/foo", "foo_url", emptyHandler) + r.Get("/bar", "bar_url", emptyHandler) + + w := httptest.NewRecorder() + p := Params{} + r.IndexHandler(w, nil, p) + + expected := `{ + "bar_url": "/bar", + "foo_bar_url": "/foo/bar" +}` + body := string(w.Body.Bytes()) + + if body != expected { + t.Errorf("indexHandler should return %s, but did return %s", expected, body) } } diff --git a/json_test.go b/json_test.go index b7f8451..77d8009 100644 --- a/json_test.go +++ b/json_test.go @@ -1 +1,63 @@ package goat + +import ( + "bytes" + "net/http/httptest" + "testing" +) + +func TestWriteError(t *testing.T) { + // In + code := 500 + err := "foo" + + // Expected + json := `{ + "error": "` + err + `" +} +` + buf := bytes.NewBufferString(json) + + w := httptest.NewRecorder() + WriteError(w, code, err) + + // Test code + if w.Code != code { + t.Errorf("WriteError should set Code to %i, but did set it to %i", code, w.Code) + } + + // Test body + if w.Body == nil { + t.Errorf("WriteError should set Body to %s, but didn't", json) + } else if string(w.Body.Bytes()) == string(buf.Bytes()) { + t.Errorf("WriteError should set Body to %v, but did set it to %v", buf, w.Body) + } +} + +func TestWriteJSON(t *testing.T) { + in := map[string]string{ + "foo": "bar", + "knock": "knock", + } + json := `{ + "foo": "bar", + "knock": "knock" +} +` + buf := bytes.NewBufferString(json) + + w := httptest.NewRecorder() + WriteJSON(w, in) + + if w.Body == nil { + t.Errorf("WriteJSON should set the Body to %s, but didn't", json) + } else if string(w.Body.Bytes()) == string(buf.Bytes()) { + t.Errorf("WriteJSON set the Body to %v, but should set it to %v", buf, w.Body) + } + + // Test error + w = httptest.NewRecorder() + if err := WriteJSON(w, WriteJSON); err == nil { + t.Errorf("WriteJSON should return an error, but didn't") + } +} diff --git a/middleware_test.go b/middleware_test.go index 0008f1f..a69434c 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -8,9 +8,11 @@ import ( func TestUse(t *testing.T) { r := New() mw := func(h http.Handler) http.Handler { return nil } - //exp := []Middleware{mw} - r.Use(mw) - // TODO: Test function equality + if len(r.middleware) == 0 { + t.Errorf("Use should add one item to middleware, but didn't") + } + + // TODO: Check function equality } diff --git a/router_test.go b/router_test.go index df884e9..13a14e7 100644 --- a/router_test.go +++ b/router_test.go @@ -1,6 +1,7 @@ package goat import ( + "net/http/httptest" "reflect" "testing" ) @@ -53,3 +54,23 @@ func TestSubrouter(t *testing.T) { t.Errorf("Subrouter should add %v to children of %v, but didn't", sr, r) } } + +func TestNotFoundHandler(t *testing.T) { + w := httptest.NewRecorder() + r := New() + + r.notFoundHandler(w, nil) + + expCode := 404 + if w.Code != expCode { + t.Errorf("NotFoundHandler should set the status code to %i, but didn't", expCode) + } + + expBody := `{ + "error": "404 Not Found" +}` + body := string(w.Body.Bytes()) + if body != expBody { + t.Errorf("NotFoundHandler set the Body to %s, but should set it to %s", body, expBody) + } +}