Skip to content

Commit

Permalink
Merge pull request #572 from tdakkota/fix/handle-no-body-correctly
Browse files Browse the repository at this point in the history
fix(middleware): do not type assert request body if operation has not body
  • Loading branch information
ernado authored Sep 17, 2022
2 parents a48fa41 + f8d601c commit 5af2602
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
48 changes: 45 additions & 3 deletions internal/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package internal
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -97,6 +99,23 @@ func (s *testMiddleware) PetUploadAvatarByID(
return &api.PetUploadAvatarByIDOK{}, nil
}

func (s *testMiddleware) GetHeader(ctx context.Context, params api.GetHeaderParams) (api.Hash, error) {
h := sha256.Sum256([]byte(params.XAuthToken))
return api.Hash{
Raw: h[:],
Hex: hex.EncodeToString(h[:]),
}, nil
}

func (s *testMiddleware) ErrorGet(ctx context.Context) (api.ErrorStatusCode, error) {
return api.ErrorStatusCode{
StatusCode: http.StatusInternalServerError,
Response: api.Error{
Message: "test_error",
},
}, nil
}

func TestMiddleware(t *testing.T) {
a := require.New(t)
ctx := context.Background()
Expand All @@ -107,6 +126,12 @@ func TestMiddleware(t *testing.T) {
return zapcore.NewTee(orig, observerCore)
}),
)
checkLog := func(a *require.Assertions) {
entries := logs.TakeAll()
a.Len(entries, 3)
a.Equal("Handling request", entries[0].Message)
a.Equal("Success", entries[2].Message)
}

handler := &testMiddleware{}
h, err := api.NewServer(handler, handler,
Expand All @@ -123,6 +148,9 @@ func TestMiddleware(t *testing.T) {
client, err := api.NewClient(s.URL, handler, api.WithClient(s.Client()))
a.NoError(err)

// Test an endpoint with params and body.
//
// Check that request was modified.
stream := api.PetUploadAvatarByIDReq{
Data: io.NopCloser(bytes.NewReader(petAvatar)),
}
Expand All @@ -131,8 +159,22 @@ func TestMiddleware(t *testing.T) {
})
a.NoError(err)
a.Equal(&api.PetUploadAvatarByIDOK{}, got)
checkLog(a)

entries := logs.All()
a.Len(entries, 3)
a.Equal("Handling request", entries[0].Message)
// Test an endpoint with params only.
const token = "test_token"
hash, err := client.GetHeader(ctx, api.GetHeaderParams{
XAuthToken: token,
})
a.NoError(err)
sum := sha256.Sum256([]byte(token))
a.Equal(sum[:], hash.Raw)
checkLog(a)

// Test an endpoint without params and body.
code, err := client.ErrorGet(ctx)
a.NoError(err)
a.Equal(http.StatusInternalServerError, code.StatusCode)
a.Equal("test_error", code.Response.Message)
checkLog(a)
}
5 changes: 4 additions & 1 deletion middleware/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ func HookMiddleware[RequestType, ParamsType, ResponseType any](
cb func(context.Context, RequestType, ParamsType) (ResponseType, error),
) (r ResponseType, err error) {
next := func(req Request) (Response, error) {
request := req.Body.(RequestType)
var request RequestType
if body := req.Body; body != nil {
request = body.(RequestType)
}
var params ParamsType
if unpack != nil {
params = unpack(req.Params)
Expand Down
2 changes: 1 addition & 1 deletion middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Request struct {
OperationName string
// OperationID is the spec operation ID, if any.
OperationID string
// Body is the operation request body. May be nil, if the operation has no body.
// Body is the operation request body. May be nil, if the operation has not body.
Body any
// Params is the operation parameters.
Params map[string]any
Expand Down

0 comments on commit 5af2602

Please sign in to comment.