diff --git a/edit.go b/edit.go index b69c1c0f..81923e4a 100644 --- a/edit.go +++ b/edit.go @@ -32,6 +32,12 @@ func (s *server) handleEdit(w http.ResponseWriter, r *http.Request) { return } + // Serve 404 for /foo. + if r.URL.Path != "/" && !strings.HasPrefix(r.URL.Path, "/p/") { + http.NotFound(w, r) + return + } + snip := &snippet{Body: []byte(hello)} if strings.HasPrefix(r.URL.Path, "/p/") { if !allowShare(r) { diff --git a/server_test.go b/server_test.go index 0c174075..86217424 100644 --- a/server_test.go +++ b/server_test.go @@ -5,6 +5,7 @@ package main import ( "bytes" + "context" "fmt" "io/ioutil" "net/http" @@ -42,8 +43,8 @@ func TestEdit(t *testing.T) { id := "bar" barBody := []byte("Snippy McSnipface") snip := &snippet{Body: barBody} - if err := s.db.PutSnippet(nil, id, snip); err != nil { - t.Fatalf("s.dbPutSnippet(nil, %+v, %+v): %v", id, snip, err) + if err := s.db.PutSnippet(context.Background(), id, snip); err != nil { + t.Fatalf("s.dbPutSnippet(context.Background(), %+v, %+v): %v", id, snip, err) } testCases := []struct { @@ -54,6 +55,7 @@ func TestEdit(t *testing.T) { respBody []byte }{ {"foo.play.golang.org to play.golang.org", "https://foo.play.golang.org", http.StatusFound, map[string]string{"Location": "https://play.golang.org"}, nil}, + {"Non-existent page", "https://play.golang.org/foo", http.StatusNotFound, nil, nil}, {"Unknown snippet", "https://play.golang.org/p/foo", http.StatusNotFound, nil, nil}, {"Existing snippet", "https://play.golang.org/p/" + id, http.StatusOK, nil, nil}, {"Plaintext snippet", "https://play.golang.org/p/" + id + ".go", http.StatusOK, nil, barBody},