-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
request_processor_test.go
63 lines (52 loc) · 1.89 KB
/
request_processor_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
package muxie
import (
"encoding/xml"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
type person struct {
XMLName xml.Name `json:"-" xml:"person"`
Name string `json:"name" xml:"name,attr"`
Age int `json:"age" xml:"age,attr"`
Description string `json:"description" xml:"description"`
}
func testProcessor(t *testing.T, p Processor, cType, tmplStrValue string) {
testValue := person{Name: "kataras", Age: 25, Description: "software engineer"}
testValueStr := fmt.Sprintf(tmplStrValue, testValue.Name, testValue.Age, testValue.Description)
mux := NewMux()
mux.HandleFunc("/read", func(w http.ResponseWriter, r *http.Request) {
var v person
if err := Bind(r, p, &v); err != nil {
t.Fatal(err)
}
if expected, got := v.Name, testValue.Name; expected != got {
t.Fatalf("expected name to be: '%s' but got: '%s'", expected, got)
}
if expected, got := v.Age, testValue.Age; expected != got {
t.Fatalf("expected age to be: '%d' but got: '%d'", expected, got)
}
if expected, got := v.Description, testValue.Description; expected != got {
t.Fatalf("expected description to be: '%s' but got: '%s'", expected, got)
}
})
mux.HandleFunc("/write", func(w http.ResponseWriter, r *http.Request) {
if err := Dispatch(w, p, testValue); err != nil {
t.Fatal(err)
}
})
srv := httptest.NewServer(mux)
defer srv.Close()
expectWithBody(t, http.MethodGet, srv.URL+"/read", testValueStr,
http.Header{"Content-Type": []string{cType}}).statusCode(http.StatusOK)
expect(t, http.MethodGet, srv.URL+"/write").statusCode(http.StatusOK).
headerEq("Content-Type", withCharset(cType)).
bodyEq(testValueStr)
}
func TestJSON(t *testing.T) {
testProcessor(t, JSON, "application/json", `{"name":"%s","age":%d,"description":"%s"}`)
}
func TestXML(t *testing.T) {
testProcessor(t, XML, "text/xml", `<person name="%s" age="%d"><description>%s</description></person>`)
}