Skip to content

Commit

Permalink
Adapt player name extraction (quii#463)
Browse files Browse the repository at this point in the history
* Adapt player name extraction in json chapter

This was changed with quii#283 in the previous chapter. Following through with this change in the json chapter to stay consistent.

* Adapt player name extraction in io chapter

* Adapt player name extraction in cmdline chapter

* Adapt player name restriction in time chapter

* Adapt player name extraction in websocket chapter
  • Loading branch information
voelzmo authored Jun 10, 2021
1 parent 395712b commit d703543
Show file tree
Hide file tree
Showing 25 changed files with 60 additions and 41 deletions.
3 changes: 2 additions & 1 deletion command-line/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion command-line/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion command-line/v3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players
Expand Down Expand Up @@ -60,7 +61,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io/v3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io/v4/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io/v5/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io/v6/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io/v7/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io/v8/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion io/v9/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
29 changes: 12 additions & 17 deletions json.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package main
import (
"fmt"
"net/http"
"strings"
)

type PlayerStore interface {
Expand All @@ -27,7 +28,7 @@ type PlayerServer struct {
}

func (p *PlayerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down Expand Up @@ -120,26 +121,20 @@ Before worrying about actual scores and JSON we will try and keep the changes sm
## Try to run the test

```
=== RUN TestLeague/it_returns_200_on_/league
panic: runtime error: slice bounds out of range [recovered]
panic: runtime error: slice bounds out of range
goroutine 6 [running]:
testing.tRunner.func1(0xc42010c3c0)
/usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:742 +0x29d
panic(0x1274d60, 0x1438240)
/usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229
github.com/quii/learn-go-with-tests/json-and-io/v2.(*PlayerServer).ServeHTTP(0xc420048d30, 0x12fc1c0, 0xc420010940, 0xc420116000)
/Users/quii/go/src/github.com/quii/learn-go-with-tests/json-and-io/v2/server.go:20 +0xec
--- FAIL: TestLeague/it_returns_200_on_/league (0.00s)
server_test.go:101: status code is wrong: got 404, want 200
FAIL
FAIL playerstore 0.221s
FAIL
```

Your `PlayerServer` should be panicking like this. Go to the line of code in the stack trace which is pointing to `server.go`.
Our `PlayerServer` returns a `404 Not Found`, as if we were trying to get the wins for an unknown player. Looking at how `server.go` implements `ServeHTTP`, we realize that it always assumes to be called with a URL pointing to a specific player:

```go
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")
```

In the previous chapter, we mentioned this was a fairly naive way of doing our routing. What is happening is it's trying to split the string of the path starting at an index beyond `/league` so it is `slice bounds out of range`.
In the previous chapter, we mentioned this was a fairly naive way of doing our routing. Our test informs us correctly that we need a concept how to deal with different request paths.

## Write enough code to make it pass

Expand All @@ -158,7 +153,7 @@ func (p *PlayerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}))

router.Handle("/players/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down Expand Up @@ -199,7 +194,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion json/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand All @@ -17,7 +18,7 @@ type PlayerServer struct {
}

func (p *PlayerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion json/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -37,7 +38,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion json/v3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -51,7 +52,7 @@ func (p *PlayerServer) getLeagueTable() []Player {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion json/v4/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -46,7 +47,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion json/v5/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
3 changes: 2 additions & 1 deletion json/v6/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// PlayerStore stores score information about players.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
}

func (p *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
player := r.URL.Path[len("/players/"):]
player := strings.TrimPrefix(r.URL.Path, "/players/")

switch r.Method {
case http.MethodPost:
Expand Down
Loading

0 comments on commit d703543

Please sign in to comment.