This repository has been archived by the owner on Mar 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
5 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters