-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeRequest_test.go
102 lines (87 loc) · 2.7 KB
/
makeRequest_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
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
package pogifyapi
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/alicebob/miniredis/v2"
"github.com/gin-gonic/gin"
)
func Test_server_makeRequest(t *testing.T) {
m, _ := miniredis.Run()
defer m.Close()
os.Setenv("REDIS_URI", "redis://"+m.Addr())
router := gin.Default()
Server(router.Group("/"))
t.Run("empty call", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/request", nil)
router.ServeHTTP(w, req)
if w.Code != 400 {
t.Errorf("empty call to makeRequest didn't return 400, but %v", w.Code)
}
})
t.Run("invalid body, inactive session", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/request", strings.NewReader("{\"invalid\":\"invalid\"}"))
router.ServeHTTP(w, req)
if w.Code != 400 {
t.Errorf("invalid body to makeRequest didn't return 400, but %v", w.Code)
}
})
t.Run("valid request, inactive session", func(t *testing.T) {
validBody := request{
Session: "notexist",
Provider: "notaprovider",
Token: "not.a.token",
Request: "not a request",
}
validBodyBytes, _ := json.Marshal(validBody)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/request", bytes.NewReader(validBodyBytes))
router.ServeHTTP(w, req)
if expect := 404; w.Code != expect {
body, _ := ioutil.ReadAll(w.Body)
t.Errorf("%s", string(body))
t.Errorf("invalid body to makeRequest didn't return %v, but %v", expect, w.Code)
}
})
t.Run("valid request, active session", func(t *testing.T) {
validBody := request{
Session: "exist",
Provider: "notaprovider",
Token: "not.a.token",
Request: "not a request",
}
validBodyBytes, _ := json.Marshal(validBody)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/request", bytes.NewReader(validBodyBytes))
router.ServeHTTP(w, req)
if expect := 200; w.Code != expect {
body, _ := ioutil.ReadAll(w.Body)
t.Errorf("%s", string(body))
t.Errorf("invalid body to makeRequest didn't return %v, but %v", expect, w.Code)
}
})
t.Run("repeated call should 429", func(t *testing.T) {
validBody := request{
Session: "exist",
Provider: "notaprovider",
Token: "not.a.token",
Request: "not a request",
}
validBodyBytes, _ := json.Marshal(validBody)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/request", bytes.NewReader(validBodyBytes))
router.ServeHTTP(w, req)
if expect := 429; w.Code != expect {
body, _ := ioutil.ReadAll(w.Body)
t.Errorf("%s", string(body))
t.Errorf("invalid body to makeRequest didn't return %v, but %v", expect, w.Code)
}
})
}