11package lars
22
33import (
4+ "bytes"
45 "encoding/xml"
56 "io"
7+ "mime/multipart"
68 "net/http"
79 "net/http/httptest"
10+ "net/url"
811 "os"
912 "reflect"
13+ "strings"
1014 "testing"
1115 "time"
1216
@@ -26,6 +30,124 @@ import (
2630// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
2731//
2832
33+ func TestDecode (t * testing.T ) {
34+
35+ type TestStruct struct {
36+ ID int `form:"id"`
37+ Posted string
38+ MultiPartPosted string
39+ }
40+
41+ test := new (TestStruct )
42+
43+ l := New ()
44+ l .Post ("/decode/:id" , func (c Context ) {
45+ err := c .Decode (true , 16 << 10 , test )
46+ Equal (t , err , nil )
47+ })
48+ l .Post ("/decode2/:id" , func (c Context ) {
49+ err := c .Decode (false , 16 << 10 , test )
50+ Equal (t , err , nil )
51+ })
52+ hf := l .Serve ()
53+
54+ form := url.Values {}
55+ form .Add ("Posted" , "value" )
56+
57+ r , _ := http .NewRequest (POST , "/decode/13" , strings .NewReader (form .Encode ()))
58+ r .Header .Set (ContentType , ApplicationForm )
59+ w := httptest .NewRecorder ()
60+
61+ hf .ServeHTTP (w , r )
62+
63+ Equal (t , w .Code , http .StatusOK )
64+ Equal (t , test .ID , 13 )
65+ Equal (t , test .Posted , "value" )
66+ Equal (t , test .MultiPartPosted , "" )
67+
68+ test = new (TestStruct )
69+ r , _ = http .NewRequest (POST , "/decode2/13" , strings .NewReader (form .Encode ()))
70+ r .Header .Set (ContentType , ApplicationForm )
71+ w = httptest .NewRecorder ()
72+
73+ hf .ServeHTTP (w , r )
74+
75+ Equal (t , w .Code , http .StatusOK )
76+ Equal (t , test .ID , 0 )
77+ Equal (t , test .Posted , "value" )
78+ Equal (t , test .MultiPartPosted , "" )
79+
80+ body := & bytes.Buffer {}
81+ writer := multipart .NewWriter (body )
82+
83+ err := writer .WriteField ("MultiPartPosted" , "value" )
84+ Equal (t , err , nil )
85+
86+ // Don't forget to close the multipart writer.
87+ // If you don't close it, your request will be missing the terminating boundary.
88+ err = writer .Close ()
89+ Equal (t , err , nil )
90+
91+ test = new (TestStruct )
92+ r , _ = http .NewRequest (POST , "/decode/13" , body )
93+ r .Header .Set (ContentType , writer .FormDataContentType ())
94+ w = httptest .NewRecorder ()
95+
96+ hf .ServeHTTP (w , r )
97+ Equal (t , w .Code , http .StatusOK )
98+ Equal (t , test .ID , 13 )
99+ Equal (t , test .Posted , "" )
100+ Equal (t , test .MultiPartPosted , "value" )
101+
102+ body = & bytes.Buffer {}
103+ writer = multipart .NewWriter (body )
104+
105+ err = writer .WriteField ("MultiPartPosted" , "value" )
106+ Equal (t , err , nil )
107+
108+ // Don't forget to close the multipart writer.
109+ // If you don't close it, your request will be missing the terminating boundary.
110+ err = writer .Close ()
111+ Equal (t , err , nil )
112+
113+ test = new (TestStruct )
114+ r , _ = http .NewRequest (POST , "/decode2/13" , body )
115+ r .Header .Set (ContentType , writer .FormDataContentType ())
116+ w = httptest .NewRecorder ()
117+
118+ hf .ServeHTTP (w , r )
119+ Equal (t , w .Code , http .StatusOK )
120+ Equal (t , test .ID , 0 )
121+ Equal (t , test .Posted , "" )
122+ Equal (t , test .MultiPartPosted , "value" )
123+
124+ jsonBody := `{"ID":13,"Posted":"value","MultiPartPosted":"value"}`
125+ test = new (TestStruct )
126+ r , _ = http .NewRequest (POST , "/decode/13" , strings .NewReader (jsonBody ))
127+ r .Header .Set (ContentType , ApplicationJSON )
128+ w = httptest .NewRecorder ()
129+
130+ hf .ServeHTTP (w , r )
131+
132+ Equal (t , w .Code , http .StatusOK )
133+ Equal (t , test .ID , 13 )
134+ Equal (t , test .Posted , "value" )
135+ Equal (t , test .MultiPartPosted , "value" )
136+
137+ xmlBody := `<TestStruct><ID>13</ID><Posted>value</Posted><MultiPartPosted>value</MultiPartPosted></TestStruct>`
138+ test = new (TestStruct )
139+ r , _ = http .NewRequest (POST , "/decode/13" , strings .NewReader (xmlBody ))
140+ r .Header .Set (ContentType , ApplicationXML )
141+ w = httptest .NewRecorder ()
142+
143+ hf .ServeHTTP (w , r )
144+
145+ Equal (t , w .Code , http .StatusOK )
146+ Equal (t , test .ID , 13 )
147+ Equal (t , test .Posted , "value" )
148+ Equal (t , test .MultiPartPosted , "value" )
149+ }
150+
29151func TestStream (t * testing.T ) {
30152 l := New ()
31153
0 commit comments