forked from quii/learn-go-with-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip - starting storing of player scores
- Loading branch information
Showing
4 changed files
with
173 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,22 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// InMemoryPlayerStore collects data about players in memory | ||
type InMemoryPlayerStore struct{} | ||
|
||
// 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,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// PlayerStore stores score information about players | ||
type PlayerStore interface { | ||
GetPlayerScore(name string) string | ||
} | ||
|
||
// PlayerServer is a HTTP interface for player information | ||
type PlayerServer struct { | ||
store PlayerStore | ||
} | ||
|
||
func (p *PlayerServer) ServeHTTP(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) | ||
} |
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,91 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
type StubPlayerStore struct { | ||
scores map[string]string | ||
} | ||
|
||
func (s *StubPlayerStore) GetPlayerScore(name string) string { | ||
score := s.scores[name] | ||
return score | ||
} | ||
|
||
func TestGETPlayers(t *testing.T) { | ||
store := StubPlayerStore{ | ||
map[string]string{ | ||
"Pepper": "20", | ||
"Floyd": "10", | ||
}, | ||
} | ||
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 IgnoreTestStoreWins(t *testing.T) { | ||
store := StubPlayerStore{ | ||
map[string]string{}, | ||
} | ||
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) | ||
}) | ||
} | ||
|
||
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) | ||
} | ||
} |