Skip to content

Commit 57f2d4a

Browse files
Moved tests into the handlers package
1 parent b5198a5 commit 57f2d4a

3 files changed

Lines changed: 67 additions & 8 deletions

File tree

‎chapter9/listing04/handlers/handlers.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package handlers provides the endpoints for the web service.
12
package handlers
23

34
import (
@@ -25,8 +26,9 @@ func SendJSON(rw http.ResponseWriter, r *http.Request) {
2526

2627
data, err := json.Marshal(&u)
2728
if err != nil {
28-
// We want this error condition to panic so we get a stack trace. This should
29-
// never happen. The http package will catch the panic and provide logging
29+
// We want this error condition to panic so we get a
30+
// stack trace. This should never happen. The http
31+
// package will catch the panic and provide logging
3032
// and return a 500 back to the caller.
3133
log.Panic("Unable to unmarshal response", err)
3234
}

‎chapter9/listing04/handlers/handlers_example_test.go‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@ import (
77
"log"
88
"net/http"
99
"net/http/httptest"
10-
11-
"github.com/ardanstudios/gotraining/09-testing/01-testing/example4/handlers"
1210
)
1311

14-
func init() {
15-
handlers.Routes()
16-
}
17-
1812
// ExampleSendJSON provides a basic example test example.
1913
func ExampleSendJSON() {
2014
r, _ := http.NewRequest("GET", "/sendjson", nil)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Sample test to show how to test the execution of an
2+
// internal endpoint.
3+
package handlers_test
4+
5+
import (
6+
"encoding/json"
7+
"net/http"
8+
"net/http/httptest"
9+
"testing"
10+
11+
"github.com/goinaction/code/chapter9/listing04/handlers"
12+
)
13+
14+
const checkMark = "\u2713"
15+
const ballotX = "\u2717"
16+
17+
func init() {
18+
handlers.Routes()
19+
}
20+
21+
// TestSendJSON testing the sendjson internal endpoint.
22+
func TestSendJSON(t *testing.T) {
23+
t.Log("Given the need to test the SendJSON endpoint.")
24+
{
25+
r, err := http.NewRequest("GET", "/sendjson", nil)
26+
if err != nil {
27+
t.Fatal("\tShould be able to create a request.",
28+
ballotX, err)
29+
}
30+
t.Log("\tShould be able to create a request.",
31+
checkMark)
32+
33+
w := httptest.NewRecorder()
34+
http.DefaultServeMux.ServeHTTP(w, r)
35+
36+
if w.Code != 200 {
37+
t.Fatal("\tShould receive \"200\"", ballotX, w.Code)
38+
}
39+
t.Log("\tShould receive \"200\"", checkMark)
40+
41+
u := struct {
42+
Name string
43+
Email string
44+
}{}
45+
46+
if err := json.NewDecoder(w.Body).Decode(&u); err != nil {
47+
t.Fatal("\tShould decode the response.", ballotX)
48+
}
49+
t.Log("\tShould decode the response.", checkMark)
50+
51+
if u.Name == "Bill" {
52+
t.Log("\tShould have a Name.", checkMark)
53+
} else {
54+
t.Error("\tShould have a Name.", ballotX, u.Name)
55+
}
56+
57+
if u.Email == "bill@ardanstudios.com" {
58+
t.Log("\tShould have an Email.", checkMark)
59+
} else {
60+
t.Error("\tShould have an for Email.", ballotX, u.Email)
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)