Skip to content

Commit

Permalink
add test of api
Browse files Browse the repository at this point in the history
  • Loading branch information
reki9185 committed Oct 1, 2024
1 parent ba44e08 commit 93eed7b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
22 changes: 18 additions & 4 deletions internal/sbi/api_hi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,28 @@ import (
func (s *Server) getHiRoute() []Route {
return []Route {

Check failure on line 9 in internal/sbi/api_hi.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/free5gc) --custom-order (gci)
{
Name: "Hi",
Name: "sayHello",

Check failure on line 11 in internal/sbi/api_hi.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/free5gc) --custom-order (gci)
Method: http.MethodGet,
Pattern: "/",
APIFunc: func(c *gin.Context) {
c.JSON(http.StatusOK, "Today is a good day.")
},
APIFunc: s.SayHello,
// Use
// curl -X GET http://127.0.0.163:8000/hi/ -w "\n"
},
{
Name: "sayGoodBye",
Method: http.MethodPost,
Pattern: "/",
APIFunc: s.SayGoodBye,
// Use
// curl -X POST http://127.0.0.163:8000/hi/ -w "\n"
},
}
}

func(s *Server)SayHello(c *gin.Context) {
c.String(http.StatusOK, "Hello!\n")
}

func(s *Server)SayGoodBye(c *gin.Context) {
c.String(http.StatusOK, "Good-bye!\n")
}
77 changes: 77 additions & 0 deletions internal/sbi/api_hi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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_getHiRoutes(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("sayHello", func(t *testing.T) {
const EXPECTED_STATUS = http.StatusOK
const EXPECTED_BODY = "Hello!\n"

httpRecorder := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(httpRecorder)

var err error
ginCtx.Request, err = http.NewRequest("GET", "/hi", nil)
if err != nil {
t.Errorf("Failed to create request: %s", err)
return
}

server.SayHello(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("sayGoodBye", func(t *testing.T) {
const EXPECTED_STATUS = http.StatusOK
const EXPECTED_BODY = "Good-bye!\n"

httpRecorder := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(httpRecorder)

var err error
ginCtx.Request, err = http.NewRequest("POST", "/hi", nil)
if err != nil {
t.Errorf("Failed to create request: %s", err)
return
}

server.SayGoodBye(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())
}
})
}

0 comments on commit 93eed7b

Please sign in to comment.