From ef1b43ac62eded0e140f1946dc43cf7b7263f6b9 Mon Sep 17 00:00:00 2001 From: Andrey Beletsky Date: Tue, 16 Feb 2021 19:40:18 +0700 Subject: [PATCH] Do not treat server errors as API originated errors --- extras/lbryinc/client.go | 3 +++ extras/lbryinc/client_test.go | 27 +++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/extras/lbryinc/client.go b/extras/lbryinc/client.go index a9345c1..e9f12d2 100644 --- a/extras/lbryinc/client.go +++ b/extras/lbryinc/client.go @@ -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) } diff --git a/extras/lbryinc/client_test.go b/extras/lbryinc/client_test.go index 6fbd04f..3b2714e 100644 --- a/extras/lbryinc/client_test.go +++ b/extras/lbryinc/client_test.go @@ -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}) @@ -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}) @@ -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"}) @@ -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,