-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguestbook_test.go
81 lines (61 loc) · 1.77 KB
/
guestbook_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
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
func TestHitCounter(t *testing.T) {
server := httptest.NewServer(getMux())
defer server.Close()
startingVal := hitCounter
_, err := http.Get(server.URL)
if err != nil {
t.Errorf("Request failed: %s", err)
}
expected := startingVal + 1
if hitCounter != expected {
t.Errorf("Hit counter does match expected value: %d != %d", hitCounter, expected)
}
}
func TestAddSignature(t *testing.T) {
server := httptest.NewServer(getMux())
defer server.Close()
startingSignatures := signatures[:]
_, err := http.PostForm(server.URL, url.Values{
"name": {"John Smith"},
})
if err != nil {
t.Errorf("Request failed: %s", err)
}
if len(startingSignatures) == len(signatures) {
t.Error("Expected a new signature but none found")
}
newSig := signatures[len(signatures)-1]
if newSig.Name != "John Smith" {
t.Errorf("New signature did not match expected name: %s", newSig.Name)
}
if newSig.Timestamp.Format("2016-01-02") != time.Now().Format("2016-01-02") {
t.Errorf("new signature did not match expected timestamp: %s != %s",
newSig.Timestamp.Format("2016-01-02"), time.Now().Format("2016-01-02"))
}
}
func TestAddSignatureTrimsSpaces(t *testing.T) {
server := httptest.NewServer(getMux())
defer server.Close()
startingSignatures := signatures[:]
_, err := http.PostForm(server.URL, url.Values{
"name": {" John Smith "}, // Note the extra whitespace
})
if err != nil {
t.Errorf("Request failed: %s", err)
}
if len(startingSignatures) == len(signatures) {
t.Error("Expected a new signature but none found")
}
newSig := signatures[len(signatures)-1]
if newSig.Name != "John Smith" {
t.Errorf("New signature did not match expected name: %s", newSig.Name)
}
}