-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
73 lines (64 loc) · 1.7 KB
/
request_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
package common
import (
"bytes"
"encoding/json"
"encoding/xml"
"net/http"
"sync"
"testing"
)
type Person struct {
Name string `json:"name" xml:"name"`
Age int `json:"age" xml:"age"`
}
var p = Person{Name: "Lilei", Age: 15}
func TestParseJSON(t *testing.T) {
data, _ := json.Marshal(p)
buffer := bytes.NewBuffer(data)
req, _ := http.NewRequest("POST", "http://localhost", buffer)
np := &Person{}
if err := ParseJSON(req, np); err != nil {
t.Error("parse json failed:", err)
}
if np.Name != p.Name || np.Age != p.Age {
t.Error("parse json result doesn't match")
}
}
func TestParseXML(t *testing.T) {
data, _ := xml.Marshal(p)
buffer := bytes.NewBuffer(data)
req, _ := http.NewRequest("POST", "http://localhost", buffer)
np := &Person{}
if err := ParseXML(req, np); err != nil {
t.Error("parse xml failed:", err)
}
if np.Name != p.Name || np.Age != p.Age {
t.Error("parse xml result doesn't match")
}
}
func TestPostJSON(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
t.Errorf("wrong method. POST expected, %s got", req.Method)
}
ct := req.Header.Get("Content-Type")
if ct != "application/json; charset=utf-8" {
t.Errorf("wrong header(Content-Type). 'application/json; charset=utf-8' expected, '%s' got", ct)
}
np := &Person{}
if err := ParseJSON(req, np); err != nil {
t.Error("parse json failed:", err)
}
if np.Name != p.Name || np.Age != p.Age {
t.Errorf("wrong json content. %+v expected, %+v got", p, np)
}
wg.Done()
})
go http.ListenAndServe(":12345", nil)
if _, err := PostJSON("http://127.0.0.1:12345", p); err != nil {
t.Error(err)
}
wg.Wait()
}