client: Add additional test case for errors.

Tests now check that the correct vspd error code is returned by the
client.
This commit is contained in:
jholdstock 2023-03-01 09:23:33 +00:00 committed by Jamie Holdstock
parent be79a8e279
commit 79fd2f7bbe

View File

@ -16,34 +16,43 @@ import (
func TestErrorDetails(t *testing.T) { func TestErrorDetails(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
httpStatus int respHTTPStatus int
responseBodyBytes []byte respBodyBytes []byte
expectedErr string expectedErr string
vspdError bool vspdError bool
vspdErrCode int64
}{ }{
"500, vspd error": { "500, vspd error (generic bad request)": {
httpStatus: 500, respHTTPStatus: 500,
responseBodyBytes: []byte(`{"code": 1, "message": "bad request"}`), respBodyBytes: []byte(`{"code": 1, "message": "bad request"}`),
expectedErr: `bad request`, expectedErr: `bad request`,
vspdError: true, vspdError: true,
vspdErrCode: 1,
},
"428, vspd error (cannot broadcast fee)": {
respHTTPStatus: 428,
respBodyBytes: []byte(`{"code": 16, "message": "fee transaction could not be broadcast due to unknown outputs"}`),
expectedErr: `fee transaction could not be broadcast due to unknown outputs`,
vspdError: true,
vspdErrCode: 16,
}, },
"500, no body": { "500, no body": {
httpStatus: 500, respHTTPStatus: 500,
responseBodyBytes: nil, respBodyBytes: nil,
expectedErr: `http status 500 (Internal Server Error) with no body`, expectedErr: `http status 500 (Internal Server Error) with no body`,
vspdError: false, vspdError: false,
}, },
"500, non vspd error": { "500, non vspd error": {
httpStatus: 500, respHTTPStatus: 500,
responseBodyBytes: []byte(`an error occurred`), respBodyBytes: []byte(`an error occurred`),
expectedErr: `http status 500 (Internal Server Error) with body "an error occurred"`, expectedErr: `http status 500 (Internal Server Error) with body "an error occurred"`,
vspdError: false, vspdError: false,
}, },
"500, non vspd error (json)": { "500, non vspd error (json)": {
httpStatus: 500, respHTTPStatus: 500,
responseBodyBytes: []byte(`{"some": "json"}`), respBodyBytes: []byte(`{"some": "json"}`),
expectedErr: `http status 500 (Internal Server Error) with body "{\"some\": \"json\"}"`, expectedErr: `http status 500 (Internal Server Error) with body "{\"some\": \"json\"}"`,
vspdError: false, vspdError: false,
}, },
} }
@ -51,8 +60,8 @@ func TestErrorDetails(t *testing.T) {
t.Run(testName, func(t *testing.T) { t.Run(testName, func(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(testData.httpStatus) res.WriteHeader(testData.respHTTPStatus)
_, err := res.Write(testData.responseBodyBytes) _, err := res.Write(testData.respBodyBytes)
if err != nil { if err != nil {
t.Fatalf("writing response body failed: %v", err) t.Fatalf("writing response body failed: %v", err)
} }
@ -84,6 +93,11 @@ func TestErrorDetails(t *testing.T) {
if !errors.As(err, &e) { if !errors.As(err, &e) {
t.Fatal("unable to unwrap vspd error") t.Fatal("unable to unwrap vspd error")
} }
if e.Code != testData.vspdErrCode {
t.Fatalf("incorrect vspd error code, expected %d, got %d",
testData.vspdErrCode, e.Code)
}
} }
}) })