Added httprouter and additional movie route stubs
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
598cf87ca8
commit
ec1a4feb74
@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
fmt.Fprintln(w, "Resource created")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) getAllMoviesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintln(w, "Movies List")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintln(w, "status: available!")
|
||||||
|
fmt.Fprintf(w, "environment: %s\n", app.config.env)
|
||||||
|
fmt.Fprintf(w, "version: %s\n", Version)
|
||||||
|
}
|
@ -0,0 +1,109 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newTestApplication() application {
|
||||||
|
cfg := config{env: "test"}
|
||||||
|
return application{config: cfg, logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHealthRoute(t *testing.T) {
|
||||||
|
respRec := httptest.NewRecorder()
|
||||||
|
|
||||||
|
r, err := http.NewRequest(http.MethodGet, "/v1/healthcheck", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
app := newTestApplication()
|
||||||
|
health := http.HandlerFunc(app.healthCheckHandler)
|
||||||
|
health.ServeHTTP(respRec, r)
|
||||||
|
|
||||||
|
resp := respRec.Result()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("Got status code %q, wanted status code %q", resp.StatusCode, http.StatusOK)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body = bytes.TrimSpace(body)
|
||||||
|
|
||||||
|
if !strings.Contains(string(body), "environment: test") {
|
||||||
|
t.Fatalf("Did not find: %q in response body", "environment: test")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateMovieHandler(t *testing.T) {
|
||||||
|
respRec := httptest.NewRecorder()
|
||||||
|
want := "Resource created"
|
||||||
|
|
||||||
|
r, err := http.NewRequest(http.MethodPost, "/v1/movies", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
app := newTestApplication()
|
||||||
|
movie := http.HandlerFunc(app.createMovieHandler)
|
||||||
|
movie.ServeHTTP(respRec, r)
|
||||||
|
|
||||||
|
resp := respRec.Result()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusAccepted {
|
||||||
|
t.Fatalf("Got status code %q, wanted status code %q", resp.StatusCode, http.StatusOK)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body = bytes.TrimSpace(body)
|
||||||
|
|
||||||
|
if !strings.Contains(string(body), want) {
|
||||||
|
t.Fatalf("Did not find: %q in response body", want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetAllMoviesHandler(t *testing.T) {
|
||||||
|
respRec := httptest.NewRecorder()
|
||||||
|
want := "Movies List"
|
||||||
|
|
||||||
|
r, err := http.NewRequest(http.MethodGet, "/v1/movies", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
app := newTestApplication()
|
||||||
|
movie := http.HandlerFunc(app.getAllMoviesHandler)
|
||||||
|
movie.ServeHTTP(respRec, r)
|
||||||
|
|
||||||
|
resp := respRec.Result()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("Got status code %q, wanted status code %q", resp.StatusCode, http.StatusOK)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body = bytes.TrimSpace(body)
|
||||||
|
|
||||||
|
if !strings.Contains(string(body), want) {
|
||||||
|
t.Fatalf("Did not find: %q in response body", want)
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (app *application) HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Fprintln(w, "status: available!")
|
|
||||||
fmt.Fprintf(w, "environment: %s\n", app.config.env)
|
|
||||||
fmt.Fprintf(w, "version: %s\n", Version)
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestHealthRoute(t *testing.T) {
|
|
||||||
respRec := httptest.NewRecorder()
|
|
||||||
|
|
||||||
r, err := http.NewRequest(http.MethodGet, "/v1/healthcheck", nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
cfg := config{env: "test"}
|
|
||||||
|
|
||||||
app := application{config: cfg, logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
|
|
||||||
|
|
||||||
health := http.HandlerFunc(app.HealthCheckHandler)
|
|
||||||
health.ServeHTTP(respRec, r)
|
|
||||||
|
|
||||||
resp := respRec.Result()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
t.Fatalf("Got status code %q, wanted status code %q", resp.StatusCode, http.StatusOK)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
body = bytes.TrimSpace(body)
|
|
||||||
|
|
||||||
if !strings.Contains(string(body), "environment: test") {
|
|
||||||
t.Fatalf("Did not find: %q in response body", "environment: test")
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (app *application) routes() http.Handler {
|
||||||
|
router := httprouter.New()
|
||||||
|
|
||||||
|
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthCheckHandler)
|
||||||
|
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
|
||||||
|
router.HandlerFunc(http.MethodGet, "/v1/movies", app.getAllMoviesHandler)
|
||||||
|
|
||||||
|
return router
|
||||||
|
}
|
@ -1,4 +1,6 @@
|
|||||||
module git.runcible.io/learning/pulley
|
module git.runcible.io/learning/pulley
|
||||||
|
|
||||||
//go 1.24.1
|
//go 1.24.1
|
||||||
go 1.23.3
|
go 1.23.3
|
||||||
|
|
||||||
|
require github.com/julienschmidt/httprouter v1.3.0 // indirect
|
||||||
|
Loading…
Reference in New Issue