Skip to content

Commit

Permalink
Moved tests into the handlers package
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed May 16, 2015
1 parent b5198a5 commit 57f2d4a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
6 changes: 4 additions & 2 deletions chapter9/listing04/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package handlers provides the endpoints for the web service.
package handlers

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

data, err := json.Marshal(&u)
if err != nil {
// We want this error condition to panic so we get a stack trace. This should
// never happen. The http package will catch the panic and provide logging
// We want this error condition to panic so we get a
// stack trace. This should never happen. The http
// package will catch the panic and provide logging
// and return a 500 back to the caller.
log.Panic("Unable to unmarshal response", err)
}
Expand Down
6 changes: 0 additions & 6 deletions chapter9/listing04/handlers/handlers_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ import (
"log"
"net/http"
"net/http/httptest"

"github.com/ardanstudios/gotraining/09-testing/01-testing/example4/handlers"
)

func init() {
handlers.Routes()
}

// ExampleSendJSON provides a basic example test example.
func ExampleSendJSON() {
r, _ := http.NewRequest("GET", "/sendjson", nil)
Expand Down
63 changes: 63 additions & 0 deletions chapter9/listing04/handlers/handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Sample test to show how to test the execution of an
// internal endpoint.
package handlers_test

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/goinaction/code/chapter9/listing04/handlers"
)

const checkMark = "\u2713"
const ballotX = "\u2717"

func init() {
handlers.Routes()
}

// TestSendJSON testing the sendjson internal endpoint.
func TestSendJSON(t *testing.T) {
t.Log("Given the need to test the SendJSON endpoint.")
{
r, err := http.NewRequest("GET", "/sendjson", nil)
if err != nil {
t.Fatal("\tShould be able to create a request.",
ballotX, err)
}
t.Log("\tShould be able to create a request.",
checkMark)

w := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(w, r)

if w.Code != 200 {
t.Fatal("\tShould receive \"200\"", ballotX, w.Code)
}
t.Log("\tShould receive \"200\"", checkMark)

u := struct {
Name string
Email string
}{}

if err := json.NewDecoder(w.Body).Decode(&u); err != nil {
t.Fatal("\tShould decode the response.", ballotX)
}
t.Log("\tShould decode the response.", checkMark)

if u.Name == "Bill" {
t.Log("\tShould have a Name.", checkMark)
} else {
t.Error("\tShould have a Name.", ballotX, u.Name)
}

if u.Email == "[email protected]" {
t.Log("\tShould have an Email.", checkMark)
} else {
t.Error("\tShould have an for Email.", ballotX, u.Email)
}
}
}

0 comments on commit 57f2d4a

Please sign in to comment.