-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
194 lines (149 loc) · 4.81 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package poker_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gorilla/websocket"
poker "github.com/ljones140/golang-player-webserver"
)
var (
dummyGame = &poker.GameSpy{}
tenMS = 10 * time.Millisecond
)
func TestGETPlayers(t *testing.T) {
store := poker.StubPlayerStore{
Scores: map[string]int{
"Pepper": 20,
"Floyd": 10,
},
}
server := mustMakePlayerServer(t, &store, dummyGame)
t.Run("returns Pepper's score", func(t *testing.T) {
request := poker.NewGetScoreRequest("Pepper")
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
poker.AssertResponseBody(t, response.Body.String(), "20")
assertStatus(t, response, http.StatusOK)
})
t.Run("returns Floyd's score", func(t *testing.T) {
request := poker.NewGetScoreRequest("Floyd")
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
poker.AssertResponseBody(t, response.Body.String(), "10")
assertStatus(t, response, http.StatusOK)
})
t.Run("returns 404 when player does not exist", func(t *testing.T) {
request := poker.NewGetScoreRequest("NonExistantPlayer")
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
assertStatus(t, response, http.StatusNotFound)
})
}
func TestLeague(t *testing.T) {
t.Run("it returns the league table as JSON", func(t *testing.T) {
wantedLeague := []poker.Player{
{"Cleo", 32},
{"Cleo", 20},
{"Cleo", 14},
}
store := poker.StubPlayerStore{nil, nil, wantedLeague}
server := mustMakePlayerServer(t, &store, dummyGame)
request := poker.NewGetLeagueRequest()
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
got := poker.GetLeagueFromResponse(t, response.Body)
poker.AssertLeague(t, got, wantedLeague)
assertStatus(t, response, http.StatusOK)
poker.AssertContentType(t, response, "application/json")
})
}
func TestStoreWins(t *testing.T) {
store := poker.StubPlayerStore{
Scores: map[string]int{},
}
server := mustMakePlayerServer(t, &store, dummyGame)
t.Run("it records win when POST", func(t *testing.T) {
player := "Pepper"
request := poker.NewPostWinRequest(player)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
assertStatus(t, response, http.StatusAccepted)
//TODO: use helper here
if len(store.WinCalls) != 1 {
t.Errorf("got %d calls to RecordWin want %d", len(store.WinCalls), 1)
}
if store.WinCalls[0] != player {
t.Errorf("did not store correct winner go %q want %q", store.WinCalls[0], player)
}
})
}
func TestGame(t *testing.T) {
t.Run("GET /game returns a 200", func(t *testing.T) {
server := mustMakePlayerServer(t, &poker.StubPlayerStore{}, dummyGame)
request := poker.NewGameRequest()
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
assertStatus(t, response, http.StatusOK)
})
t.Run("starts a game with given no of players, sends blind alerts via websocket and declares winner", func(t *testing.T) {
wantedBlindAlert := "Blind is 100"
winner := "Ruth"
game := &poker.GameSpy{BlindAlert: []byte(wantedBlindAlert)}
server := httptest.NewServer(mustMakePlayerServer(t, dummyPlayerStore, game))
ws := mustDialWS(t, "ws"+strings.TrimPrefix(server.URL, "http")+"/ws")
defer server.Close()
defer ws.Close()
writeWSMessage(t, ws, "3")
writeWSMessage(t, ws, winner)
assertGameStartedWith(t, game, 3)
assertFinishCalledWith(t, game, winner)
within(t, tenMS, func() { assertWebsocketGotMsg(t, ws, wantedBlindAlert) })
})
}
func assertStatus(t testing.TB, response *httptest.ResponseRecorder, want int) {
t.Helper()
got := response.Code
if got != want {
t.Errorf("did not get correct status, got %d want %d", got, want)
}
}
func assertWebsocketGotMsg(t *testing.T, ws *websocket.Conn, want string) {
_, gotMessage, _ := ws.ReadMessage()
if string(gotMessage) != want {
t.Errorf("got blind alert %q, wanted blind alert %q", string(gotMessage), want)
}
}
func mustMakePlayerServer(t *testing.T, store poker.PlayerStore, game poker.Game) *poker.PlayerServer {
server, err := poker.NewPlayerServer(store, game)
if err != nil {
t.Fatal("problem creating player server", err)
}
return server
}
func mustDialWS(t *testing.T, url string) *websocket.Conn {
ws, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
t.Fatalf("could not open a ws connection on %s %v", url, err)
}
return ws
}
func writeWSMessage(t *testing.T, conn *websocket.Conn, message string) {
if err := conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
t.Fatalf("could not send message over ws connection %v", err)
}
}
func within(t testing.TB, d time.Duration, assert func()) {
t.Helper()
done := make(chan struct{}, 1)
go func() {
assert()
done <- struct{}{}
}()
select {
case <-time.After(d):
t.Error("timed out")
case <-done:
}
}