diff --git a/acceptance/ping_test.go b/acceptance/ping_test.go index be83689..980face 100644 --- a/acceptance/ping_test.go +++ b/acceptance/ping_test.go @@ -1,13 +1,16 @@ package acceptance_test import ( + "bytes" "encoding/json" "io" "net/http" "net/url" + "source-score/pkg/handlers" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "google.golang.org/protobuf/proto" ) var _ = Describe("API Tests", func() { @@ -27,9 +30,36 @@ var _ = Describe("API Tests", func() { var respBody responseBody err = json.Unmarshal(body, &respBody) - Expect(err).To(BeNil()) + Expect(err).To(BeNil()) Expect(respBody.Data).To(BeEquivalentTo("Pong")) }) }) + + When("POST request is sent to /ping", func() { + It("should sent message included in the reponse", func() { + msg := handlers.IncomingMessage{ + Message: "sample incoming message", + } + data, err := proto.Marshal(&msg) + Expect(err).To(BeNil()) + + resp, err := http.Post( + endpoint, + "application/x-protobuf", + bytes.NewReader(data), + ) + Expect(err).To(BeNil()) + Expect(resp.StatusCode).To(BeEquivalentTo(http.StatusOK)) + + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + Expect(err).To(BeNil()) + + var respBody responseBody + err = json.Unmarshal(body, &respBody) + Expect(err).To(BeNil()) + Expect(respBody.Data).To(ContainSubstring("sample incoming message")) + }) + }) }) }) diff --git a/api/source-score.yaml b/api/source-score.yaml index ef7993b..27c9bec 100644 --- a/api/source-score.yaml +++ b/api/source-score.yaml @@ -6,6 +6,10 @@ info: paths: /ping: + post: + responses: + 201: + description: ping posted get: responses: 200: diff --git a/go.mod b/go.mod index f493e39..5d5be6a 100644 --- a/go.mod +++ b/go.mod @@ -236,7 +236,7 @@ require ( golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.20.0 // indirect golang.org/x/tools v0.27.0 // indirect - google.golang.org/protobuf v1.34.2 // indirect + google.golang.org/protobuf v1.34.2 gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/pkg/api/router.go b/pkg/api/router.go index c618ca0..697a9c3 100644 --- a/pkg/api/router.go +++ b/pkg/api/router.go @@ -1,11 +1,13 @@ package api import ( + "io" "log/slog" "net/http" "source-score/pkg/handlers" "github.com/gin-gonic/gin" + "google.golang.org/protobuf/proto" ) type router struct { @@ -46,3 +48,19 @@ func (r *router) GetPing(ctx *gin.Context) { ctx.JSON(http.StatusOK, gin.H{"data": message}) } + +func (r *router) PostPing(ctx *gin.Context) { + body, _ := io.ReadAll(ctx.Request.Body) + incomingMsg := &handlers.IncomingMessage{ + Message: "sample incoming message", + } + + err := proto.Unmarshal(body, incomingMsg) + if err != nil { + panic("failed proto marshalling:: " + err.Error()) + } + + message := r.pingHandler.PostPing(ctx, incomingMsg) + + ctx.JSON(http.StatusOK, gin.H{"data": message}) +} diff --git a/pkg/domain/protocols/message.proto b/pkg/domain/protocols/message.proto new file mode 100644 index 0000000..c32218a --- /dev/null +++ b/pkg/domain/protocols/message.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +option go_package = "/handlers"; + +message IncomingMessage{ + string message = 1; +} diff --git a/pkg/handlers/message.pb.go b/pkg/handlers/message.pb.go new file mode 100644 index 0000000..9108da9 --- /dev/null +++ b/pkg/handlers/message.pb.go @@ -0,0 +1,143 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.31.0 +// protoc v3.19.6 +// source: pkg/domain/protocols/message.proto + +package handlers + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type IncomingMessage struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` +} + +func (x *IncomingMessage) Reset() { + *x = IncomingMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_pkg_domain_protocols_message_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IncomingMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IncomingMessage) ProtoMessage() {} + +func (x *IncomingMessage) ProtoReflect() protoreflect.Message { + mi := &file_pkg_domain_protocols_message_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IncomingMessage.ProtoReflect.Descriptor instead. +func (*IncomingMessage) Descriptor() ([]byte, []int) { + return file_pkg_domain_protocols_message_proto_rawDescGZIP(), []int{0} +} + +func (x *IncomingMessage) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +var File_pkg_domain_protocols_message_proto protoreflect.FileDescriptor + +var file_pkg_domain_protocols_message_proto_rawDesc = []byte{ + 0x0a, 0x22, 0x70, 0x6b, 0x67, 0x2f, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x73, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2b, 0x0a, 0x0f, 0x49, 0x6e, 0x63, 0x6f, 0x6d, 0x69, 0x6e, 0x67, + 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x42, 0x0b, 0x5a, 0x09, 0x2f, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x73, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_pkg_domain_protocols_message_proto_rawDescOnce sync.Once + file_pkg_domain_protocols_message_proto_rawDescData = file_pkg_domain_protocols_message_proto_rawDesc +) + +func file_pkg_domain_protocols_message_proto_rawDescGZIP() []byte { + file_pkg_domain_protocols_message_proto_rawDescOnce.Do(func() { + file_pkg_domain_protocols_message_proto_rawDescData = protoimpl.X.CompressGZIP(file_pkg_domain_protocols_message_proto_rawDescData) + }) + return file_pkg_domain_protocols_message_proto_rawDescData +} + +var file_pkg_domain_protocols_message_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_pkg_domain_protocols_message_proto_goTypes = []interface{}{ + (*IncomingMessage)(nil), // 0: IncomingMessage +} +var file_pkg_domain_protocols_message_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_pkg_domain_protocols_message_proto_init() } +func file_pkg_domain_protocols_message_proto_init() { + if File_pkg_domain_protocols_message_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_pkg_domain_protocols_message_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IncomingMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_pkg_domain_protocols_message_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_pkg_domain_protocols_message_proto_goTypes, + DependencyIndexes: file_pkg_domain_protocols_message_proto_depIdxs, + MessageInfos: file_pkg_domain_protocols_message_proto_msgTypes, + }.Build() + File_pkg_domain_protocols_message_proto = out.File + file_pkg_domain_protocols_message_proto_rawDesc = nil + file_pkg_domain_protocols_message_proto_goTypes = nil + file_pkg_domain_protocols_message_proto_depIdxs = nil +} diff --git a/pkg/handlers/ping.go b/pkg/handlers/ping.go index dd17a3e..3e8474f 100644 --- a/pkg/handlers/ping.go +++ b/pkg/handlers/ping.go @@ -15,5 +15,11 @@ func NewPingHandler() *PingHandler { } func (ph PingHandler) GetPing(ctx context.Context) string { + return ph.message } + +func (ph PingHandler) PostPing(ctx context.Context, inccomingMsg *IncomingMessage) string { + + return "incoming: " + inccomingMsg.Message + "\nresponse: " + ph.message +}