Skip to content

Commit

Permalink
Maintain source backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jbduncan authored and onsi committed Mar 18, 2024
1 parent a6c8875 commit a350b95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
21 changes: 12 additions & 9 deletions ghttp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/onsi/gomega/internal/gutil"
"github.com/onsi/gomega/types"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/protoadapt"
"google.golang.org/protobuf/runtime/protoiface"
)

type GHTTPWithGomega struct {
Expand Down Expand Up @@ -193,7 +195,7 @@ func (g GHTTPWithGomega) VerifyFormKV(key string, values ...string) http.Handler
// representation of the passed message.
//
// VerifyProtoRepresenting also verifies that the request's content type is application/x-protobuf
func (g GHTTPWithGomega) VerifyProtoRepresenting(expected proto.Message) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyProtoRepresenting(expected protoiface.MessageV1) http.HandlerFunc {
return CombineHandlers(
g.VerifyContentType("application/x-protobuf"),
func(w http.ResponseWriter, req *http.Request) {
Expand All @@ -204,13 +206,14 @@ func (g GHTTPWithGomega) VerifyProtoRepresenting(expected proto.Message) http.Ha
expectedType := reflect.TypeOf(expected)
actualValuePtr := reflect.New(expectedType.Elem())

actual, ok := actualValuePtr.Interface().(proto.Message)
g.gomega.Expect(ok).Should(BeTrueBecause("Message value should be a proto.Message"))
actual, ok := actualValuePtr.Interface().(protoiface.MessageV1)
g.gomega.Expect(ok).Should(BeTrueBecause("Message value should be a protoiface.MessageV1"))

err = proto.Unmarshal(body, actual)
err = proto.Unmarshal(body, protoadapt.MessageV2Of(actual))
g.gomega.Expect(err).ShouldNot(HaveOccurred(), "Failed to unmarshal protobuf")

g.gomega.Expect(proto.Equal(expected, actual)).Should(BeTrue(), "ProtoBuf Mismatch")
g.gomega.Expect(proto.Equal(protoadapt.MessageV2Of(expected), protoadapt.MessageV2Of(actual))).
Should(BeTrue(), "ProtoBuf Mismatch")
},
)
}
Expand Down Expand Up @@ -328,9 +331,9 @@ func (g GHTTPWithGomega) RespondWithJSONEncodedPtr(statusCode *int, object inter
// containing the protobuf serialization of the provided message.
//
// Also, RespondWithProto can be given an optional http.Header. The headers defined therein will be added to the response headers.
func (g GHTTPWithGomega) RespondWithProto(statusCode int, message proto.Message, optionalHeader ...http.Header) http.HandlerFunc {
func (g GHTTPWithGomega) RespondWithProto(statusCode int, message protoadapt.MessageV1, optionalHeader ...http.Header) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
data, err := proto.Marshal(message)
data, err := proto.Marshal(protoadapt.MessageV2Of(message))
g.gomega.Expect(err).ShouldNot(HaveOccurred())

var headers http.Header
Expand Down Expand Up @@ -397,7 +400,7 @@ func VerifyFormKV(key string, values ...string) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyFormKV(key, values...)
}

func VerifyProtoRepresenting(expected proto.Message) http.HandlerFunc {
func VerifyProtoRepresenting(expected protoiface.MessageV1) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyProtoRepresenting(expected)
}

Expand All @@ -417,6 +420,6 @@ func RespondWithJSONEncodedPtr(statusCode *int, object interface{}, optionalHead
return NewGHTTPWithGomega(gomega.Default).RespondWithJSONEncodedPtr(statusCode, object, optionalHeader...)
}

func RespondWithProto(statusCode int, message proto.Message, optionalHeader ...http.Header) http.HandlerFunc {
func RespondWithProto(statusCode int, message protoadapt.MessageV1, optionalHeader ...http.Header) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).RespondWithProto(statusCode, message, optionalHeader...)
}
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
Expand All @@ -31,10 +29,6 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down

0 comments on commit a350b95

Please sign in to comment.