-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
continue with more iterations on http server
- Loading branch information
Showing
4 changed files
with
286 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// InMemoryPlayerStore collects data about players in memory | ||
type InMemoryPlayerStore struct{} | ||
|
||
// RecordWin will record a player's win | ||
func (i *InMemoryPlayerStore) RecordWin(name string) { | ||
} | ||
|
||
// GetPlayerScore retrieves scores for a given player | ||
func (i *InMemoryPlayerStore) GetPlayerScore(name string) string { | ||
return "123" | ||
} | ||
|
||
func main() { | ||
server := &PlayerServer{&InMemoryPlayerStore{}} | ||
|
||
if err := http.ListenAndServe(":5000", server); err != nil { | ||
log.Fatalf("could not listen on port 5000 %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// PlayerStore stores score information about players | ||
type PlayerStore interface { | ||
GetPlayerScore(name string) string | ||
RecordWin(name string) | ||
} | ||
|
||
// PlayerServer is a HTTP interface for player information | ||
type PlayerServer struct { | ||
store PlayerStore | ||
} | ||
|
||
func (p *PlayerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
|
||
switch r.Method { | ||
case http.MethodPost: | ||
p.processWin(w) | ||
case http.MethodGet: | ||
p.showScore(w, r) | ||
} | ||
|
||
} | ||
|
||
func (p *PlayerServer) showScore(w http.ResponseWriter, r *http.Request) { | ||
player := r.URL.Path[len("/players/"):] | ||
|
||
score := p.store.GetPlayerScore(player) | ||
|
||
if score == "" { | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
|
||
fmt.Fprint(w, score) | ||
} | ||
|
||
func (p *PlayerServer) processWin(w http.ResponseWriter) { | ||
p.store.RecordWin("Bob") | ||
w.WriteHeader(http.StatusAccepted) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
type StubPlayerStore struct { | ||
scores map[string]string | ||
winCalls []string | ||
} | ||
|
||
func (s *StubPlayerStore) GetPlayerScore(name string) string { | ||
score := s.scores[name] | ||
return score | ||
} | ||
|
||
func (s *StubPlayerStore) RecordWin(name string) { | ||
s.winCalls = append(s.winCalls, name) | ||
} | ||
|
||
func TestGETPlayers(t *testing.T) { | ||
store := StubPlayerStore{ | ||
map[string]string{ | ||
"Pepper": "20", | ||
"Floyd": "10", | ||
}, | ||
nil, | ||
} | ||
server := &PlayerServer{&store} | ||
|
||
t.Run("returns Pepper's score", func(t *testing.T) { | ||
req := newGetScoreRequest("Pepper") | ||
res := httptest.NewRecorder() | ||
|
||
server.ServeHTTP(res, req) | ||
|
||
assertStatus(t, res.Code, http.StatusOK) | ||
assertResponseBody(t, res.Body.String(), "20") | ||
}) | ||
|
||
t.Run("returns Floyd's score", func(t *testing.T) { | ||
req := newGetScoreRequest("Floyd") | ||
res := httptest.NewRecorder() | ||
|
||
server.ServeHTTP(res, req) | ||
|
||
assertStatus(t, res.Code, http.StatusOK) | ||
assertResponseBody(t, res.Body.String(), "10") | ||
}) | ||
|
||
t.Run("returns 404 on missing players", func(t *testing.T) { | ||
req := newGetScoreRequest("Apollo") | ||
res := httptest.NewRecorder() | ||
|
||
server.ServeHTTP(res, req) | ||
|
||
assertStatus(t, res.Code, http.StatusNotFound) | ||
}) | ||
} | ||
|
||
func TestStoreWins(t *testing.T) { | ||
store := StubPlayerStore{ | ||
map[string]string{}, | ||
nil, | ||
} | ||
server := &PlayerServer{&store} | ||
|
||
t.Run("it accepts POSTs to /win", func(t *testing.T) { | ||
req, _ := http.NewRequest(http.MethodPost, "/players/Pepper/win", nil) | ||
res := httptest.NewRecorder() | ||
|
||
server.ServeHTTP(res, req) | ||
|
||
assertStatus(t, res.Code, http.StatusAccepted) | ||
|
||
if len(store.winCalls) != 1 { | ||
t.Errorf("got %d calls to RecordWin want %d", len(store.winCalls), 1) | ||
} | ||
}) | ||
} | ||
|
||
func assertStatus(t *testing.T, got, want int) { | ||
t.Helper() | ||
if got != want { | ||
t.Errorf("did not get correct status, got %d, want %d", got, want) | ||
} | ||
} | ||
|
||
func newGetScoreRequest(name string) *http.Request { | ||
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/players/%s", name), nil) | ||
return req | ||
} | ||
|
||
func assertResponseBody(t *testing.T, got, want string) { | ||
t.Helper() | ||
if got != want { | ||
t.Errorf("response body is wrong, got '%s' want '%s'", got, want) | ||
} | ||
} |