/
web_test.go
49 lines (37 loc) · 1.14 KB
/
web_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
package main
import "io/ioutil"
import "net/http/httptest"
import "net/http"
import "testing"
import "contact"
func TestGetContact(t *testing.T) {
contact.Add("kenshero",contact.Contact{
Tel :"123456",
Email :"ken@mail.com",
})
ts := httptest.NewServer(http.HandlerFunc(getContact))
defer ts.Close()
resp, _ := http.Get(ts.URL + "/contacts/get?name=kenshero")
b, _ := ioutil.ReadAll(resp.Body)
content := string(b)
expectContent := `{"tel":"123456","mail":"ken@mail.com"}`+"\n"
if content != expectContent{
t.Error("Expect %s but got %s",expectContent,content)
}
}
func TestPostContact(t *testing.T) {
contact.Add("kenshero",contact.Contact{
Tel :"123456",
Email :"ken@mail.com",
})
ts := httptest.NewServer(http.HandlerFunc(getContact))
defer ts.Close()
resp, _ := http.POST(ts.URL + "/contacts/get?name=kenshero","application/json",nil)
if resp.StatusCode != http.StatusMethodNotAllowed{
t.Error("Expect 405 but got %d",resp.StatusCode)
}
expectContent := `{"tel":"123456","mail":"ken@mail.com"}`+"\n"
if content != expectContent{
t.Error("Expect %s but got %s",expectContent,content)
}
}