@@ -116,7 +116,7 @@ func TestDoBoxMissingError(t *testing.T) {
116116 var actualOut obj
117117 _ , err = c .Do (req , & actualOut )
118118 is .True (err != nil )
119- is .Equal (err .Error (), " testbox: an unknown error occurred in the box" )
119+ is .Equal (err .Error (), ` testbox: 200: {"success":false," error":""}` )
120120}
121121
122122func TestDoHTTPError (t * testing.T ) {
@@ -145,5 +145,72 @@ func TestDoHTTPError(t *testing.T) {
145145 var actualOut obj
146146 _ , err = c .Do (req , & actualOut )
147147 is .True (err != nil )
148- is .Equal (err .Error (), "testbox: 500 Internal Server Error" )
148+ is .Equal (err .Error (), "testbox: 500: something went wrong" )
149+ }
150+
151+ func TestDoNoResponseExpected (t * testing.T ) {
152+ is := is .New (t )
153+ type obj struct {
154+ Field1 string `json:"field1"`
155+ Field2 int `json:"field2"`
156+ Field3 bool `json:"field3"`
157+ }
158+ in := obj {Field1 : "in" , Field2 : 123 , Field3 : true }
159+ out := obj {Field1 : "in" , Field2 : 123 , Field3 : true }
160+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
161+ var requestObj obj
162+ is .Equal (r .Method , http .MethodPost )
163+ is .Equal (r .URL .Path , "/something" )
164+ is .NoErr (json .NewDecoder (r .Body ).Decode (& requestObj ))
165+ is .Equal (requestObj .Field1 , in .Field1 )
166+ is .Equal (requestObj .Field2 , in .Field2 )
167+ is .Equal (requestObj .Field3 , in .Field3 )
168+ is .NoErr (json .NewEncoder (w ).Encode (struct {
169+ Success bool `json:"success"`
170+ obj
171+ }{
172+ Success : true ,
173+ obj : out ,
174+ }))
175+ }))
176+ defer srv .Close ()
177+ var buf bytes.Buffer
178+ is .NoErr (json .NewEncoder (& buf ).Encode (in ))
179+ req , err := http .NewRequest (http .MethodPost , srv .URL + "/something" , & buf )
180+ c := mbhttp .New ("testbox" , http .DefaultClient )
181+ _ , err = c .Do (req , nil )
182+ is .NoErr (err )
183+ }
184+ func TestDoNoResponseExpectedWithError (t * testing.T ) {
185+ is := is .New (t )
186+ type obj struct {
187+ Field1 string `json:"field1"`
188+ Field2 int `json:"field2"`
189+ Field3 bool `json:"field3"`
190+ }
191+ in := obj {Field1 : "in" , Field2 : 123 , Field3 : true }
192+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
193+ var requestObj obj
194+ is .Equal (r .Method , http .MethodPost )
195+ is .Equal (r .URL .Path , "/something" )
196+ is .NoErr (json .NewDecoder (r .Body ).Decode (& requestObj ))
197+ is .Equal (requestObj .Field1 , in .Field1 )
198+ is .Equal (requestObj .Field2 , in .Field2 )
199+ is .Equal (requestObj .Field3 , in .Field3 )
200+ is .NoErr (json .NewEncoder (w ).Encode (struct {
201+ Success bool `json:"success"`
202+ Error string `json:"error"`
203+ }{
204+ Success : false ,
205+ Error : "something went wrong" ,
206+ }))
207+ }))
208+ defer srv .Close ()
209+ var buf bytes.Buffer
210+ is .NoErr (json .NewEncoder (& buf ).Encode (in ))
211+ req , err := http .NewRequest (http .MethodPost , srv .URL + "/something" , & buf )
212+ c := mbhttp .New ("testbox" , http .DefaultClient )
213+ _ , err = c .Do (req , nil )
214+ is .True (err != nil )
215+ is .Equal (err .Error (), "testbox: something went wrong" )
149216}
0 commit comments