Skip to content

Commit

Permalink
Do not treat server errors as API originated errors
Browse files Browse the repository at this point in the history
  • Loading branch information
anbsky committed Feb 16, 2021
1 parent 39e5821 commit ef1b43a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions extras/lbryinc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func (c Client) doCall(url string, payload string) ([]byte, error) {
if err != nil {
return body, err
}
if r.StatusCode >= 500 {
return body, fmt.Errorf("server returned non-OK status: %v", r.StatusCode)
}
defer r.Body.Close()
return ioutil.ReadAll(r.Body)
}
Expand Down
27 changes: 19 additions & 8 deletions extras/lbryinc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ import (
"github.com/stretchr/testify/assert"
)

func launchDummyServer(lastReq **http.Request, path, response string) *httptest.Server {
func launchDummyServer(lastReq **http.Request, path, response string, status int) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
*lastReq = &*r
if lastReq != nil {
*lastReq = &*r
}
if r.URL.Path != path {
fmt.Printf("path doesn't match: %v != %v", r.URL.Path, path)
w.WriteHeader(http.StatusNotFound)
} else {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.WriteHeader(status)
w.Write([]byte(response))
}
}))
}

func TestUserMe(t *testing.T) {
var req *http.Request
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
ts := launchDummyServer(nil, makeMethodPath(userObjectPath, userMeMethod), userMeResponse, http.StatusOK)
defer ts.Close()

c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
Expand All @@ -35,8 +36,7 @@ func TestUserMe(t *testing.T) {
}

func TestUserHasVerifiedEmail(t *testing.T) {
var req *http.Request
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse)
ts := launchDummyServer(nil, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse, http.StatusOK)
defer ts.Close()

c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
Expand All @@ -48,7 +48,7 @@ func TestUserHasVerifiedEmail(t *testing.T) {

func TestRemoteIP(t *testing.T) {
var req *http.Request
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse, http.StatusOK)
defer ts.Close()

c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL, RemoteIP: "8.8.8.8"})
Expand All @@ -74,6 +74,17 @@ func TestHTTPError(t *testing.T) {
assert.EqualError(t, err, `Post "http://lolcathost/user/has_verified_email": dial tcp: lookup lolcathost: no such host`)
}

func TestGatewayError(t *testing.T) {
var req *http.Request
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), "", http.StatusBadGateway)
defer ts.Close()
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: ts.URL})

r, err := c.UserHasVerifiedEmail()
assert.Nil(t, r)
assert.EqualError(t, err, `server returned non-OK status: 502`)
}

const userMeResponse = `{
"success": true,
"error": null,
Expand Down

0 comments on commit ef1b43a

Please sign in to comment.