-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
68 lines (56 loc) · 1.69 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package paddle_test
import (
"embed"
"net/http"
"net/http/httptest"
)
type stubPath string
const (
event_types stubPath = "testdata/event_types.json"
events stubPath = "testdata/events.json"
transaction stubPath = "testdata/transaction.json"
transactions stubPath = "testdata/transactions.json"
transactionsPaginatedPg1 stubPath = "testdata/transactions_paginated_pg1.json"
transactionsPaginatedPg2 stubPath = "testdata/transactions_paginated_pg2.json"
priceCreatedEvent stubPath = "testdata/price_created.json"
simulation stubPath = "testdata/simulation.json"
simulationScenario stubPath = "testdata/simulation_scenario.json"
simulations stubPath = "testdata/simulations.json"
simulationRun stubPath = "testdata/simulation_run.json"
simulationRunWithEvents stubPath = "testdata/simulation_run_with_events.json"
)
//go:embed testdata
var exampleData embed.FS
type stub struct {
paths []stubPath
}
type mockServerResponse struct {
stub *stub
body *[]byte
}
func mockServerForExample(res mockServerResponse) *httptest.Server {
call := 0
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
var body []byte
if res.stub != nil && len(res.stub.paths) > 0 {
stub := res.stub.paths[0]
if len(res.stub.paths) > 1 {
stub = res.stub.paths[call]
}
c, err := exampleData.ReadFile(string(stub))
if err != nil {
w.Write([]byte(err.Error()))
return
}
body = c
}
if res.body != nil {
body = *res.body
}
w.Write(body)
call++
}))
return s
}