Skip to content

Commit

Permalink
test(issue-2): added test case for invalid path parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaba505 committed Sep 29, 2024
1 parent b8cee4a commit 0e5b864
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 9 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.0

require (
github.com/stretchr/testify v1.9.0
github.com/z5labs/bedrock v0.10.0
github.com/z5labs/bedrock v0.10.1
go.opentelemetry.io/contrib/bridges/otelslog v0.5.0
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.6.0
Expand All @@ -19,6 +19,7 @@ require (
go.opentelemetry.io/otel/sdk/log v0.6.0
go.opentelemetry.io/otel/sdk/metric v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
golang.org/x/sync v0.8.0
google.golang.org/protobuf v1.34.2
)

Expand All @@ -39,7 +40,6 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-viper/mapstructure/v2 v2.1.0 h1:gHnMa2Y/pIxElCH2GlZZ1lZSsn6XMtufpGyP1XxdC/w=
github.com/go-viper/mapstructure/v2 v2.1.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand Down Expand Up @@ -49,10 +47,8 @@ github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCO
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
github.com/z5labs/bedrock v0.9.8 h1:YWGvStpAy/+qDOZnPMNPhWZbRT6dCZKtHRaudfJmuNo=
github.com/z5labs/bedrock v0.9.8/go.mod h1:fmqIPNcZJ8PZYHZ9Nl3DxbrQcXAEfUQF+ayjnvCiwy8=
github.com/z5labs/bedrock v0.10.0 h1:ALQYv048QRNuQq8FAWPGNSEqdGUeexIv0EbgMiwmQ64=
github.com/z5labs/bedrock v0.10.0/go.mod h1:IdEKPA+TgqDFsSVD6L+elp9eJGmSL3xQ61bC7UjNmQA=
github.com/z5labs/bedrock v0.10.1 h1:qp0CyJPAAU925ACr4SSmlX/r4kLZZutmHIb51z40/ww=
github.com/z5labs/bedrock v0.10.1/go.mod h1:IdEKPA+TgqDFsSVD6L+elp9eJGmSL3xQ61bC7UjNmQA=
go.opentelemetry.io/contrib/bridges/otelslog v0.5.0 h1:lU3F57OSLK5mQ1PDBVAfDDaKCPv37MrEbCfTzsF4bz0=
go.opentelemetry.io/contrib/bridges/otelslog v0.5.0/go.mod h1:I84u06zJFr8T5D73fslEUbnRBimVVSBhuVw8L8I92AU=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 h1:ZIg3ZT/aQ7AfKqdwp7ECpOK6vHqquXXuyTjIO8ZdmPs=
Expand Down
5 changes: 5 additions & 0 deletions rest/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func mapErrorToStatus(err error) *humuspb.Status {
Code: humuspb.Code_INVALID_ARGUMENT,
Message: fmt.Sprintf("invalid header: %s", e.Header),
}
case endpoint.InvalidPathParamError:
return &humuspb.Status{
Code: humuspb.Code_INVALID_ARGUMENT,
Message: fmt.Sprintf("invalid path param: %s", e.Param),
}
case endpoint.InvalidQueryParamError:
return &humuspb.Status{
Code: humuspb.Code_INVALID_ARGUMENT,
Expand Down
115 changes: 114 additions & 1 deletion rest/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package rest

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -15,8 +16,9 @@ import (
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/z5labs/humus/humuspb"

"github.com/stretchr/testify/assert"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
Expand Down Expand Up @@ -369,6 +371,117 @@ func TestErrHandler_HandleError(t *testing.T) {
}
})

t.Run("if a InvalidPathParamError is returned", func(t *testing.T) {
h := noopHandler{}

e := NewEndpoint(
http.MethodGet,
"/{id}",
h,
PathParams(
PathParam{
Name: "id",
Pattern: "^[a-zA-Z]$",
},
),
)

op := e.operation.OpenApi()
b, _ := json.Marshal(op)
fmt.Println(string(b))

app := New(
ListenOn(0),
RegisterEndpoint(e),
)

addrCh := make(chan net.Addr)
app.listen = func(network, addr string) (net.Listener, error) {
defer close(addrCh)
ls, err := net.Listen(network, addr)
if err != nil {
return nil, err
}
addrCh <- ls.Addr()
return ls, nil
}

ctx, cancel := context.WithCancel(context.Background())
eg, egctx := errgroup.WithContext(ctx)
eg.Go(func() error {
return app.Run(egctx)
})

respCh := make(chan *http.Response, 1)
eg.Go(func() error {
defer cancel()
defer close(respCh)

addr := <-addrCh
if addr == nil {
return errors.New("received nil net.Addr")
}

req, err := http.NewRequestWithContext(
egctx,
http.MethodGet,
fmt.Sprintf("http://%s/123", addr),
nil,
)
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}

select {
case <-egctx.Done():
case respCh <- resp:
}
return nil
})

err := eg.Wait()
if !assert.Nil(t, err) {
return
}

resp := <-respCh
if !assert.NotNil(t, resp) {
return
}
defer resp.Body.Close()

if !assert.Equal(t, http.StatusBadRequest, resp.StatusCode) {
return
}

headers := resp.Header
if !assert.Contains(t, headers, "Content-Type") {
return
}
if !assert.Equal(t, ProtobufContentType, headers.Get("Content-Type")) {
return
}

b, err = io.ReadAll(resp.Body)
if !assert.Nil(t, err) {
return
}

var status humuspb.Status
err = proto.Unmarshal(b, &status)
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.Code) {
return
}
})

t.Run("if a InvalidQueryParamError is returned", func(t *testing.T) {
h := noopHandler{}

Expand Down

0 comments on commit 0e5b864

Please sign in to comment.