Skip to content
This repository was archived by the owner on May 28, 2026. It is now read-only.
Draft
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
32 changes: 31 additions & 1 deletion acceptance/ping_test.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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"))
})
})
})
})
4 changes: 4 additions & 0 deletions api/source-score.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ info:

paths:
/ping:
post:
responses:
201:
description: ping posted
get:
responses:
200:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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})
}
7 changes: 7 additions & 0 deletions pkg/domain/protocols/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

option go_package = "/handlers";

message IncomingMessage{
string message = 1;
}
143 changes: 143 additions & 0 deletions pkg/handlers/message.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/handlers/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading