Skip to content
Merged
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
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.6.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/urfave/cli v1.22.15
go.uber.org/mock v0.4.0
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -18,6 +19,7 @@ require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -33,6 +35,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
Expand Down
33 changes: 33 additions & 0 deletions internal/sbi/api_hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package sbi

import (
"net/http"

"github.com/gin-gonic/gin"
)

func (s *Server) getHelloRoute() []Route {
return []Route{
{
Name: "GetHello",
Method: "GET",
Pattern: "/",
APIFunc: s.GetHello,
},
{
Name: "PostHello",
Method: "POST",
Pattern: "/:message",
APIFunc: s.PostHello,
},
}
}

func (s *Server) GetHello(c *gin.Context) {
c.String(http.StatusOK, "Hello from feature/hello!")
}

func (s *Server) PostHello(c *gin.Context) {
message := c.Param("message")
c.String(http.StatusOK, "Received message: %s", message)
}
79 changes: 79 additions & 0 deletions internal/sbi/api_hello_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package sbi_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/andy89923/nf-example/internal/sbi"
"github.com/andy89923/nf-example/pkg/factory"
"github.com/gin-gonic/gin"
"go.uber.org/mock/gomock"
)

func Test_getHelloRoutes(t *testing.T) {
gin.SetMode(gin.TestMode)

mockCtrl := gomock.NewController(t)
nfApp := sbi.NewMocknfApp(mockCtrl)
nfApp.EXPECT().Config().Return(&factory.Config{
Configuration: &factory.Configuration{
Sbi: &factory.Sbi{
Port: 8000,
},
},
}).AnyTimes()
server := sbi.NewServer(nfApp, "")

t.Run("GetHello", func(t *testing.T) {
const EXPECTED_STATUS = http.StatusOK
const EXPECTED_BODY = "Hello from feature/hello!"

httpRecorder := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(httpRecorder)

var err error
ginCtx.Request, err = http.NewRequest("GET", "/", nil)
if err != nil {
t.Errorf("Failed to create request: %s", err)
return
}

server.GetHello(ginCtx)

if httpRecorder.Code != EXPECTED_STATUS {
t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code)
}

if httpRecorder.Body.String() != EXPECTED_BODY {
t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String())
}
})

t.Run("PostHello", func(t *testing.T) {
const EXPECTED_STATUS = http.StatusOK
const TEST_MESSAGE = "test"
const EXPECTED_BODY = "Received message: test"

httpRecorder := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(httpRecorder)

var err error
ginCtx.Request, err = http.NewRequest("POST", "/"+TEST_MESSAGE, nil)
if err != nil {
t.Errorf("Failed to create request: %s", err)
return
}
ginCtx.Params = []gin.Param{{Key: "message", Value: TEST_MESSAGE}}

server.PostHello(ginCtx)

if httpRecorder.Code != EXPECTED_STATUS {
t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code)
}

if httpRecorder.Body.String() != EXPECTED_BODY {
t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String())
}
})
}
3 changes: 3 additions & 0 deletions internal/sbi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func newRouter(s *Server) *gin.Engine {
ChiikawaGroup := router.Group("/chiikawa")
applyRoutes(ChiikawaGroup, s.getChiikawaRoute())

helloGroup := router.Group("/hello")
applyRoutes(helloGroup, s.getHelloRoute())

return router
}

Expand Down
Loading