diff --git a/internal/sbi/api_default.go b/internal/sbi/api_default.go index 70bc577..204cbd8 100644 --- a/internal/sbi/api_default.go +++ b/internal/sbi/api_default.go @@ -18,5 +18,25 @@ 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") + }, + }, } } + +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") + } +}