Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apiendpoint/api_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package apiendpoint

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -146,6 +147,8 @@ func executeAPIEndpoint[TReq any, TResp any](w http.ResponseWriter, r *http.Requ
return apierror.NewBadRequestf("Error unmarshaling request body: %s.", err)
}
}

r.Body = io.NopCloser(bytes.NewReader(reqData))
}

if rawExtractor, ok := any(&req).(RawExtractor); ok {
Expand Down
74 changes: 41 additions & 33 deletions apiendpoint/api_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -48,35 +49,35 @@ func TestMountAndServe(t *testing.T) {
}
}

t.Run("GetEndpointAndExtractRaw", func(t *testing.T) {
t.Run("GetEndpoint", func(t *testing.T) {
t.Parallel()

mux, bundle := setup(t)

req := httptest.NewRequest(http.MethodGet, "/api/get-endpoint/Hello.", nil)
req := httptest.NewRequest(http.MethodGet, "/api/get-endpoint", nil)
mux.ServeHTTP(bundle.recorder, req)

requireStatusAndJSONResponse(t, http.StatusOK, &postResponse{Message: "Hello."}, bundle.recorder)
requireStatusAndJSONResponse(t, http.StatusOK, &getResponse{Message: "Hello."}, bundle.recorder)
})

t.Run("BodyIgnoredOnGet", func(t *testing.T) {
t.Parallel()

mux, bundle := setup(t)

req := httptest.NewRequest(http.MethodGet, "/api/get-endpoint/Hello.",
req := httptest.NewRequest(http.MethodGet, "/api/get-endpoint",
bytes.NewBuffer(mustMarshalJSON(t, &getRequest{IgnoredJSONMessage: "Ignored hello."})))
mux.ServeHTTP(bundle.recorder, req)

requireStatusAndJSONResponse(t, http.StatusOK, &postResponse{Message: "Hello."}, bundle.recorder)
requireStatusAndJSONResponse(t, http.StatusOK, &getResponse{Message: "Hello."}, bundle.recorder)
})

t.Run("MethodNotAllowed", func(t *testing.T) {
t.Parallel()

mux, bundle := setup(t)

req := httptest.NewRequest(http.MethodPost, "/api/get-endpoint/Hello.", nil)
req := httptest.NewRequest(http.MethodPost, "/api/get-endpoint", nil)
mux.ServeHTTP(bundle.recorder, req)

// This error comes from net/http.
Expand All @@ -91,11 +92,11 @@ func TestMountAndServe(t *testing.T) {
mux := http.NewServeMux()
Mount(mux, &postEndpoint{}, nil)

req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint",
bytes.NewBuffer(mustMarshalJSON(t, &postRequest{Message: "Hello."})))
reqPayload := mustMarshalJSON(t, &postRequest{Message: "Hello."})
req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint/123", bytes.NewBuffer(reqPayload))
mux.ServeHTTP(bundle.recorder, req)

requireStatusAndJSONResponse(t, http.StatusCreated, &postResponse{Message: "Hello."}, bundle.recorder)
requireStatusAndJSONResponse(t, http.StatusCreated, &postResponse{ID: "123", Message: "Hello.", RawPayload: reqPayload}, bundle.recorder)
})

t.Run("OptionsWithCustomLogger", func(t *testing.T) {
Expand All @@ -104,33 +105,32 @@ func TestMountAndServe(t *testing.T) {
_, bundle := setup(t)

mux := http.NewServeMux()
Mount(mux, &postEndpoint{}, &MountOpts{Logger: bundle.logger})
Mount(mux, &getEndpoint{}, &MountOpts{Logger: bundle.logger})

req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint",
bytes.NewBuffer(mustMarshalJSON(t, &postRequest{Message: "Hello."})))
req := httptest.NewRequest(http.MethodGet, "/api/get-endpoint", nil)
mux.ServeHTTP(bundle.recorder, req)

requireStatusAndJSONResponse(t, http.StatusCreated, &postResponse{Message: "Hello."}, bundle.recorder)
requireStatusAndJSONResponse(t, http.StatusOK, &getResponse{Message: "Hello."}, bundle.recorder)
})

t.Run("PostEndpoint", func(t *testing.T) {
t.Run("PostEndpointAndExtractRaw", func(t *testing.T) {
t.Parallel()

mux, bundle := setup(t)

req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint",
bytes.NewBuffer(mustMarshalJSON(t, &postRequest{Message: "Hello."})))
reqPayload := mustMarshalJSON(t, &postRequest{Message: "Hello."})
req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint/123", bytes.NewBuffer(reqPayload))
mux.ServeHTTP(bundle.recorder, req)

requireStatusAndJSONResponse(t, http.StatusCreated, &postResponse{Message: "Hello."}, bundle.recorder)
requireStatusAndJSONResponse(t, http.StatusCreated, &postResponse{ID: "123", Message: "Hello.", RawPayload: reqPayload}, bundle.recorder)
})

t.Run("ValidationError", func(t *testing.T) {
t.Parallel()

mux, bundle := setup(t)

req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint", nil)
req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint/123", nil)
mux.ServeHTTP(bundle.recorder, req)

requireStatusAndJSONResponse(t, http.StatusBadRequest, &apierror.APIError{Message: "Field `message` is required."}, bundle.recorder)
Expand All @@ -141,7 +141,7 @@ func TestMountAndServe(t *testing.T) {

mux, bundle := setup(t)

req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint",
req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint/123",
bytes.NewBuffer(mustMarshalJSON(t, &postRequest{MakeAPIError: true, Message: "Hello."})))
mux.ServeHTTP(bundle.recorder, req)

Expand All @@ -153,7 +153,7 @@ func TestMountAndServe(t *testing.T) {

mux, bundle := setup(t)

req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint",
req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint/123",
bytes.NewBuffer(mustMarshalJSON(t, &postRequest{MakePostgresError: true, Message: "Hello."})))
mux.ServeHTTP(bundle.recorder, req)

Expand All @@ -168,7 +168,7 @@ func TestMountAndServe(t *testing.T) {
ctx, cancel := context.WithDeadline(ctx, time.Now())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/api/post-endpoint",
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/api/post-endpoint/123",
bytes.NewBuffer(mustMarshalJSON(t, &postRequest{Message: "Hello."})))
require.NoError(t, err)
mux.ServeHTTP(bundle.recorder, req)
Expand All @@ -181,7 +181,7 @@ func TestMountAndServe(t *testing.T) {

mux, bundle := setup(t)

req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint",
req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint/123",
bytes.NewBuffer(mustMarshalJSON(t, &postRequest{MakeInternalError: true, Message: "Hello."})))
mux.ServeHTTP(bundle.recorder, req)

Expand Down Expand Up @@ -274,19 +274,13 @@ type getEndpoint struct {

func (*getEndpoint) Meta() *EndpointMeta {
return &EndpointMeta{
Pattern: "GET /api/get-endpoint/{message}",
Pattern: "GET /api/get-endpoint",
StatusCode: http.StatusOK,
}
}

type getRequest struct {
IgnoredJSONMessage string `json:"ignored_json" validate:"-"`
Message string `json:"-" validate:"required"`
}

func (req *getRequest) ExtractRaw(r *http.Request) error {
req.Message = r.PathValue("message")
return nil
}

type getResponse struct {
Expand All @@ -299,7 +293,7 @@ func (a *getEndpoint) Execute(_ context.Context, req *getRequest) (*getResponse,
return &getResponse{Message: req.IgnoredJSONMessage}, nil
}

return &getResponse{Message: req.Message}, nil
return &getResponse{Message: "Hello."}, nil
}

//
Expand All @@ -312,20 +306,34 @@ type postEndpoint struct {

func (*postEndpoint) Meta() *EndpointMeta {
return &EndpointMeta{
Pattern: "POST /api/post-endpoint",
Pattern: "POST /api/post-endpoint/{id}",
StatusCode: http.StatusCreated,
}
}

type postRequest struct {
ID string `json:"-" validate:"-"`
MakeAPIError bool `json:"make_api_error" validate:"-"`
MakeInternalError bool `json:"make_internal_error" validate:"-"`
MakePostgresError bool `json:"make_postgres_error" validate:"-"`
Message string `json:"message" validate:"required"`
RawPayload []byte `json:"-" validate:"-"`
}

func (req *postRequest) ExtractRaw(r *http.Request) error {
var err error
if req.RawPayload, err = io.ReadAll(r.Body); err != nil {
return err
}

req.ID = r.PathValue("id")
return nil
}

type postResponse struct {
Message string `json:"message"`
ID string `json:"id"`
Message string `json:"message"`
RawPayload json.RawMessage `json:"raw_payload"`
}

func (a *postEndpoint) Execute(ctx context.Context, req *postRequest) (*postResponse, error) {
Expand All @@ -346,5 +354,5 @@ func (a *postEndpoint) Execute(ctx context.Context, req *postRequest) (*postResp
return nil, fmt.Errorf("error running Postgres query: %w", &pgconn.PgError{Code: pgerrcode.InsufficientPrivilege})
}

return &postResponse{Message: req.Message}, nil
return &postResponse{ID: req.ID, Message: req.Message, RawPayload: req.RawPayload}, nil
}
Loading