@@ -9,23 +9,24 @@ import (
99 "github.com/stretchr/testify/assert"
1010)
1111
12- func launchDummyServer (lastReq * * http.Request , path , response string ) * httptest.Server {
12+ func launchDummyServer (lastReq * * http.Request , path , response string , status int ) * httptest.Server {
1313 return httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
14- * lastReq = & * r
14+ if lastReq != nil {
15+ * lastReq = & * r
16+ }
1517 if r .URL .Path != path {
1618 fmt .Printf ("path doesn't match: %v != %v" , r .URL .Path , path )
1719 w .WriteHeader (http .StatusNotFound )
1820 } else {
1921 w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
20- w .WriteHeader (http . StatusOK )
22+ w .WriteHeader (status )
2123 w .Write ([]byte (response ))
2224 }
2325 }))
2426}
2527
2628func TestUserMe (t * testing.T ) {
27- var req * http.Request
28- ts := launchDummyServer (& req , makeMethodPath (userObjectPath , userMeMethod ), userMeResponse )
29+ ts := launchDummyServer (nil , makeMethodPath (userObjectPath , userMeMethod ), userMeResponse , http .StatusOK )
2930 defer ts .Close ()
3031
3132 c := NewClient ("realToken" , & ClientOpts {ServerAddress : ts .URL })
@@ -35,8 +36,7 @@ func TestUserMe(t *testing.T) {
3536}
3637
3738func TestUserHasVerifiedEmail (t * testing.T ) {
38- var req * http.Request
39- ts := launchDummyServer (& req , makeMethodPath (userObjectPath , userHasVerifiedEmailMethod ), userHasVerifiedEmailResponse )
39+ ts := launchDummyServer (nil , makeMethodPath (userObjectPath , userHasVerifiedEmailMethod ), userHasVerifiedEmailResponse , http .StatusOK )
4040 defer ts .Close ()
4141
4242 c := NewClient ("realToken" , & ClientOpts {ServerAddress : ts .URL })
@@ -48,7 +48,7 @@ func TestUserHasVerifiedEmail(t *testing.T) {
4848
4949func TestRemoteIP (t * testing.T ) {
5050 var req * http.Request
51- ts := launchDummyServer (& req , makeMethodPath (userObjectPath , userMeMethod ), userMeResponse )
51+ ts := launchDummyServer (& req , makeMethodPath (userObjectPath , userMeMethod ), userMeResponse , http . StatusOK )
5252 defer ts .Close ()
5353
5454 c := NewClient ("realToken" , & ClientOpts {ServerAddress : ts .URL , RemoteIP : "8.8.8.8" })
@@ -74,6 +74,17 @@ func TestHTTPError(t *testing.T) {
7474 assert .EqualError (t , err , `Post "http://lolcathost/user/has_verified_email": dial tcp: lookup lolcathost: no such host` )
7575}
7676
77+ func TestGatewayError (t * testing.T ) {
78+ var req * http.Request
79+ ts := launchDummyServer (& req , makeMethodPath (userObjectPath , userHasVerifiedEmailMethod ), "" , http .StatusBadGateway )
80+ defer ts .Close ()
81+ c := NewClient ("zcasdasc" , & ClientOpts {ServerAddress : ts .URL })
82+
83+ r , err := c .UserHasVerifiedEmail ()
84+ assert .Nil (t , r )
85+ assert .EqualError (t , err , `server returned non-OK status: 502` )
86+ }
87+
7788const userMeResponse = `{
7889 "success": true,
7990 "error": null,
0 commit comments