forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
134 lines (111 loc) · 2.78 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Tests for the sample program to show how to create a basic CRUD based
// web api for customers.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/ardanlabs/gotraining/topics/web/customer"
)
func TestRoot(t *testing.T) {
// Startup a server to handle processing these routes.
ts := httptest.NewServer(App())
defer ts.Close()
// Request the root URL `/`.
res, err := http.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
// Validate we were redirected to the customers url.
got := res.Request.URL.String()
want := ts.URL + "/customers"
if got != want {
t.Log("Wanted:", got)
t.Log("Got :", want)
t.Fatal("Mismatch")
}
}
func TestIndexHandler(t *testing.T) {
// Startup a server to handle processing these routes.
ts := httptest.NewServer(App())
defer ts.Close()
// Request all the customers in the DB.
res, err := http.Get(ts.URL + "/customers")
if err != nil {
t.Fatal(err)
}
// Read in the response from the api call.
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
// Validate we received all the known customers.
got := string(b)
want := `[{"ID":1,"Name":"Mary Jane"},{"ID":2,"Name":"Bob Smith"}]`
if got[:len(got)-1] != want {
t.Log("Wanted:", want)
t.Log("Got :", got)
t.Fatal("Mismatch")
}
}
func TestShowHandler(t *testing.T) {
// Startup a server to handle processing these routes.
ts := httptest.NewServer(App())
defer ts.Close()
// Find customer 1 in the DB.
c1, err := customer.Find(1)
if err != nil {
t.Fatal(err)
}
// Request customer 1 from the DB.
url := fmt.Sprintf("%s/customers/%d", ts.URL, c1.ID)
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
// Read in the response from the api call.
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
// Validate we received all the known customers.
got := string(b)
want := `{"ID":1,"Name":"Mary Jane"}`
if got[:len(got)-1] != want {
t.Log("Wanted:", want)
t.Log("Got :", got)
t.Fatal("Mismatch")
}
}
func TestCreateHandler(t *testing.T) {
// Startup a server to handle processing these routes.
ts := httptest.NewServer(App())
defer ts.Close()
// Create a JSON document from this Customer value.
b, err := json.Marshal(customer.Customer{Name: "Jane Doe"})
if err != nil {
t.Fatal(err)
}
// Save this customer into the database.
res, err := http.Post(ts.URL+"/customers", "application/json", bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}
// Read in the response from the api call.
b, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
// Validate we received all the known customers.
got := string(b)
want := `{"ID":3,"Name":"Jane Doe"}`
if got != want {
t.Log("Wanted:", want)
t.Log("Got :", got)
t.Fatal("Mismatch")
}
}