forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
128 lines (104 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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Tests for the sample program to show how to use the echo toolkit.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/ardanlabs/gotraining/topics/web/customer"
)
func TestIndexHandler(t *testing.T) {
// Start a server to handle these requests.
ts := httptest.NewServer(App())
defer ts.Close()
// Perform the GET request for the list of customers.
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.
body := string(b)
for _, n := range []string{"Mary Jane", "Bob Smith"} {
if !strings.Contains(body, n) {
t.Fatalf("Expected %s to contain %s", body, n)
}
}
}
func TestShowHandler(t *testing.T) {
// Start a server to handle these requests.
ts := httptest.NewServer(App())
defer ts.Close()
// Retrieve customer 1, `Mary Jane`.
c1, err := customer.Find(1)
if err != nil {
t.Fatal(err)
}
// Perform a GET request to retrieve customer 1.
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 the correct customer.
body := string(b)
if !strings.Contains(body, c1.Name) {
t.Fatalf("Expected %s to contain %s", body, c1.Name)
}
}
func TestCreateHandler(t *testing.T) {
// Start a server to handle these requests.
ts := httptest.NewServer(App())
defer ts.Close()
customerName := "Jane Doe"
// Perform a POST call to create a new customer.
res, err := http.PostForm(ts.URL+"/customers", url.Values{"name": []string{customerName}})
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 the correct customer back.
body := string(b)
if !strings.Contains(body, customerName) {
t.Fatalf("Expected %s to contain %s", body, customerName)
}
// Retrieve customer 3, `Jane Doe`.
c3, err := customer.Find(3)
if err != nil {
t.Fatal(err)
}
// Perform a GET request to retrieve customer 1.
url := fmt.Sprintf("%s/customers/%d", ts.URL, c3.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 the correct customer.
body = string(b)
if !strings.Contains(body, c3.Name) {
t.Fatalf("Expected %s to contain %s", body, c3.Name)
}
}