From 4e3affab2b560dce5ea65134f66f00a1a08f5d0d Mon Sep 17 00:00:00 2001 From: wiwi Date: Sun, 19 Oct 2025 14:19:26 +0000 Subject: [PATCH 1/3] feat: add /default/exercise GET & POST --- internal/sbi/api_default.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/internal/sbi/api_default.go b/internal/sbi/api_default.go index 70bc577..65a1d1d 100644 --- a/internal/sbi/api_default.go +++ b/internal/sbi/api_default.go @@ -18,5 +18,21 @@ func (s *Server) getDefaultRoute() []Route { // Use // curl -X GET http://127.0.0.163:8000/default/ -w "\n" }, + { + Name: "Exercise GET", + Method: http.MethodGet, + Pattern: "/exercise", + APIFunc: func(c *gin.Context) { + c.JSON(http.StatusOK, "This is get") + }, + }, + { + Name: "Exercise POST", + Method: http.MethodPost, + Pattern: "/exercise", + APIFunc: func(c *gin.Context) { + c.JSON(http.StatusOK, "This is post") + }, + }, } } From bdd7d9053350cd2e1e9a2fbf7282914209374004 Mon Sep 17 00:00:00 2001 From: wiwi Date: Mon, 20 Oct 2025 06:33:54 +0000 Subject: [PATCH 2/3] chore: format api_default.go to satisfy gci --- internal/sbi/api_default.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/sbi/api_default.go b/internal/sbi/api_default.go index 65a1d1d..caa2258 100644 --- a/internal/sbi/api_default.go +++ b/internal/sbi/api_default.go @@ -18,19 +18,19 @@ func (s *Server) getDefaultRoute() []Route { // Use // curl -X GET http://127.0.0.163:8000/default/ -w "\n" }, - { - Name: "Exercise GET", - Method: http.MethodGet, + { + Name: "Exercise GET", + Method: http.MethodGet, Pattern: "/exercise", APIFunc: func(c *gin.Context) { c.JSON(http.StatusOK, "This is get") }, }, - { - Name: "Exercise POST", - Method: http.MethodPost, + { + Name: "Exercise POST", + Method: http.MethodPost, Pattern: "/exercise", - APIFunc: func(c *gin.Context) { + APIFunc: func(c *gin.Context) { c.JSON(http.StatusOK, "This is post") }, }, From 5a3e332eaedbe282f55371f343bcd4e32e9d35d0 Mon Sep 17 00:00:00 2001 From: wiwi Date: Tue, 21 Oct 2025 04:54:09 +0000 Subject: [PATCH 3/3] test: add unit test --- internal/sbi/api_default.go | 4 ++ internal/sbi/api_default_test.go | 74 ++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 internal/sbi/api_default_test.go diff --git a/internal/sbi/api_default.go b/internal/sbi/api_default.go index caa2258..204cbd8 100644 --- a/internal/sbi/api_default.go +++ b/internal/sbi/api_default.go @@ -36,3 +36,7 @@ func (s *Server) getDefaultRoute() []Route { }, } } + +func (s *Server) RegisterDefaultRoutes(group *gin.RouterGroup) { + applyRoutes(group, s.getDefaultRoute()) +} diff --git a/internal/sbi/api_default_test.go b/internal/sbi/api_default_test.go new file mode 100644 index 0000000..ee0cccb --- /dev/null +++ b/internal/sbi/api_default_test.go @@ -0,0 +1,74 @@ +package sbi_test + +import ( + "bytes" + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/Alonza0314/nf-example/internal/sbi" + "github.com/gin-gonic/gin" +) + +func TestDefaultRoot(t *testing.T) { + gin.SetMode(gin.TestMode) + r := gin.New() + + s := &sbi.Server{} + group := r.Group("/default") + s.RegisterDefaultRoutes(group) + + req := httptest.NewRequest(http.MethodGet, "/default/", nil).WithContext(context.Background()) + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Fatalf("status=%d, want 200", w.Code) + } + got := strings.TrimSpace(w.Body.String()) + if got != `"Hello free5GC!"` { + t.Fatalf("body=%s, want %q", got, "Hello free5GC!") + } +} + +func TestDefaultExerciseGET(t *testing.T) { + gin.SetMode(gin.TestMode) + r := gin.New() + s := &sbi.Server{} + group := r.Group("/default") + s.RegisterDefaultRoutes(group) + + req := httptest.NewRequest(http.MethodGet, "/default/exercise", nil).WithContext(context.Background()) + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Fatalf("status=%d, want 200", w.Code) + } + if strings.TrimSpace(w.Body.String()) != `"This is get"` { + t.Fatalf("body=%s, want %q", w.Body.String(), "This is get") + } +} + +func TestDefaultExercisePOST(t *testing.T) { + gin.SetMode(gin.TestMode) + r := gin.New() + s := &sbi.Server{} + group := r.Group("/default") + s.RegisterDefaultRoutes(group) + + req := httptest.NewRequest(http.MethodPost, "/default/exercise", bytes.NewBufferString(`{}`)). + WithContext(context.Background()) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Fatalf("status=%d, want 200", w.Code) + } + if strings.TrimSpace(w.Body.String()) != `"This is post"` { + t.Fatalf("body=%s, want %q", w.Body.String(), "This is post") + } +}