Skip to content

Commit 08087a1

Browse files
test: remove httpmock and resty
1 parent 1b20afb commit 08087a1

File tree

5 files changed

+115
-115
lines changed

5 files changed

+115
-115
lines changed

internal/monitor/coinbase/monitor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
poller "github.com/achannarasappa/ticker/v4/internal/monitor/coinbase/poller"
1313
streamer "github.com/achannarasappa/ticker/v4/internal/monitor/coinbase/streamer"
1414
unary "github.com/achannarasappa/ticker/v4/internal/monitor/coinbase/unary"
15-
resty "github.com/go-resty/resty/v2"
1615
)
1716

1817
type MonitorCoinbase struct {
@@ -44,7 +43,7 @@ type input struct {
4443

4544
// Config contains the required configuration for the Coinbase monitor
4645
type Config struct {
47-
Client resty.Client
46+
UnaryURL string
4847
}
4948

5049
// Option defines an option for configuring the monitor
@@ -54,7 +53,7 @@ func NewMonitorCoinbase(config Config, opts ...Option) *MonitorCoinbase {
5453

5554
ctx, cancel := context.WithCancel(context.Background())
5655

57-
unaryAPI := unary.NewUnaryAPI(config.Client)
56+
unaryAPI := unary.NewUnaryAPI(config.UnaryURL)
5857

5958
monitor := &MonitorCoinbase{
6059
assetQuotesCache: make(map[string]*c.AssetQuote),

internal/monitor/coinbase/monitor_suite_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,10 @@ package monitorCoinbase_test
33
import (
44
"testing"
55

6-
"github.com/go-resty/resty/v2"
7-
"github.com/jarcoal/httpmock"
86
. "github.com/onsi/ginkgo/v2"
97
. "github.com/onsi/gomega"
108
)
119

12-
var client = resty.New()
13-
14-
var _ = BeforeSuite(func() {
15-
httpmock.ActivateNonDefault(client.GetClient())
16-
})
17-
18-
var _ = BeforeEach(func() {
19-
httpmock.Reset()
20-
})
21-
22-
var _ = AfterSuite(func() {
23-
httpmock.DeactivateAndReset()
24-
})
25-
2610
func TestCoinbase(t *testing.T) {
2711
RegisterFailHandler(Fail)
2812
RunSpecs(t, "Coinbase Suite")

internal/monitor/coinbase/monitor_test.go

Lines changed: 112 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
package monitorCoinbase_test
22

33
import (
4-
"fmt"
54
"net/http"
65
"time"
76

8-
"github.com/go-resty/resty/v2"
9-
"github.com/jarcoal/httpmock"
107
. "github.com/onsi/ginkgo/v2"
118
. "github.com/onsi/gomega"
129

1310
c "github.com/achannarasappa/ticker/v4/internal/common"
1411
monitorCoinbase "github.com/achannarasappa/ticker/v4/internal/monitor/coinbase"
12+
unary "github.com/achannarasappa/ticker/v4/internal/monitor/coinbase/unary"
13+
"github.com/onsi/gomega/ghttp"
1514
)
1615

1716
var _ = Describe("Monitor Coinbase", func() {
17+
var (
18+
server *ghttp.Server
19+
)
20+
21+
BeforeEach(func() {
22+
server = ghttp.NewServer()
23+
})
24+
25+
AfterEach(func() {
26+
server.Close()
27+
})
1828

1929
Describe("NewMonitorCoinbase", func() {
2030
It("should return a new MonitorCoinbase", func() {
2131
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
22-
Client: *resty.New(),
32+
UnaryURL: server.URL(),
2333
})
2434
Expect(monitor).NotTo(BeNil())
2535
})
@@ -28,7 +38,7 @@ var _ = Describe("Monitor Coinbase", func() {
2838
It("should set the underlying symbols", func() {
2939
underlyingSymbols := []string{"BTC-USD"}
3040
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
31-
Client: *resty.New(),
41+
UnaryURL: server.URL(),
3242
}, monitorCoinbase.WithSymbolsUnderlying(underlyingSymbols))
3343

3444
Expect(monitor).NotTo(BeNil())
@@ -39,7 +49,7 @@ var _ = Describe("Monitor Coinbase", func() {
3949
It("should set the streaming URL", func() {
4050
url := "wss://websocket-feed.exchange.coinbase.com"
4151
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
42-
Client: *resty.New(),
52+
UnaryURL: server.URL(),
4353
}, monitorCoinbase.WithStreamingURL(url))
4454

4555
Expect(monitor).NotTo(BeNil())
@@ -50,7 +60,7 @@ var _ = Describe("Monitor Coinbase", func() {
5060
It("should set the refresh interval", func() {
5161
interval := 10 * time.Second
5262
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
53-
Client: *resty.New(),
63+
UnaryURL: server.URL(),
5464
}, monitorCoinbase.WithRefreshInterval(interval))
5565

5666
Expect(monitor).NotTo(BeNil())
@@ -61,7 +71,7 @@ var _ = Describe("Monitor Coinbase", func() {
6171
It("should set the onUpdate function", func() {
6272
onUpdate := func(symbol string, pq c.QuotePrice) {}
6373
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
64-
Client: *resty.New(),
74+
UnaryURL: server.URL(),
6575
})
6676
monitor.SetOnUpdate(onUpdate)
6777

@@ -71,35 +81,32 @@ var _ = Describe("Monitor Coinbase", func() {
7181
})
7282

7383
Describe("GetAssetQuotes", func() {
74-
BeforeEach(func() {
75-
responseFixture := `{
76-
"products": [
77-
{
78-
"base_display_symbol": "BTC",
79-
"product_type": "SPOT",
80-
"product_id": "BTC-USD",
81-
"base_name": "Bitcoin",
82-
"price": "50000.00",
83-
"price_percentage_change_24h": "2.5",
84-
"volume_24h": "1000.50",
85-
"display_name": "Bitcoin",
86-
"status": "online",
87-
"quote_currency_id": "USD",
88-
"product_venue": "CBE"
89-
}
90-
]
91-
}`
92-
responseUrl := `=~\/api\/v3\/brokerage\/market\/products.*product_ids\=BTC\-USD.*`
93-
httpmock.RegisterResponder("GET", responseUrl, func(req *http.Request) (*http.Response, error) {
94-
resp := httpmock.NewStringResponse(200, responseFixture)
95-
resp.Header.Set("Content-Type", "application/json")
96-
return resp, nil
97-
})
98-
})
99-
10084
It("should return the asset quotes", func() {
85+
server.RouteToHandler("GET", "/api/v3/brokerage/market/products",
86+
ghttp.CombineHandlers(
87+
ghttp.VerifyRequest("GET", "/api/v3/brokerage/market/products", "product_ids=BTC-USD"),
88+
ghttp.RespondWithJSONEncoded(http.StatusOK, unary.Response{
89+
Products: []unary.ResponseQuote{
90+
{
91+
Symbol: "BTC",
92+
ProductID: "BTC-USD",
93+
ShortName: "Bitcoin",
94+
Price: "50000.00",
95+
PriceChange24H: "2.5",
96+
Volume24H: "1000.50",
97+
DisplayName: "Bitcoin",
98+
MarketState: "online",
99+
Currency: "USD",
100+
ExchangeName: "CBE",
101+
ProductType: "SPOT",
102+
},
103+
},
104+
}),
105+
),
106+
)
107+
101108
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
102-
Client: *client,
109+
UnaryURL: server.URL(),
103110
})
104111

105112
monitor.SetSymbols([]string{"BTC-USD"})
@@ -114,13 +121,15 @@ var _ = Describe("Monitor Coinbase", func() {
114121

115122
When("the http request fails", func() {
116123
It("should return an error", func() {
117-
// Override the previous responder with an error response
118-
responseUrl := `=~\/api\/v3\/brokerage\/market\/products.*product_ids\=BTC\-USD.*`
119-
httpmock.RegisterResponder("GET", responseUrl,
120-
httpmock.NewErrorResponder(fmt.Errorf("network error")))
124+
server.RouteToHandler("GET", "/api/v3/brokerage/market/products",
125+
ghttp.CombineHandlers(
126+
ghttp.VerifyRequest("GET", "/api/v3/brokerage/market/products", "product_ids=BTC-USD"),
127+
ghttp.RespondWith(http.StatusInternalServerError, ""),
128+
),
129+
)
121130

122131
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
123-
Client: *client,
132+
UnaryURL: server.URL(),
124133
})
125134

126135
monitor.SetSymbols([]string{"BTC-USD"})
@@ -132,40 +141,63 @@ var _ = Describe("Monitor Coinbase", func() {
132141

133142
When("the ignoreCache flag is set to true", func() {
134143
It("should return the asset quotes from the cache", func() {
144+
baseResponse := func() http.HandlerFunc {
145+
return ghttp.CombineHandlers(
146+
ghttp.VerifyRequest("GET", "/api/v3/brokerage/market/products", "product_ids=BTC-USD"),
147+
ghttp.RespondWithJSONEncoded(http.StatusOK, unary.Response{
148+
Products: []unary.ResponseQuote{
149+
{
150+
Symbol: "BTC",
151+
ProductID: "BTC-USD",
152+
ShortName: "Bitcoin",
153+
Price: "50000.00",
154+
PriceChange24H: "2.5",
155+
Volume24H: "1000.50",
156+
DisplayName: "Bitcoin",
157+
MarketState: "online",
158+
Currency: "USD",
159+
ExchangeName: "CBE",
160+
ProductType: "SPOT",
161+
},
162+
},
163+
}),
164+
)
165+
}
166+
167+
// First response
168+
server.AppendHandlers(
169+
baseResponse(),
170+
baseResponse(),
171+
ghttp.CombineHandlers(
172+
ghttp.VerifyRequest("GET", "/api/v3/brokerage/market/products", "product_ids=BTC-USD"),
173+
ghttp.RespondWithJSONEncoded(http.StatusOK, unary.Response{
174+
Products: []unary.ResponseQuote{
175+
{
176+
Symbol: "BTC",
177+
ProductID: "BTC-USD",
178+
ShortName: "Bitcoin",
179+
Price: "55000.00",
180+
PriceChange24H: "5.0",
181+
Volume24H: "1000.50",
182+
DisplayName: "Bitcoin",
183+
MarketState: "online",
184+
Currency: "USD",
185+
ExchangeName: "CBE",
186+
ProductType: "SPOT",
187+
},
188+
},
189+
}),
190+
),
191+
)
192+
135193
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
136-
Client: *client,
194+
UnaryURL: server.URL(),
137195
})
138196

139197
monitor.SetSymbols([]string{"BTC-USD"})
140198

141199
// First call to populate cache
142200
firstQuotes := monitor.GetAssetQuotes(true)
143-
144-
// Modify the HTTP mock to return different data
145-
responseFixture := `{
146-
"products": [
147-
{
148-
"base_display_symbol": "BTC",
149-
"product_type": "SPOT",
150-
"product_id": "BTC-USD",
151-
"base_name": "Bitcoin",
152-
"price": "55000.00",
153-
"price_percentage_change_24h": "5.0",
154-
"volume_24h": "2000.50",
155-
"display_name": "Bitcoin",
156-
"status": "online",
157-
"quote_currency_id": "USD",
158-
"product_venue": "CBE"
159-
}
160-
]
161-
}`
162-
responseUrl := `=~\/api\/v3\/brokerage\/market\/products.*product_ids\=BTC\-USD.*`
163-
httpmock.RegisterResponder("GET", responseUrl, func(req *http.Request) (*http.Response, error) {
164-
resp := httpmock.NewStringResponse(200, responseFixture)
165-
resp.Header.Set("Content-Type", "application/json")
166-
return resp, nil
167-
})
168-
169201
// Second call with ignoreCache=false should return cached data
170202
secondQuotes := monitor.GetAssetQuotes(false)
171203

@@ -179,7 +211,7 @@ var _ = Describe("Monitor Coinbase", func() {
179211
Describe("Start", func() {
180212
It("should start the monitor", func() {
181213
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
182-
Client: *client,
214+
UnaryURL: server.URL(),
183215
}, monitorCoinbase.WithRefreshInterval(10*time.Second))
184216

185217
err := monitor.Start()
@@ -189,7 +221,7 @@ var _ = Describe("Monitor Coinbase", func() {
189221
When("the monitor is already started", func() {
190222
It("should return an error", func() {
191223
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
192-
Client: *client,
224+
UnaryURL: server.URL(),
193225
}, monitorCoinbase.WithRefreshInterval(10*time.Second))
194226

195227
err := monitor.Start()
@@ -202,22 +234,23 @@ var _ = Describe("Monitor Coinbase", func() {
202234
})
203235

204236
When("the initial unary request for quotes fails", func() {
205-
BeforeEach(func() {
206-
responseUrl := `=~\/api\/v3\/brokerage\/market\/products.*product_ids\=BTC\-USD.*`
207-
httpmock.RegisterResponder("GET", responseUrl,
208-
httpmock.NewErrorResponder(fmt.Errorf("network error")))
209-
})
210-
211237
It("should return an error", func() {
238+
server.RouteToHandler("GET", "/api/v3/brokerage/market/products",
239+
ghttp.CombineHandlers(
240+
ghttp.VerifyRequest("GET", "/api/v3/brokerage/market/products", "product_ids=BTC-USD"),
241+
ghttp.RespondWith(http.StatusInternalServerError, "network error"),
242+
),
243+
)
244+
212245
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
213-
Client: *client,
246+
UnaryURL: server.URL(),
214247
}, monitorCoinbase.WithRefreshInterval(10*time.Second))
215248

216249
monitor.SetSymbols([]string{"BTC-USD"})
217250

218251
err := monitor.Start()
219252
Expect(err).To(HaveOccurred())
220-
Expect(err.Error()).To(ContainSubstring("network error"))
253+
Expect(err.Error()).To(ContainSubstring("request failed with status 500"))
221254
})
222255
})
223256

@@ -249,7 +282,7 @@ var _ = Describe("Monitor Coinbase", func() {
249282

250283
It("should stop the monitor", func() {
251284
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
252-
Client: *client,
285+
UnaryURL: server.URL(),
253286
}, monitorCoinbase.WithRefreshInterval(10*time.Second))
254287

255288
err := monitor.Start()
@@ -262,7 +295,7 @@ var _ = Describe("Monitor Coinbase", func() {
262295
When("the monitor is not started", func() {
263296
It("should return an error", func() {
264297
monitor := monitorCoinbase.NewMonitorCoinbase(monitorCoinbase.Config{
265-
Client: *client,
298+
UnaryURL: server.URL(),
266299
})
267300

268301
err := monitor.Stop()

internal/monitor/coinbase/unary/unary_suite_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,10 @@ package unary_test
33
import (
44
"testing"
55

6-
"github.com/go-resty/resty/v2"
7-
"github.com/jarcoal/httpmock"
86
. "github.com/onsi/ginkgo/v2"
97
. "github.com/onsi/gomega"
108
)
119

12-
var client = resty.New()
13-
14-
var _ = BeforeSuite(func() {
15-
httpmock.ActivateNonDefault(client.GetClient())
16-
})
17-
18-
var _ = BeforeEach(func() {
19-
httpmock.Reset()
20-
})
21-
22-
var _ = AfterSuite(func() {
23-
httpmock.DeactivateAndReset()
24-
})
25-
2610
func TestUnary(t *testing.T) {
2711
RegisterFailHandler(Fail)
2812
RunSpecs(t, "Unary Suite")

0 commit comments

Comments
 (0)