Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
Merge branch '1.0.1-development'
Browse files Browse the repository at this point in the history
* 1.0.1-development:
  Update description
  Add test for indexHandler
  Test notFoundHandler
  Pass function to enforce error
  Switch to strings for comparison
  Add more tests, still failing though
  Add better tests for json
  • Loading branch information
bahlo committed Aug 1, 2014
2 parents 2209397 + 23edece commit b61ebcb
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
24 changes: 23 additions & 1 deletion index_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goat

import (
"net/http/httptest"
"reflect"
"testing"
)
Expand All @@ -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)
}
}
62 changes: 62 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
8 changes: 5 additions & 3 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 21 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goat

import (
"net/http/httptest"
"reflect"
"testing"
)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit b61ebcb

Please # to comment.