Skip to content

Commit

Permalink
Added better error messages, more test coverage, new example
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jan 24, 2022
1 parent c4428f6 commit 6437fd5
Show file tree
Hide file tree
Showing 21 changed files with 514 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ linters:
- bodyclose
- revive
- unconvert
- dupl
- misspell
- dogsled
- prealloc
Expand All @@ -350,6 +349,7 @@ linters:
- gochecknoglobals
- whitespace
- wsl
- dupl
- goerr113
- godot
- testpackage
Expand Down
24 changes: 18 additions & 6 deletions addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func (c *Client) AddressInfo(ctx context.Context, address string) (addressInfo *
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrAddressNotFound
}
err = json.Unmarshal([]byte(resp), &addressInfo)
return
}
Expand All @@ -40,7 +42,9 @@ func (c *Client) AddressBalance(ctx context.Context, address string) (balance *A
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrAddressNotFound
}
err = json.Unmarshal([]byte(resp), &balance)
return
}
Expand All @@ -59,7 +63,9 @@ func (c *Client) AddressHistory(ctx context.Context, address string) (history Ad
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrAddressNotFound
}
err = json.Unmarshal([]byte(resp), &history)
return
}
Expand All @@ -78,7 +84,9 @@ func (c *Client) AddressUnspentTransactions(ctx context.Context, address string)
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrAddressNotFound
}
err = json.Unmarshal([]byte(resp), &history)
return
}
Expand Down Expand Up @@ -205,7 +213,9 @@ func (c *Client) BulkBalance(ctx context.Context, list *AddressList) (balances A
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrAddressNotFound
}
err = json.Unmarshal([]byte(resp), &balances)
return
}
Expand All @@ -231,7 +241,9 @@ func (c *Client) BulkUnspentTransactions(ctx context.Context, list *AddressList)
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrAddressNotFound
}
err = json.Unmarshal([]byte(resp), &response)
return
}
83 changes: 82 additions & 1 deletion addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func (m *mockHTTPAddresses) Do(req *http.Request) (*http.Response, error) {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"isvalid": false,"address": "","scriptPubKey": "","ismine": false,"iswatchonly": false,"isscript": false}`)))
}

// Not found
if strings.Contains(req.URL.String(), "/notFound/info") {
resp.StatusCode = http.StatusNotFound
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}

//
// Address Balance
//
Expand All @@ -65,6 +72,13 @@ func (m *mockHTTPAddresses) Do(req *http.Request) (*http.Response, error) {
return resp, fmt.Errorf("bad request")
}

// Not found
if strings.Contains(req.URL.String(), "/16ZqP5notFound/balance") {
resp.StatusCode = http.StatusNotFound
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}

//
// Address History
//
Expand All @@ -87,6 +101,13 @@ func (m *mockHTTPAddresses) Do(req *http.Request) (*http.Response, error) {
return resp, fmt.Errorf("bad request")
}

// Not found
if strings.Contains(req.URL.String(), "/16ZqP5notFound/history") {
resp.StatusCode = http.StatusNotFound
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}

//
// Address unspent
//
Expand All @@ -109,6 +130,13 @@ func (m *mockHTTPAddresses) Do(req *http.Request) (*http.Response, error) {
return resp, fmt.Errorf("bad request")
}

// Not found
if strings.Contains(req.URL.String(), "/16ZqP5notFound/unspent") {
resp.StatusCode = http.StatusNotFound
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}

//
// Address bulk balance
//
Expand Down Expand Up @@ -224,6 +252,34 @@ func (m *mockHTTPAddressesErrors) Do(req *http.Request) (*http.Response, error)
return nil, fmt.Errorf("no valid response found")
}

// mockHTTPAddressesNotFound for mocking requests
type mockHTTPAddressesNotFound struct{}

// Do is a mock http request
func (m *mockHTTPAddressesNotFound) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusNotFound

// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}

// Not found
if strings.Contains(req.URL.String(), "/addresses/balance") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}

// Not found
if strings.Contains(req.URL.String(), "/addresses/unspent") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}

return resp, nil
}

// TestClient_AddressInfo tests the AddressInfo()
func TestClient_AddressInfo(t *testing.T) {
t.Parallel()
Expand All @@ -242,6 +298,7 @@ func TestClient_AddressInfo(t *testing.T) {
{"16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA", "16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA", false, http.StatusOK},
{"16ZqP5invalid", "", false, http.StatusOK},
{"error", "", true, http.StatusInternalServerError},
{"notFound", "", true, http.StatusNotFound},
}

// Test all
Expand Down Expand Up @@ -276,6 +333,7 @@ func TestClient_AddressBalance(t *testing.T) {
}{
{"16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA", 10102050381, 123, false, http.StatusOK},
{"16ZqP5invalid", 0, 0, true, http.StatusBadRequest},
{"16ZqP5notFound", 0, 0, true, http.StatusNotFound},
}

// Test all
Expand Down Expand Up @@ -313,6 +371,7 @@ func TestClient_AddressHistory(t *testing.T) {
{"16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA", "6b22c47e7956e5404e05c3dc87dc9f46e929acfd46c8dd7813a34e1218d2f9d1", 563052, false, http.StatusOK},
{"1NfHy82RqJVGEau9u5DwFRyGc6QKwDuQeT", "", 0, false, http.StatusOK},
{"16ZqP5invalid", "", 0, true, http.StatusBadRequest},
{"16ZqP5notFound", "", 0, true, http.StatusNotFound},
}

// Test all
Expand Down Expand Up @@ -351,6 +410,7 @@ func TestClient_AddressUnspentTransactions(t *testing.T) {
{"16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA", "33b9432a0ea203bbb6ec00592622cf6e90223849e4c9a76447a19a3ed43907d3", 639302, 2451680, false, http.StatusOK},
{"1NfHy82RqJVGEau9u5DwFRyGc6QKwDuQeT", "", 0, 0, false, http.StatusOK},
{"16ZqP5invalid", "", 0, 0, true, http.StatusBadRequest},
{"16ZqP5notFound", "", 0, 0, true, http.StatusNotFound},
}

// Test all
Expand Down Expand Up @@ -384,12 +444,14 @@ func TestClient_AddressUnspentTransactionDetails(t *testing.T) {
// Create the list of tests
var tests = []struct {
input string
txHash string
txHash string ``
height int64
expectedError bool
statusCode int
}{
{"16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA", "33b9432a0ea203bbb6ec00592622cf6e90223849e4c9a76447a19a3ed43907d3", 639302, false, http.StatusOK},
{"16ZqP5notFound", "", 0, true, http.StatusNotFound},
{"16ZqP5invalid", "", 0, true, http.StatusBadRequest},
}

// Test all
Expand Down Expand Up @@ -493,6 +555,16 @@ func TestClient_BulkBalance(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, balances)
})

t.Run("not found", func(t *testing.T) {
client := newMockClient(&mockHTTPAddressesNotFound{})
ctx := context.Background()
balances, err := client.BulkBalance(ctx, &AddressList{Addresses: []string{
"16ZBEb7pp6mx5EAGrdeKivztd5eRJFuvYP",
}})
assert.Error(t, err)
assert.Nil(t, balances)
})
}

// TestClient_BulkUnspentTransactions tests the BulkUnspentTransactions()
Expand Down Expand Up @@ -548,4 +620,13 @@ func TestClient_BulkUnspentTransactions(t *testing.T) {
assert.Nil(t, balances)
})

t.Run("not found", func(t *testing.T) {
client := newMockClient(&mockHTTPAddressesNotFound{})
ctx := context.Background()
balances, err := client.BulkUnspentTransactions(ctx, &AddressList{Addresses: []string{
"16ZBEb7pp6mx5EAGrdeKivztd5eRJFuvYP",
}})
assert.Error(t, err)
assert.Nil(t, balances)
})
}
23 changes: 15 additions & 8 deletions blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func (c *Client) GetBlockByHash(ctx context.Context, hash string) (blockInfo *Bl
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrBlockNotFound
}
err = json.Unmarshal([]byte(resp), &blockInfo)
return
}
Expand All @@ -40,11 +42,10 @@ func (c *Client) GetBlockByHeight(ctx context.Context, height int64) (blockInfo
); err != nil {
return
}

// Added this condition (for bad block heights, 200 success but empty result - no JSON response)
if len(resp) > 0 {
err = json.Unmarshal([]byte(resp), &blockInfo)
if len(resp) == 0 {
return nil, ErrBlockNotFound
}
err = json.Unmarshal([]byte(resp), &blockInfo)
return
}

Expand All @@ -63,7 +64,9 @@ func (c *Client) GetBlockPages(ctx context.Context, hash string, page int) (txLi
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrBlockNotFound
}
err = json.Unmarshal([]byte(resp), &txList)
return
}
Expand All @@ -82,7 +85,9 @@ func (c *Client) GetHeaderByHash(ctx context.Context, hash string) (headerInfo *
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrBlockNotFound
}
err = json.Unmarshal([]byte(resp), &headerInfo)
return
}
Expand All @@ -101,7 +106,9 @@ func (c *Client) GetHeaders(ctx context.Context) (blockHeaders []*BlockInfo, err
); err != nil {
return
}

if len(resp) == 0 {
return nil, ErrHeadersNotFound
}
err = json.Unmarshal([]byte(resp), &blockHeaders)
return
}
Loading

0 comments on commit 6437fd5

Please sign in to comment.