1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2025-06-30 19:28:39 -04:00

Add DecodeResponse and *Response.Valid() for fine-grained parse control

This commit is contained in:
John Starich
2020-03-31 00:29:11 -05:00
committed by Aaron Lindsay
parent 677a09295a
commit f19189de45
2 changed files with 102 additions and 32 deletions

View File

@ -177,7 +177,7 @@ func TestValidSamples(t *testing.T) {
func TestInvalidResponse(t *testing.T) {
// in this example, the severity is invalid due to mixed upper and lower case letters
resp, err := ofxgo.ParseResponse(bytes.NewReader([]byte(`
const invalidResponse = `
OFXHEADER:100
DATA:OFXSGML
VERSION:102
@ -208,20 +208,58 @@ NEWFILEUID:NONE
</STMTTRNRS>
</BANKMSGSRSV1>
</OFX>
`)))
expectedErr := "Validation failed: Invalid STATUS>SEVERITY; Invalid STATUS>SEVERITY"
if err == nil {
t.Fatalf("ParseResponse should fail with %q, found nil", expectedErr)
}
if _, ok := err.(ofxgo.ErrInvalid); !ok {
t.Errorf("ParseResponse should return an error with type ErrInvalid, found %T", err)
}
if err.Error() != expectedErr {
t.Errorf("ParseResponse should fail with %q, found %v", expectedErr, err)
}
if resp == nil {
t.Errorf("Response must not be nil if only validation errors are present")
}
`
const expectedErr = "Validation failed: Invalid STATUS>SEVERITY; Invalid STATUS>SEVERITY"
t.Run("parse response", func(t *testing.T) {
resp, err := ofxgo.ParseResponse(bytes.NewReader([]byte(invalidResponse)))
expectedErr := "Validation failed: Invalid STATUS>SEVERITY; Invalid STATUS>SEVERITY"
if err == nil {
t.Fatalf("ParseResponse should fail with %q, found nil", expectedErr)
}
if _, ok := err.(ofxgo.ErrInvalid); !ok {
t.Errorf("ParseResponse should return an error with type ErrInvalid, found %T", err)
}
if err.Error() != expectedErr {
t.Errorf("ParseResponse should fail with %q, found %v", expectedErr, err)
}
if resp == nil {
t.Errorf("Response must not be nil if only validation errors are present")
}
})
t.Run("parse failed", func(t *testing.T) {
resp, err := ofxgo.ParseResponse(bytes.NewReader(nil))
if err == nil {
t.Error("ParseResponse should fail to decode")
}
if resp != nil {
t.Errorf("ParseResponse should return a nil response, found: %v", resp)
}
})
t.Run("decode, then validate response", func(t *testing.T) {
resp, err := ofxgo.DecodeResponse(bytes.NewReader([]byte(invalidResponse)))
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}
if resp == nil {
t.Fatal("Response should not be nil from successful decode")
}
valid, err := resp.Valid()
if valid {
t.Error("Response should not be valid")
}
if err == nil {
t.Fatalf("response.Valid() should fail with %q, found nil", expectedErr)
}
if _, ok := err.(ofxgo.ErrInvalid); !ok {
t.Errorf("response.Valid() should return an error of type ErrInvalid, found: %T", err)
}
if err.Error() != expectedErr {
t.Errorf("response.Valid() should return an error with message %q, but found %q", expectedErr, err.Error())
}
})
}
func TestErrInvalidError(t *testing.T) {