Skip to content

Commit 537e9a9

Browse files
committed
Added fetch route
1 parent 4c0769e commit 537e9a9

File tree

4 files changed

+203
-104
lines changed

4 files changed

+203
-104
lines changed

api/src/bump_test.go

Lines changed: 145 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,198 @@
11
package main
22

33
import (
4-
"testing"
5-
"fmt"
6-
//
7-
"net/http"
8-
"encoding/json"
9-
"bytes"
10-
"time"
11-
"os"
12-
"io/ioutil"
13-
4+
"bytes"
5+
"crypto/md5"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"net/http"
10+
"os"
11+
"testing"
12+
"time"
1413
)
1514

1615
func startServer() {
17-
path := "store.json"
18-
err := os.Remove(path)
19-
if err != nil {
20-
fmt.Println(err)
21-
}
16+
path := "store.json"
17+
err := os.Remove(path)
18+
if err != nil {
19+
fmt.Println(err)
20+
}
2221

23-
go HandleRequests()
22+
go HandleRequests()
2423
}
2524

2625
func sendInt(guildId int) (br BumpResponse) {
27-
reqBody, _ := json.Marshal(map[string]int{
28-
"guildId": guildId,
29-
})
26+
reqBody, _ := json.Marshal(map[string]int{
27+
"guildId": guildId,
28+
})
3029

31-
resp, _ := http.Post("http://localhost:8080/V1/bump", "application/json", bytes.NewBuffer(reqBody))
30+
resp, _ := http.Post("http://localhost:8080/V1/bump", "application/json", bytes.NewBuffer(reqBody))
3231

33-
defer resp.Body.Close()
34-
body, _ := ioutil.ReadAll(resp.Body)
32+
defer resp.Body.Close()
33+
body, _ := ioutil.ReadAll(resp.Body)
3534

36-
_ = json.Unmarshal(body, &br)
35+
_ = json.Unmarshal(body, &br)
3736

38-
return br
37+
return br
3938
}
4039

4140
func sendString(guildId string) (br BumpResponse) {
42-
reqBody, _ := json.Marshal(map[string]string{
43-
"guildId": guildId,
44-
})
41+
reqBody, _ := json.Marshal(map[string]string{
42+
"guildId": guildId,
43+
})
44+
45+
resp, _ := http.Post("http://localhost:8080/V1/bump", "application/json", bytes.NewBuffer(reqBody))
4546

46-
resp, _ := http.Post("http://localhost:8080/V1/bump", "application/json", bytes.NewBuffer(reqBody))
47+
defer resp.Body.Close()
48+
body, _ := ioutil.ReadAll(resp.Body)
4749

48-
defer resp.Body.Close()
49-
body, _ := ioutil.ReadAll(resp.Body)
50+
_ = json.Unmarshal(body, &br)
5051

51-
_ = json.Unmarshal(body, &br)
52+
return br
5253

53-
return br
54+
}
5455

56+
func Hash(guilds []Guild) [16]byte {
57+
guildBytes := []byte{}
58+
for _, item := range guilds {
59+
jsonBytes, _ := json.Marshal(item)
60+
guildBytes = append(guildBytes, jsonBytes...)
61+
}
62+
return md5.Sum(guildBytes)
5563
}
5664

5765
func TestServerStart(t *testing.T) {
58-
go startServer()
66+
go startServer()
5967

60-
TempTestInterval = 1
61-
Logging = false
68+
TempTestInterval = 1
69+
Logging = false
6270

63-
time.Sleep(time.Millisecond * 200)
71+
time.Sleep(time.Millisecond * 200)
6472
}
6573

6674
func TestBumpNormal(t *testing.T) {
67-
var guildId int = 636145886279237611
75+
var guildId int = 636145886279237611
6876

69-
var expected = BumpResponse {
70-
Code: 200, // OK
71-
Payload: Guild {
72-
GuildId: guildId,
73-
},
74-
}
77+
var expected = BumpResponse{
78+
Code: 200, // OK
79+
Payload: Guild{
80+
GuildId: guildId,
81+
},
82+
}
7583

76-
// Normal send, returns 200
77-
resp := sendInt(guildId)
84+
// Normal send, returns 200
85+
resp := sendInt(guildId)
7886

79-
if resp.Code != expected.Code {
80-
t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)
81-
}
82-
if resp.Payload.GuildId != expected.Payload.GuildId {t.Errorf("GuilId's were not equal: %v != %v", resp.Payload.GuildId, expected.Payload.GuildId)}
87+
if resp.Code != expected.Code {
88+
t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)
89+
}
90+
if resp.Payload.GuildId != expected.Payload.GuildId {
91+
t.Errorf("GuilId's were not equal: %v != %v", resp.Payload.GuildId, expected.Payload.GuildId)
92+
}
8393

8494
}
8595

8696
func TestBumpEarly(t *testing.T) {
87-
var guildId int = 636145886279237699
97+
var guildId int = 636145886279237699
8898

89-
_ = sendInt(guildId) // send first bump request
99+
_ = sendInt(guildId) // send first bump request
90100

91-
var expected = BumpResponse {
92-
Code: 425, // Too Early
93-
Payload: Guild {
94-
GuildId: guildId,
95-
},
96-
}
101+
var expected = BumpResponse{
102+
Code: 425, // Too Early
103+
Payload: Guild{
104+
GuildId: guildId,
105+
},
106+
}
97107

98-
resp := sendInt(guildId) // send second (early) bump request
108+
resp := sendInt(guildId) // send second (early) bump request
99109

100-
if resp.Code != expected.Code {t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)}
110+
if resp.Code != expected.Code {
111+
t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)
112+
}
101113

102-
time.Sleep(time.Second * 2)
114+
time.Sleep(time.Second * 2)
103115

104-
expected = BumpResponse {
105-
Code: 200, // OK
106-
Payload: Guild {
107-
GuildId: guildId,
108-
},
109-
}
116+
expected = BumpResponse{
117+
Code: 200, // OK
118+
Payload: Guild{
119+
GuildId: guildId,
120+
},
121+
}
110122

111-
resp = sendInt(guildId) // send third (late) bump request!
123+
resp = sendInt(guildId) // send third (late) bump request!
112124

113-
if resp.Code != expected.Code {
114-
t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)
115-
}
116-
if resp.Payload.GuildId != expected.Payload.GuildId {t.Errorf("GuilId's were not equal: %v != %v", resp.Payload.GuildId, expected.Payload.GuildId)}
125+
if resp.Code != expected.Code {
126+
t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)
127+
}
128+
if resp.Payload.GuildId != expected.Payload.GuildId {
129+
t.Errorf("GuilId's were not equal: %v != %v", resp.Payload.GuildId, expected.Payload.GuildId)
130+
}
117131

118132
}
119133

120134
func TestBumpString(t *testing.T) {
121-
var expected = BumpResponse {
122-
Code: 400, // Bad Request
123-
Message: "Unable to process request body",
124-
Payload: Guild {
125-
GuildId: 0,
126-
},
127-
}
128-
129-
resp := sendString("636145886279237652")
130-
131-
if resp.Code != expected.Code {t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)}
132-
if resp.Payload.GuildId != expected.Payload.GuildId {t.Errorf("GuilId's were not equal: %v != %v", resp.Payload.GuildId, expected.Payload.GuildId)}
133-
if resp.Message != expected.Message {t.Errorf("Messages were not equal: %v != %v", resp.Message, expected.Message)}
135+
var expected = BumpResponse{
136+
Code: 400, // Bad Request
137+
Message: "Unable to process request body",
138+
Payload: Guild{
139+
GuildId: 0,
140+
},
141+
}
142+
143+
resp := sendString("636145886279237652")
144+
145+
if resp.Code != expected.Code {
146+
t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)
147+
}
148+
if resp.Payload.GuildId != expected.Payload.GuildId {
149+
t.Errorf("GuilId's were not equal: %v != %v", resp.Payload.GuildId, expected.Payload.GuildId)
150+
}
151+
if resp.Message != expected.Message {
152+
t.Errorf("Messages were not equal: %v != %v", resp.Message, expected.Message)
153+
}
134154
}
135155

136156
func TestBumpTooFewChars(t *testing.T) {
137-
var guildId int = 636145
138-
var expected = BumpResponse {
139-
Code: 400, // Bad Request
140-
Message: "GuildId does not conform to 18 character long integer requirement",
141-
Payload: Guild {
142-
GuildId: guildId,
143-
},
144-
}
145-
146-
resp := sendInt(guildId)
147-
148-
if resp.Code != expected.Code {t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)}
149-
if resp.Payload.GuildId != expected.Payload.GuildId {t.Errorf("GuilId's were not equal: %v != %v", resp.Payload.GuildId, expected.Payload.GuildId)}
150-
if resp.Message != expected.Message {t.Errorf("Messages were not equal: %v != %v", resp.Message, expected.Message)}
157+
var guildId int = 636145
158+
var expected = BumpResponse{
159+
Code: 400, // Bad Request
160+
Message: "GuildId does not conform to 18 character long integer requirement",
161+
Payload: Guild{
162+
GuildId: guildId,
163+
},
164+
}
165+
166+
resp := sendInt(guildId)
167+
168+
if resp.Code != expected.Code {
169+
t.Errorf("Status codes were not equal: %v != %v", resp.Code, expected.Code)
170+
}
171+
if resp.Payload.GuildId != expected.Payload.GuildId {
172+
t.Errorf("GuilId's were not equal: %v != %v", resp.Payload.GuildId, expected.Payload.GuildId)
173+
}
174+
if resp.Message != expected.Message {
175+
t.Errorf("Messages were not equal: %v != %v", resp.Message, expected.Message)
176+
}
177+
}
178+
179+
func TestFetch(t *testing.T) {
180+
var fr FetchResponse
181+
guilds, _ := LoadStore()
182+
183+
resp, _ := http.Get("http://localhost:8080/V1/fetch")
184+
185+
defer resp.Body.Close()
186+
body, _ := ioutil.ReadAll(resp.Body)
187+
188+
_ = json.Unmarshal(body, &fr)
189+
190+
if len(fr.Payload) != len(guilds) {
191+
t.Errorf("Length of stored guilds not equal to length of /V1/fetch result: %v != %v", len(fr.Payload), len(guilds))
192+
}
193+
194+
if Hash(fr.Payload) != Hash(guilds) {
195+
t.Errorf("Guilds hash from GuildStore are not equal to /V1/fetch hash: %v != %v", Hash(fr.Payload), Hash(guilds))
196+
}
197+
151198
}

api/src/fetch.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
func FetchGuilds(w http.
8+
ResponseWriter, r *http.Request) {
9+
guilds := gs.GetGuilds()
10+
11+
if len(guilds) > 0 {
12+
WriteFetchResponse(w, 200, "Ok", guilds)
13+
return
14+
}
15+
16+
WriteFetchResponse(w, 400, "BadRequest", guilds)
17+
}

api/src/main.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313
const (
1414
PORT = ":8080"
1515
BUMP_INTERVAL = 60 // 1 minute in seconds
16-
TEST_INTERVAL = 2
16+
TEST_INTERVAL = 2
1717
STORE_FILE_NAME = "store.json"
1818
)
1919

2020
var (
21-
TempTestInterval = 0 // used to set lower interval during testing
22-
Logging = true // used to disable logging during testing
23-
gs GuildStore
21+
TempTestInterval = 0 // used to set lower interval during testing
22+
Logging = true // used to disable logging during testing
23+
gs GuildStore
2424
)
2525

2626
type Guild struct {
@@ -39,6 +39,12 @@ type BumpResponse struct {
3939
Payload Guild `json:"payload"`
4040
}
4141

42+
type FetchResponse struct {
43+
Code int `json:"code"`
44+
Message string `json:"message"`
45+
Payload []Guild `json:"paypload"`
46+
}
47+
4248
func init() {
4349
var err error
4450
gs.Guilds, err = LoadStore()
@@ -62,6 +68,8 @@ func middleware(f http.HandlerFunc) http.HandlerFunc {
6268
func HandleRequests() {
6369
router := mux.NewRouter().StrictSlash(true)
6470
router.HandleFunc("/V1/bump", middleware(BumpGuild)).Methods("POST")
71+
router.HandleFunc("/V1/fetch", middleware(FetchGuilds)).Methods("GET")
72+
6573
log.Print(fmt.Sprintf("Now serving: localhost%s", PORT))
6674
err := http.ListenAndServe(PORT, router)
6775
if err != nil {
@@ -70,11 +78,11 @@ func HandleRequests() {
7078
}
7179

7280
// makes BumpResponse object and writes it to ResponseWriter
73-
func WriteBumpResponse(w http.ResponseWriter, code int, message string, guild Guild) {
81+
func WriteBumpResponse(w http.ResponseWriter, code int, message string, payload Guild) {
7482
br := BumpResponse{
7583
Code: code,
7684
Message: message,
77-
Payload: guild,
85+
Payload: payload,
7886
}
7987

8088
payloadByte, _ := json.Marshal(br)
@@ -84,6 +92,20 @@ func WriteBumpResponse(w http.ResponseWriter, code int, message string, guild Gu
8492
w.Write(payloadByte)
8593
}
8694

95+
func WriteFetchResponse(w http.ResponseWriter, code int, message string, payload []Guild) {
96+
fr := FetchResponse{
97+
Code: code,
98+
Message: message,
99+
Payload: payload,
100+
}
101+
102+
payloadByte, _ := json.Marshal(fr)
103+
104+
w.WriteHeader(code)
105+
w.Header().Set("Content-Type", "application/json")
106+
w.Write(payloadByte)
107+
}
108+
87109
func main() {
88110
HandleRequests()
89111
}

0 commit comments

Comments
 (0)