-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
106 lines (86 loc) · 2.75 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
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// Tests happy path of submitting a well-formed GET /customers request
func TestGetCustomersHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/customers", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(getCustomers)
handler.ServeHTTP(rr, req)
// Checks for 200 status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("getCustomers returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Checks for JSON response
if ctype := rr.Header().Get("Content-Type"); ctype != "application/json" {
t.Errorf("Content-Type does not match: got %v want %v",
ctype, "application/json")
}
}
// Tests happy path of submitting a well-formed POST /customers request
func TestAddCustomerHandler(t *testing.T) {
requestBody := strings.NewReader(`
{
"name": "Example Name",
"role": "Example Role",
"email": "Example Email",
"phone": 5550199,
"contacted": true
}
`)
req, err := http.NewRequest("POST", "/customers", requestBody)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(addCustomer)
handler.ServeHTTP(rr, req)
// Checks for 201 status code
if status := rr.Code; status != http.StatusCreated {
t.Errorf("addCustomer returned wrong status code: got %v want %v",
status, http.StatusCreated)
}
// Checks for JSON response
if ctype := rr.Header().Get("Content-Type"); ctype != "application/json" {
t.Errorf("Content-Type does not match: got %v want %v",
ctype, "application/json")
}
}
// Tests unhappy path of deleting a user that doesn't exist
func TestDeleteCustomerHandler(t *testing.T) {
req, err := http.NewRequest("DELETE", "/customers/e7847fee-3a0e-455e-b151-519bdb9851c7", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(deleteCustomer)
handler.ServeHTTP(rr, req)
// Checks for 404 status code
if status := rr.Code; status != http.StatusNotFound {
t.Errorf("deleteCustomer returned wrong status code: got %v want %v",
status, http.StatusNotFound)
}
}
// Tests unhappy path of getting a user that doesn't exist
func TestGetCustomerHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/customers/e7847fee-3a0e-455e-b151-519bdb9851c7", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(getCustomer)
handler.ServeHTTP(rr, req)
// Checks for 404 status code
if status := rr.Code; status != http.StatusNotFound {
t.Errorf("getCustomer returned wrong status code: got %v want %v",
status, http.StatusNotFound)
}
}