forked from stellar-deprecated/horizon
/
helpers_test.go
65 lines (48 loc) · 1.21 KB
/
helpers_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
package horizon
import (
"bytes"
"encoding/json"
"fmt"
"log"
"github.com/PuerkitoBio/throttled"
"github.com/stellar/go-horizon/test"
)
func NewTestApp() *App {
app, err := NewApp(NewTestConfig())
if err != nil {
log.Panic(err)
}
return app
}
func NewTestConfig() Config {
return Config{
DatabaseUrl: test.DatabaseUrl(),
StellarCoreDatabaseUrl: test.StellarCoreDatabaseUrl(),
RateLimit: throttled.PerHour(10),
}
}
func NewRequestHelper(app *App) test.RequestHelper {
return test.NewRequestHelper(app.web.router)
}
func ShouldBePageOf(actual interface{}, options ...interface{}) string {
body := actual.(*bytes.Buffer)
expected := options[0].(int)
var result map[string]interface{}
err := json.Unmarshal(body.Bytes(), &result)
if err != nil {
return fmt.Sprintf("Could not unmarshal json:\n%s\n", body.String())
}
embedded, ok := result["_embedded"]
if !ok {
return "No _embedded key in response"
}
records, ok := embedded.(map[string]interface{})["records"]
if !ok {
return "No records key in _embedded"
}
length := len(records.([]interface{}))
if length != expected {
return fmt.Sprintf("Expected %d records in page, got %d", expected, length)
}
return ""
}