Skip to content

Commit

Permalink
Merge pull request #270 from burnettekm/kristi/fix-fed-wire-bug
Browse files Browse the repository at this point in the history
Fix: Wire file parsing bug
  • Loading branch information
adamdecaf authored Mar 5, 2024
2 parents ff9a532 + 3e4771e commit da227de
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
9 changes: 7 additions & 2 deletions ACHDictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ func (f *ACHDictionary) Read(r io.Reader) error {
return err
}

// Try reading the file as JSON and if that fails read as plaintext
if err := f.readJSON(bytes.NewReader(bs)); err == nil {
// Try validating the file as JSON and if that fails read as plaintext
if json.Valid(bs) {
err = f.readJSON(bytes.NewReader(bs))
if err != nil {
return err
}
return nil
}

return f.readPlaintext(bytes.NewReader(bs))
}

Expand Down
9 changes: 7 additions & 2 deletions WIREDictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,15 @@ func (f *WIREDictionary) Read(r io.Reader) error {
return err
}

// Try reading the file as JSON and if that fails read as plaintext
if err := f.readJSON(bytes.NewReader(bs)); err == nil {
// Try validating the file as JSON and if that fails read as plaintext
if json.Valid(bs) {
err = f.readJSON(bytes.NewReader(bs))
if err != nil {
return err
}
return nil
}

return f.readPlaintext(bytes.NewReader(bs))
}

Expand Down
14 changes: 3 additions & 11 deletions cmd/server/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func fedACHDataFile(logger log.Logger) (io.Reader, error) {
}

func fedWireDataFile(logger log.Logger) (io.Reader, error) {
file, err := attemptFileDownload(logger, "fedach")
file, err := attemptFileDownload(logger, "fedwire")
if err != nil && !errors.Is(err, download.ErrMissingConfigValue) {
return nil, fmt.Errorf("problem downloading fedach: %v", err)
return nil, fmt.Errorf("problem downloading fedwire: %v", err)
}

if file != nil {
Expand All @@ -55,16 +55,8 @@ func fedWireDataFile(logger log.Logger) (io.Reader, error) {
}

func attemptFileDownload(logger log.Logger, listName string) (io.Reader, error) {
routingNumber := os.Getenv("FRB_ROUTING_NUMBER")
downloadCode := os.Getenv("FRB_DOWNLOAD_CODE")
downloadURL := os.Getenv("FRB_DOWNLOAD_URL_TEMPLATE")

logger.Logf("download: attempting %s", listName)
client, err := download.NewClient(&download.ClientOpts{
RoutingNumber: routingNumber,
DownloadCode: downloadCode,
DownloadURL: downloadURL,
})
client, err := download.NewClient(nil)
if err != nil {
return nil, fmt.Errorf("client setup: %w", err)
}
Expand Down
27 changes: 16 additions & 11 deletions pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"net/http"
"net/url"
"os"
"time"
)

Expand Down Expand Up @@ -48,23 +49,27 @@ func NewClient(opts *ClientOpts) (*Client, error) {
}
}

if opts.RoutingNumber == "" {
return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingRoutingNumber)
}
routingNum, rnExists := os.LookupEnv("FRB_ROUTING_NUMBER")
downloadcd, dcdExists := os.LookupEnv("FRB_DOWNLOAD_CODE")
downloadurltemp, urlExists := os.LookupEnv("FRB_DOWNLOAD_URL_TEMPLATE")

if opts.RoutingNumber == "" {
return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingDownloadCD)
}
if !urlExists || downloadurltemp == "" {
if !rnExists || routingNum == "" {
return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingRoutingNumber)
}

if !dcdExists || downloadcd == "" {
return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingDownloadCD)
}

if opts.DownloadURL == "" {
opts.DownloadURL = DefaultFRBDownloadURLTemplate
downloadurltemp = DefaultFRBDownloadURLTemplate
}

return &Client{
httpClient: opts.HTTPClient,
routingNumber: opts.RoutingNumber,
downloadCode: opts.DownloadCode,
downloadURL: opts.DownloadURL,
routingNumber: routingNum,
downloadCode: downloadcd,
downloadURL: downloadurltemp,
}, nil
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ func TestClient__fedwire(t *testing.T) {
}

func TestClient__wire_custom_url(t *testing.T) {
file, err := os.ReadFile(filepath.Join("..", "..", "data", "fedachdir.json"))
file, err := os.ReadFile(filepath.Join("..", "..", "data", "fpddir.json"))
if err != nil {
t.Fatal(err)
}
mockHTTPServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/fedwire" {
writer.WriteHeader(http.StatusNotFound)
}
fmt.Fprint(writer, string(file))
}))
defer mockHTTPServer.Close()
Expand All @@ -110,19 +113,19 @@ func TestClient__wire_custom_url(t *testing.T) {

client := setupClient(t)

fedach, err := client.GetList("fedwire")
fedwire, err := client.GetList("fedwire")
if err != nil {
t.Fatal(err)
}

buf, ok := fedach.(*bytes.Buffer)
buf, ok := fedwire.(*bytes.Buffer)
require.True(t, ok)

if n := buf.Len(); n < 1024 {
t.Errorf("unexpected size of %d bytes", n)
}

bs, _ := io.ReadAll(io.LimitReader(fedach, 10024))
bs, _ := io.ReadAll(io.LimitReader(fedwire, 10024))
if !bytes.Equal(bs, file) {
t.Errorf("unexpected output:\n%s", string(bs))
}
Expand Down

0 comments on commit da227de

Please sign in to comment.