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,127 @@ 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+
53+ NotEqual (t , l .BuiltInFormDecoder (), nil )
54+
55+ hf := l .Serve ()
56+
57+ form := url.Values {}
58+ form .Add ("Posted" , "value" )
59+
60+ r , _ := http .NewRequest (POST , "/decode/13" , strings .NewReader (form .Encode ()))
61+ r .Header .Set (ContentType , ApplicationForm )
62+ w := httptest .NewRecorder ()
63+
64+ hf .ServeHTTP (w , r )
65+
66+ Equal (t , w .Code , http .StatusOK )
67+ Equal (t , test .ID , 13 )
68+ Equal (t , test .Posted , "value" )
69+ Equal (t , test .MultiPartPosted , "" )
70+
71+ test = new (TestStruct )
72+ r , _ = http .NewRequest (POST , "/decode2/13" , strings .NewReader (form .Encode ()))
73+ r .Header .Set (ContentType , ApplicationForm )
74+ w = httptest .NewRecorder ()
75+
76+ hf .ServeHTTP (w , r )
77+
78+ Equal (t , w .Code , http .StatusOK )
79+ Equal (t , test .ID , 0 )
80+ Equal (t , test .Posted , "value" )
81+ Equal (t , test .MultiPartPosted , "" )
82+
83+ body := & bytes.Buffer {}
84+ writer := multipart .NewWriter (body )
85+
86+ err := writer .WriteField ("MultiPartPosted" , "value" )
87+ Equal (t , err , nil )
88+
89+ // Don't forget to close the multipart writer.
90+ // If you don't close it, your request will be missing the terminating boundary.
91+ err = writer .Close ()
92+ Equal (t , err , nil )
93+
94+ test = new (TestStruct )
95+ r , _ = http .NewRequest (POST , "/decode/13" , body )
96+ r .Header .Set (ContentType , writer .FormDataContentType ())
97+ w = httptest .NewRecorder ()
98+
99+ hf .ServeHTTP (w , r )
100+ Equal (t , w .Code , http .StatusOK )
101+ Equal (t , test .ID , 13 )
102+ Equal (t , test .Posted , "" )
103+ Equal (t , test .MultiPartPosted , "value" )
104+
105+ body = & bytes.Buffer {}
106+ writer = multipart .NewWriter (body )
107+
108+ err = writer .WriteField ("MultiPartPosted" , "value" )
109+ Equal (t , err , nil )
110+
111+ // Don't forget to close the multipart writer.
112+ // If you don't close it, your request will be missing the terminating boundary.
113+ err = writer .Close ()
114+ Equal (t , err , nil )
115+
116+ test = new (TestStruct )
117+ r , _ = http .NewRequest (POST , "/decode2/13" , body )
118+ r .Header .Set (ContentType , writer .FormDataContentType ())
119+ w = httptest .NewRecorder ()
120+
121+ hf .ServeHTTP (w , r )
122+ Equal (t , w .Code , http .StatusOK )
123+ Equal (t , test .ID , 0 )
124+ Equal (t , test .Posted , "" )
125+ Equal (t , test .MultiPartPosted , "value" )
126+
127+ jsonBody := `{"ID":13,"Posted":"value","MultiPartPosted":"value"}`
128+ test = new (TestStruct )
129+ r , _ = http .NewRequest (POST , "/decode/13" , strings .NewReader (jsonBody ))
130+ r .Header .Set (ContentType , ApplicationJSON )
131+ w = httptest .NewRecorder ()
132+
133+ hf .ServeHTTP (w , r )
134+
135+ Equal (t , w .Code , http .StatusOK )
136+ Equal (t , test .ID , 13 )
137+ Equal (t , test .Posted , "value" )
138+ Equal (t , test .MultiPartPosted , "value" )
139+
140+ xmlBody := `<TestStruct><ID>13</ID><Posted>value</Posted><MultiPartPosted>value</MultiPartPosted></TestStruct>`
141+ test = new (TestStruct )
142+ r , _ = http .NewRequest (POST , "/decode/13" , strings .NewReader (xmlBody ))
143+ r .Header .Set (ContentType , ApplicationXML )
144+ w = httptest .NewRecorder ()
145+
146+ hf .ServeHTTP (w , r )
147+
148+ Equal (t , w .Code , http .StatusOK )
149+ Equal (t , test .ID , 13 )
150+ Equal (t , test .Posted , "value" )
151+ Equal (t , test .MultiPartPosted , "value" )
152+ }
153+
29154func TestStream (t * testing.T ) {
30155 l := New ()
31156
0 commit comments