From 4818a544f49312245b68c7eae21d16f1b1f543d2 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 18 Sep 2020 18:10:56 -0700 Subject: [PATCH] Add test for bitcoin mempool implementation --- bitcoin/client_fixtures/raw_mempool.json | 9 ++++ bitcoin/client_test.go | 66 ++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 bitcoin/client_fixtures/raw_mempool.json diff --git a/bitcoin/client_fixtures/raw_mempool.json b/bitcoin/client_fixtures/raw_mempool.json new file mode 100644 index 0000000..115fa59 --- /dev/null +++ b/bitcoin/client_fixtures/raw_mempool.json @@ -0,0 +1,9 @@ +{ + "result": [ + "9cec12d170e97e21a876fa2789e6bfc25aa22b8a5e05f3f276650844da0c33ab", + "37b4fcc8e0b229412faeab8baad45d3eb8e4eec41840d6ac2103987163459e75", + "7bbb29ae32117597fcdf21b464441abd571dad52d053b9c2f7204f8ea8c4762e" + ], + "error": null, + "id": "curltest" +} diff --git a/bitcoin/client_test.go b/bitcoin/client_test.go index b2f8278..554e3dc 100644 --- a/bitcoin/client_test.go +++ b/bitcoin/client_test.go @@ -1243,6 +1243,72 @@ func TestSuggestedFeeRate(t *testing.T) { } } +func TestRawMempool(t *testing.T) { + tests := map[string]struct { + responses []responseFixture + + expectedTransactions []string + expectedError error + }{ + "successful": { + responses: []responseFixture{ + { + status: http.StatusOK, + body: loadFixture("raw_mempool.json"), + url: url, + }, + }, + expectedTransactions: []string{ + "9cec12d170e97e21a876fa2789e6bfc25aa22b8a5e05f3f276650844da0c33ab", + "37b4fcc8e0b229412faeab8baad45d3eb8e4eec41840d6ac2103987163459e75", + "7bbb29ae32117597fcdf21b464441abd571dad52d053b9c2f7204f8ea8c4762e", + }, + }, + "500 error": { + responses: []responseFixture{ + { + status: http.StatusInternalServerError, + body: "{}", + url: url, + }, + }, + expectedError: errors.New("invalid response: 500 Internal Server Error"), + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + var ( + assert = assert.New(t) + ) + + responses := make(chan responseFixture, len(test.responses)) + for _, response := range test.responses { + responses <- response + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + response := <-responses + assert.Equal("application/json", r.Header.Get("Content-Type")) + assert.Equal("POST", r.Method) + assert.Equal(response.url, r.URL.RequestURI()) + + w.WriteHeader(response.status) + fmt.Fprintln(w, response.body) + })) + + client := NewClient(ts.URL, MainnetGenesisBlockIdentifier, MainnetCurrency) + txs, err := client.RawMempool(context.Background()) + if test.expectedError != nil { + assert.Contains(err.Error(), test.expectedError.Error()) + } else { + assert.NoError(err) + assert.Equal(test.expectedTransactions, txs) + } + }) + } +} + // loadFixture takes a file name and returns the response fixture. func loadFixture(fileName string) string { content, err := ioutil.ReadFile(fmt.Sprintf("client_fixtures/%s", fileName))