-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver_test.go
143 lines (125 loc) · 2.79 KB
/
httpserver_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
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
package poker_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
poker "github.com/Rahul-NITD/Poker"
)
func TestHTTPServer(t *testing.T) {
storage := NewSTUBStorage()
game := NewGameSpy(&SpyAlerter{}, &storage)
server := poker.NewServer(&storage, game)
t.Run("Test GET route", func(t *testing.T) {
cases := []struct {
title string
path string
expectedCode int
expectedResponse string
}{
{
"Server Listens dev",
"/players/dev",
http.StatusOK,
"1",
},
{
"Server listens Rahul",
"/players/Rahul",
http.StatusOK,
"2",
},
{
"Server listens Akku",
"/players/Akku",
http.StatusOK,
"3",
},
{
"Server listens undefined player",
"/players/IAmUndefined",
http.StatusNotFound,
"Player Not Found",
},
}
for _, test := range cases {
t.Run(test.title, func(t *testing.T) {
res, req := CreateGetRequest(test.path)
server.ServeHTTP(res, req)
AssertStatusCode(t, res.Code, test.expectedCode)
AssertResponseBody(t, res.Body.String(), test.expectedResponse)
})
}
})
t.Run("Test POST route", func(t *testing.T) {
cases := []struct {
title string
path string
expectedCode int
expectedResponse string
}{
{
"Server Records dev",
"/players/dev",
http.StatusAccepted,
"2",
},
{
"Server Records Akku",
"/players/Akku",
http.StatusAccepted,
"4",
},
{
"Server Records a New Player",
"/players/IAmUndefined",
http.StatusAccepted,
"1",
},
}
for _, test := range cases {
t.Run(test.title, func(t *testing.T) {
res, req := CreatePostRequest(test.path)
server.ServeHTTP(res, req)
AssertStatusCode(t, res.Code, test.expectedCode)
res, req = CreateGetRequest(test.path)
server.ServeHTTP(res, req)
AssertResponseBody(t, res.Body.String(), test.expectedResponse)
})
}
})
storage = NewSTUBStorage()
game = NewGameSpy(&SpyAlerter{}, &storage)
server = poker.NewServer(&storage, game)
t.Run("Test /league route", func(t *testing.T) {
res, req := CreateGetRequest("/league")
server.ServeHTTP(res, req)
AssertStatusCode(t, res.Code, http.StatusOK)
want := []poker.Player{
{
Name: "Akku",
Wins: 3,
},
{
Name: "Rahul",
Wins: 2,
},
{
Name: "dev",
Wins: 1,
},
}
var got []poker.Player
json.NewDecoder(res.Body).Decode(&got)
AssertLeague(t, got, want)
})
t.Run("GET /game returns 200", func(t *testing.T) {
store := NewSTUBStorage()
game := NewGameSpy(&SpyAlerter{}, &store)
server := poker.NewServer(&store, game)
req, _ := http.NewRequest(http.MethodGet, "/game", nil)
res := httptest.NewRecorder()
server.ServeHTTP(res, req)
AssertStatusCode(t, res.Code, 200)
})
}