-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrest_api_getter_test.go
More file actions
147 lines (117 loc) · 2.93 KB
/
Copy pathrest_api_getter_test.go
File metadata and controls
147 lines (117 loc) · 2.93 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package lol
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
)
type RESTStaticMock struct {
data RESTStaticData
buffer bytes.Buffer
sem chan bool
}
func NewRESTStaticMock(data []byte) (*RESTStaticMock, error) {
r := bytes.NewReader(data)
dec := json.NewDecoder(r)
res := &RESTStaticMock{
sem: make(chan bool, 1),
}
err := dec.Decode(&(res.data))
if err != nil {
return nil, err
}
if len(res.data.TeamIDs) < 2 {
return nil, fmt.Errorf("Incomplete team data")
}
if len(res.data.SummonerNames) < 2 || len(res.data.SummonerIDs) < 2 || len(res.data.SummonerIDs) != len(res.data.SummonerNames) {
return nil, fmt.Errorf("Incompletes summoner data")
}
if len(res.data.ChampionIDs) < 2 {
return nil, fmt.Errorf("Incomplete champions IDs")
}
if len(res.data.GameIDs) < 2 {
return nil, fmt.Errorf("Incomplete Game IDs")
}
if len(res.data.RegionCode) == 0 {
return nil, fmt.Errorf("missing region code")
}
if len(res.data.ResponseByRequest) == 0 {
return nil, fmt.Errorf("missing static request")
}
return res, nil
}
func (g *RESTStaticMock) Get(url string, v interface{}) error {
_, ok := g.data.ResponseByRequest[url]
if ok == false {
log.Printf("Non recognized url: %s", url)
return RESTError{Code: 404}
}
g.sem <- true
g.buffer.Reset()
r := io.TeeReader(bytes.NewReader(g.data.ResponseByRequest[url]), &g.buffer)
dec := json.NewDecoder(r)
return dec.Decode(v)
}
func (g *RESTStaticMock) LastJSONData() string {
defer func() { <-g.sem }()
return g.buffer.String()
}
func (g *RESTStaticMock) ATeamID() string {
return g.data.TeamIDs[0]
}
func (g *RESTStaticMock) SeveralTeamIDs() []string {
return g.data.TeamIDs
}
func (g *RESTStaticMock) ASummonerName() string {
return g.data.SummonerNames[0]
}
func (g *RESTStaticMock) SeveralSummonerNames() []string {
return g.data.SummonerNames
}
func (g *RESTStaticMock) ASummonerID() string {
return g.data.SummonerIDs[0]
}
func (g *RESTStaticMock) SeveralSummonerIDs() []string {
return g.data.SummonerIDs
}
func (g *RESTStaticMock) AChampionID() string {
return g.data.ChampionIDs[0]
}
func (g *RESTStaticMock) SeveralChampionIDs() []string {
return g.data.SummonerIDs
}
func (g *RESTStaticMock) AGameID() string {
return g.data.GameIDs[0]
}
func (g *RESTStaticMock) SeveralGameIDs() []string {
return g.data.GameIDs
}
func (g *RESTStaticMock) RegionCode() string {
return g.data.RegionCode
}
func (g *RESTStaticMock) Key() APIKey {
return g.data.Key
}
var getter *RESTStaticMock
var regionTest *Region
var api *APIEndpoint
func init() {
data, err := Asset("data/go-lol_testdata.json")
if err != nil {
panic(fmt.Sprintf("Could not load test data: %s", err))
}
getter, err = NewRESTStaticMock(data)
if err != nil {
panic(fmt.Sprintf("Could not parse static data: %s", err))
}
regionTest, err = NewRegionByCode(getter.RegionCode())
if err != nil {
panic(err)
}
api = &APIEndpoint{
g: getter,
region: regionTest,
key: getter.Key(),
}
}