-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
binance: parse error responses (#3090)
* parse binance error responses * fix pointer
- Loading branch information
Showing
4 changed files
with
79 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//go:build live | ||
|
||
package dexnet | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
uri := "https://dcrdata.decred.org/api/block/best" | ||
var resp struct { | ||
Height int64 `json:"height"` | ||
} | ||
var code int | ||
if err := Get(ctx, uri, &resp, WithStatusFunc(func(c int) { code = c })); err != nil { | ||
t.Fatalf("Get error: %v", err) | ||
} | ||
if resp.Height == 0 { | ||
t.Fatal("Height not parsed") | ||
} | ||
if code != http.StatusOK { | ||
t.Fatalf("expected code 200, got %d", code) | ||
} | ||
// Check size limit | ||
if err := Get(ctx, uri, &resp, WithSizeLimit(1)); err == nil { | ||
t.Fatal("Didn't get parse error for low size limit") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
//go:build live | ||
|
||
package dexnet | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
func TestErrorParsing(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
uri := "https://dcrdata.decred.org/api/block/best" | ||
var resp struct { | ||
Height int64 `json:"height"` | ||
} | ||
var code int | ||
if err := Get(ctx, uri, &resp, WithStatusFunc(func(c int) { code = c })); err != nil { | ||
t.Fatalf("Get error: %v", err) | ||
} | ||
if resp.Height == 0 { | ||
t.Fatal("Height not parsed") | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
http.Error(w, `{"code": -150, "msg": "you messed up, bruh"}`, http.StatusBadRequest) | ||
})) | ||
defer ts.Close() | ||
|
||
var errPayload struct { | ||
Code int `json:"code"` | ||
Msg string `json:"msg"` | ||
} | ||
if code != http.StatusOK { | ||
t.Fatalf("expected code 200, got %d", code) | ||
if err := Get(ctx, ts.URL, nil, WithErrorParsing(&errPayload)); err == nil { | ||
t.Fatal("didn't get an http error") | ||
} | ||
// Check size limit | ||
if err := Get(ctx, uri, &resp, WithSizeLimit(1)); err == nil { | ||
t.Fatal("Didn't get parse error for low size limit") | ||
if errPayload.Code != -150 || errPayload.Msg != "you messed up, bruh" { | ||
t.Fatal("unexpected error body") | ||
} | ||
|
||
} |