-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
80 lines (70 loc) · 2.28 KB
/
server_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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestShuffle(t *testing.T) {
e := echo.New()
body := `{ "values": ["Sword", "Axe", "Bow"] }`
req := httptest.NewRequest(http.MethodPost, "/shuffle", strings.NewReader(body))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
response := ShuffleResponse{}
if assert.NoError(t, shuffleHandler(c)) {
if assert.NoError(t, json.NewDecoder(rec.Body).Decode(&response)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Contains(t, response.Result, "Sword", "Axe", "Bow")
}
}
}
func TestPick(t *testing.T) {
e := echo.New()
body := `{ "values": ["Sword", "Axe", "Bow"] }`
req := httptest.NewRequest(http.MethodPost, "/pick", strings.NewReader(body))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
response := PickResponse{}
if assert.NoError(t, pickHandler(c)) {
if assert.NoError(t, json.NewDecoder(rec.Body).Decode(&response)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Len(t, response.Result, 1)
}
}
}
func TestPickWithCount(t *testing.T) {
e := echo.New()
body := `{ "values": ["Sword", "Axe", "Bow"], "count": 2 }`
req := httptest.NewRequest(http.MethodPost, "/pick", strings.NewReader(body))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
response := PickResponse{}
if assert.NoError(t, pickHandler(c)) {
if assert.NoError(t, json.NewDecoder(rec.Body).Decode(&response)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Len(t, response.Result, 2)
}
}
}
func TestRoll(t *testing.T) {
e := echo.New()
body := `{ "expr": "d20 + 3" }`
req := httptest.NewRequest(http.MethodPost, "/roll", strings.NewReader(body))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
response := RollResponse{}
if assert.NoError(t, rollHandler(c)) {
if assert.NoError(t, json.NewDecoder(rec.Body).Decode(&response)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Greater(t, response.Result, 3.0)
}
}
}