Compare commits

...
3 Commits
Author SHA1 Message Date
Andrey Beletsky ef1b43ac62 Do not treat server errors as API originated errors 2021-02-16 19:40:18 +07:00
Andrey Beletsky 39e5821760 Run gofmt on validate.go 2021-02-16 18:38:34 +07:00
Andrey Beletsky cb68cb004e Fix travis go version 2021-02-16 18:34:53 +07:00
5 changed files with 27 additions and 13 deletions

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@ os: linux
dist: xenial
language: go
go:
- 1.11.x
- 1.15.x
env:
global:
+3
View File
@@ -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)
}
+19 -8
View File
@@ -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,
+1 -1
View File
@@ -3,7 +3,7 @@ dist: trusty
language: go
go:
- 1.10.3
- 1.15.x
script:
- ./build_and_test.sh
+3 -3
View File
@@ -24,9 +24,9 @@ const lbrycrdTestnet = "lbrycrd_testnet"
const lbrycrdRegtest = "lbrycrd_regtest"
var addressPrefixes = map[string][2]byte{
lbrycrdMain: [2]byte{lbrycrdMainPubkeyPrefix, lbrycrdMainScriptPrefix},
lbrycrdTestnet: [2]byte{lbrycrdTestnetPubkeyPrefix, lbrycrdTestnetScriptPrefix},
lbrycrdRegtest: [2]byte{lbrycrdRegtestPubkeyPrefix, lbrycrdRegtestScriptPrefix},
lbrycrdMain: {lbrycrdMainPubkeyPrefix, lbrycrdMainScriptPrefix},
lbrycrdTestnet: {lbrycrdTestnetPubkeyPrefix, lbrycrdTestnetScriptPrefix},
lbrycrdRegtest: {lbrycrdRegtestPubkeyPrefix, lbrycrdRegtestScriptPrefix},
}
func PrefixIsValid(address [addressLength]byte, blockchainName string) bool {