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
20 changes: 20 additions & 0 deletions internal/sbi/api_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
74 changes: 74 additions & 0 deletions internal/sbi/api_default_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading