diff --git a/go.mod b/go.mod index d36d9ba..a313925 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 diff --git a/internal/sbi/api_hello.go b/internal/sbi/api_hello.go new file mode 100644 index 0000000..51f8669 --- /dev/null +++ b/internal/sbi/api_hello.go @@ -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) +} diff --git a/internal/sbi/api_hello_test.go b/internal/sbi/api_hello_test.go new file mode 100644 index 0000000..fca1386 --- /dev/null +++ b/internal/sbi/api_hello_test.go @@ -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()) + } + }) +} diff --git a/internal/sbi/router.go b/internal/sbi/router.go index 34a902d..1d16794 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -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 }