Skip to content

Commit e3c57ac

Browse files
committed
add woc test
1 parent 29878c0 commit e3c57ac

File tree

1 file changed

+140
-10
lines changed

1 file changed

+140
-10
lines changed

transaction/broadcaster/woc_test.go

+140-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package broadcaster
22

33
import (
4+
"fmt"
45
"io"
56
"net/http"
67
"strings"
@@ -15,7 +16,7 @@ type MockFailureClient struct{}
1516
func (m *MockFailureClient) Do(req *http.Request) (*http.Response, error) {
1617
return &http.Response{
1718
StatusCode: 500,
18-
Body: io.NopCloser(strings.NewReader("")),
19+
Body: io.NopCloser(strings.NewReader("Internal Server Error")),
1920
}, nil
2021
}
2122

@@ -28,23 +29,152 @@ func (m *MockSuccessClient) Do(req *http.Request) (*http.Response, error) {
2829
}, nil
2930
}
3031

31-
func TestWhatsOnChainBroadcastFail(t *testing.T) {
32-
tx, err := transaction.NewTransactionFromHex("0100000001a9b0c5a2437042e5d0c6288fad6abc2ef8725adb6fef5f1bab21b2124cfb7cf6dc9300006a47304402204c3f88aadc90a3f29669bba5c4369a2eebc10439e857a14e169d19626243ffd802205443013b187a5c7f23e2d5dd82bc4ea9a79d138a3dc6cae6e6ef68874bd23a42412103fd290068ae945c23a06775de8422ceb6010aaebab40b78e01a0af3f1322fa861ffffffff010000000000000000b1006a0963657274696861736822314c6d763150594d70387339594a556e374d3948565473446b64626155386b514e4a4032356163343531383766613035616532626436346562323632386666336432666636646338313665383335376364616366343765663862396331656433663531403064383963343363343636303262643865313831376530393137313736343134353938373337623161663865363939343930646364653462343937656338643300000000")
33-
require.NoError(t, err)
32+
type MockNetworkErrorClient struct{}
33+
34+
func (m *MockNetworkErrorClient) Do(req *http.Request) (*http.Response, error) {
35+
return nil, fmt.Errorf("network error")
36+
}
37+
38+
type MockBadRequestClient struct{}
39+
40+
func (m *MockBadRequestClient) Do(req *http.Request) (*http.Response, error) {
41+
return &http.Response{
42+
StatusCode: 400,
43+
Body: io.NopCloser(strings.NewReader("Bad Request")),
44+
}, nil
45+
}
46+
47+
type MockUnauthorizedClient struct{}
48+
49+
func (m *MockUnauthorizedClient) Do(req *http.Request) (*http.Response, error) {
50+
return &http.Response{
51+
StatusCode: 401,
52+
Body: io.NopCloser(strings.NewReader("Unauthorized")),
53+
}, nil
54+
}
55+
56+
type MockBodyReadErrorClient struct{}
57+
58+
type ErrorReader struct{}
59+
60+
func (e *ErrorReader) Read(p []byte) (int, error) {
61+
return 0, fmt.Errorf("read error")
62+
}
63+
64+
func (e *ErrorReader) Close() error {
65+
return nil
66+
}
67+
68+
func (m *MockBodyReadErrorClient) Do(req *http.Request) (*http.Response, error) {
69+
return &http.Response{
70+
StatusCode: 500,
71+
Body: &ErrorReader{},
72+
}, nil
73+
}
74+
75+
func TestWhatsOnChainBroadcast(t *testing.T) {
76+
tx := &transaction.Transaction{
77+
// Populate with valid data
78+
// For simplicity, we'll use an empty transaction
79+
}
80+
81+
b := &WhatsOnChain{
82+
Network: "main",
83+
ApiKey: "",
84+
Client: &MockSuccessClient{},
85+
}
86+
87+
success, failure := b.Broadcast(tx)
88+
require.NotNil(t, success)
89+
require.Nil(t, failure)
90+
require.Equal(t, "f702453dd03b0f055e5437d76128141803984fb10acb85fc3b2184fae2f3fa78", success.Txid)
91+
}
92+
93+
func TestWhatsOnChainBroadcastFailure(t *testing.T) {
94+
tx := &transaction.Transaction{
95+
// Populate with valid data
96+
}
97+
3498
b := &WhatsOnChain{
3599
Network: "main",
36100
ApiKey: "",
37101
Client: &MockFailureClient{},
38102
}
39103

40-
success, failure := tx.Broadcast(b)
104+
success, failure := b.Broadcast(tx)
41105
require.Nil(t, success)
42106
require.NotNil(t, failure)
107+
require.Equal(t, "500", failure.Code)
108+
require.Equal(t, "Internal Server Error", failure.Description)
109+
}
43110

44-
b.Client = &MockSuccessClient{}
111+
func TestWhatsOnChainBroadcastClientError(t *testing.T) {
112+
tx := &transaction.Transaction{
113+
// Populate with valid data
114+
}
45115

46-
success, failure = tx.Broadcast(b)
47-
require.NotNil(t, success)
48-
require.Nil(t, failure)
49-
require.Equal(t, "4d76b00f29e480e0a933cef9d9ffe303d6ab919e2cdb265dd2cea41089baa85a", success.Txid)
116+
b := &WhatsOnChain{
117+
Network: "main",
118+
ApiKey: "",
119+
Client: &MockNetworkErrorClient{},
120+
}
121+
122+
success, failure := b.Broadcast(tx)
123+
require.Nil(t, success)
124+
require.NotNil(t, failure)
125+
require.Contains(t, failure.Description, "network error")
126+
}
127+
128+
func TestWhatsOnChainBroadcastBadRequest(t *testing.T) {
129+
tx := &transaction.Transaction{
130+
// Populate with valid data
131+
}
132+
133+
b := &WhatsOnChain{
134+
Network: "main",
135+
ApiKey: "",
136+
Client: &MockBadRequestClient{},
137+
}
138+
139+
success, failure := b.Broadcast(tx)
140+
require.Nil(t, success)
141+
require.NotNil(t, failure)
142+
require.Equal(t, "400", failure.Code)
143+
require.Equal(t, "Bad Request", failure.Description)
144+
}
145+
146+
func TestWhatsOnChainBroadcastUnauthorized(t *testing.T) {
147+
tx := &transaction.Transaction{
148+
// Populate with valid data
149+
}
150+
151+
b := &WhatsOnChain{
152+
Network: "main",
153+
ApiKey: "invalid_api_key",
154+
Client: &MockUnauthorizedClient{},
155+
}
156+
157+
success, failure := b.Broadcast(tx)
158+
require.Nil(t, success)
159+
require.NotNil(t, failure)
160+
require.Equal(t, "401", failure.Code)
161+
require.Equal(t, "Unauthorized", failure.Description)
162+
}
163+
164+
func TestWhatsOnChainBroadcastBodyReadError(t *testing.T) {
165+
tx := &transaction.Transaction{
166+
// Populate with valid data
167+
}
168+
169+
b := &WhatsOnChain{
170+
Network: "main",
171+
ApiKey: "",
172+
Client: &MockBodyReadErrorClient{},
173+
}
174+
175+
success, failure := b.Broadcast(tx)
176+
require.Nil(t, success)
177+
require.NotNil(t, failure)
178+
require.Equal(t, "500", failure.Code)
179+
require.Equal(t, "unknown error", failure.Description)
50180
}

0 commit comments

Comments
 (0)