diff --git a/go.mod b/go.mod index a313925..d36d9ba 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ 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 @@ -19,7 +18,6 @@ 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 @@ -35,7 +33,6 @@ 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_greeting.go b/internal/sbi/api_greeting.go new file mode 100644 index 0000000..229c506 --- /dev/null +++ b/internal/sbi/api_greeting.go @@ -0,0 +1,63 @@ +package sbi + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +var namelist struct { + firstname []string +} + +func (s *Server) getGreetingRoute() []Route { + return []Route{ + { + Name: "Greeting", + Method: "GET", + Pattern: "/", + APIFunc: s.GetGreeting, + // Use + // curl -X GET http://127.0.0.163:8000/greeting/ -w "\n" + }, + { + Name: "Farewell", + Method: "POST", + Pattern: "/", + APIFunc: s.PostFarewell, + // Use + // curl -X POST http://127.0.0.163:8000/greeting/ -w "\n" + }, + { + Name: "Farewell to someone", + Method: http.MethodPost, + Pattern: "/to", + APIFunc: s.Greetingto, + // Use + // curl -X POST http://127.0.0.163:8000/greeting/to -d '{"name": "Alisa"}' -w "\n" + }, + } +} + +func (s *Server) GetGreeting(c *gin.Context) { + c.String(http.StatusOK, "Greetings!\n") +} + +func (s *Server) PostFarewell(c *gin.Context) { + c.String(http.StatusOK, "Farewells!\n") +} + +func (s *Server) Greetingto(c *gin.Context) { + var names struct { + Name string `json:"name"` + } + + err := c.ShouldBindJSON(&names) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + namelist.firstname = append(namelist.firstname, names.Name) + c.String(http.StatusOK, "Farewell ~ "+names.Name+"!") +} diff --git a/internal/sbi/api_greeting_test.go b/internal/sbi/api_greeting_test.go new file mode 100644 index 0000000..ac10a59 --- /dev/null +++ b/internal/sbi/api_greeting_test.go @@ -0,0 +1,105 @@ +package sbi_test + +import ( + "net/http" + "net/http/httptest" + "strings" + "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_getGreetingRoutes(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("GetGreeting", func(t *testing.T) { + const EXPECTED_STATUS = http.StatusOK + const EXPECTED_BODY = "Greetings!\n" + + httpRecorder := httptest.NewRecorder() + ginCtx, _ := gin.CreateTestContext(httpRecorder) + + var err error + ginCtx.Request, err = http.NewRequest("GET", "/greeting", nil) + if err != nil { + t.Errorf("Failed to create request: %s", err) + return + } + + server.GetGreeting(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("PostFarewell", func(t *testing.T) { + const EXPECTED_STATUS = http.StatusOK + const EXPECTED_BODY = "Farewells!\n" + + httpRecorder := httptest.NewRecorder() + ginCtx, _ := gin.CreateTestContext(httpRecorder) + + var err error + ginCtx.Request, err = http.NewRequest("POST", "/greeting", nil) + if err != nil { + t.Errorf("Failed to create request: %s", err) + return + } + + server.PostFarewell(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("Farewell to", func(t *testing.T) { + const EXPECTED_STATUS = http.StatusOK + const NAME = "Alisa" + const EXPECTED_BODY = "Farewell ~ " + NAME + "!" + + httpRecorder := httptest.NewRecorder() + ginCtx, _ := gin.CreateTestContext(httpRecorder) + + var err error + jsonBody := `{"name":"` + NAME + `"}` + ginCtx.Request, err = http.NewRequest("POST", "/greeting/to", strings.NewReader(jsonBody)) + if err != nil { + t.Errorf("Failed to create request: %s", err) + return + } + + server.Greetingto(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 1d16794..6b4c537 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -63,6 +63,9 @@ func newRouter(s *Server) *gin.Engine { helloGroup := router.Group("/hello") applyRoutes(helloGroup, s.getHelloRoute()) + greetingGroup := router.Group("/greeting") + applyRoutes(greetingGroup, s.getGreetingRoute()) + return router }