|
|
|
@ -7,8 +7,9 @@ import (
|
|
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"git.runcible.io/learning/pulley/internal/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func newTestApplication() application {
|
|
|
|
@ -35,15 +36,12 @@ func TestHealthRoute(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
body = bytes.TrimSpace(body)
|
|
|
|
|
|
|
|
|
|
if !strings.Contains(string(body), "environment: test") {
|
|
|
|
|
t.Fatalf("Did not find: %q in response body", "environment: test")
|
|
|
|
|
}
|
|
|
|
|
assert.StringContains(t, string(body), "environment: test")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreateMovieHandler(t *testing.T) {
|
|
|
|
@ -65,15 +63,12 @@ func TestCreateMovieHandler(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
body = bytes.TrimSpace(body)
|
|
|
|
|
|
|
|
|
|
if !strings.Contains(string(body), want) {
|
|
|
|
|
t.Fatalf("Did not find: %q in response body", want)
|
|
|
|
|
}
|
|
|
|
|
assert.StringContains(t, string(body), want)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetAllMoviesHandler(t *testing.T) {
|
|
|
|
@ -107,9 +102,7 @@ func TestGetAllMoviesHandler(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/v1/movies/%s", test.id), nil)
|
|
|
|
|
t.Logf("Path: %s, Method: %s", r.URL.Path, r.Method)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
app := newTestApplication()
|
|
|
|
|
// want to test with httprouter since we use it to parse context
|
|
|
|
@ -117,9 +110,8 @@ func TestGetAllMoviesHandler(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
resp := respRec.Result()
|
|
|
|
|
t.Logf("Code: %d", resp.StatusCode)
|
|
|
|
|
if resp.StatusCode != test.wantCode {
|
|
|
|
|
t.Fatalf("Got status code '%d', wanted status code '%d'", resp.StatusCode, test.wantCode)
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(t, resp.StatusCode, test.wantCode)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|