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
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
63 changes: 63 additions & 0 deletions internal/sbi/api_greeting.go
Original file line number Diff line number Diff line change
@@ -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+"!")
}
105 changes: 105 additions & 0 deletions internal/sbi/api_greeting_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
})
}
3 changes: 3 additions & 0 deletions internal/sbi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading