forked from xinyunh0929/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_test.go
143 lines (116 loc) · 3.16 KB
/
app_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
135
136
137
138
139
140
141
142
143
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"mime/multipart"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/GoogleCloudPlatform/golang-samples/getting-started/bookshelf"
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
"github.com/GoogleCloudPlatform/golang-samples/internal/webtest"
)
var wt *webtest.W
func TestMain(m *testing.M) {
serv := httptest.NewServer(nil)
wt = webtest.New(nil, serv.Listener.Addr().String())
registerHandlers()
os.Exit(m.Run())
}
func TestMainFunc(t *testing.T) {
wt := webtest.New(t, "localhost:8080")
m := testutil.BuildMain(t)
defer m.Cleanup()
m.Run(nil, func() {
wt.WaitForNet()
bodyContains(t, wt, "/", "No books found")
})
}
func TestNoBooks(t *testing.T) {
bodyContains(t, wt, "/", "No books found")
}
func TestBookDetail(t *testing.T) {
const title = "book mcbook"
id, err := bookshelf.DB.AddBook(&bookshelf.Book{
Title: title,
})
if err != nil {
t.Fatal(err)
}
bodyContains(t, wt, "/", title)
bookPath := fmt.Sprintf("/books/%d", id)
bodyContains(t, wt, bookPath, title)
if err := bookshelf.DB.DeleteBook(id); err != nil {
t.Fatal(err)
}
bodyContains(t, wt, "/", "No books found")
}
func TestEditBook(t *testing.T) {
const title = "book mcbook"
id, err := bookshelf.DB.AddBook(&bookshelf.Book{
Title: title,
})
if err != nil {
t.Fatal(err)
}
bookPath := fmt.Sprintf("/books/%d", id)
editPath := bookPath + "/edit"
bodyContains(t, wt, editPath, "Edit book")
bodyContains(t, wt, editPath, title)
var body bytes.Buffer
m := multipart.NewWriter(&body)
m.WriteField("title", "simpsons")
m.WriteField("author", "homer")
m.Close()
resp, err := wt.Post(bookPath, "multipart/form-data; boundary="+m.Boundary(), &body)
if err != nil {
t.Fatal(err)
}
if got, want := resp.Request.URL.Path, bookPath; got != want {
t.Errorf("got %s, want %s", got, want)
}
bodyContains(t, wt, bookPath, "simpsons")
bodyContains(t, wt, bookPath, "homer")
if err := bookshelf.DB.DeleteBook(id); err != nil {
t.Fatalf("got err %v, want nil", err)
}
}
func TestAddAndDelete(t *testing.T) {
bodyContains(t, wt, "/books/add", "Add book")
bookPath := fmt.Sprintf("/books")
var body bytes.Buffer
m := multipart.NewWriter(&body)
m.WriteField("title", "simpsons")
m.WriteField("author", "homer")
m.Close()
resp, err := wt.Post(bookPath, "multipart/form-data; boundary="+m.Boundary(), &body)
if err != nil {
t.Fatal(err)
}
gotPath := resp.Request.URL.Path
if wantPrefix := "/books"; !strings.HasPrefix(gotPath, wantPrefix) {
t.Fatalf("redirect: got %q, want prefix %q", gotPath, wantPrefix)
}
bodyContains(t, wt, gotPath, "simpsons")
bodyContains(t, wt, gotPath, "homer")
_, err = wt.Post(gotPath+":delete", "", nil)
if err != nil {
t.Fatal(err)
}
}
func bodyContains(t *testing.T, wt *webtest.W, path, contains string) (ok bool) {
body, _, err := wt.GetBody(path)
if err != nil {
t.Error(err)
return false
}
if !strings.Contains(body, contains) {
t.Errorf("want %s to contain %s", body, contains)
return false
}
return true
}