Skip to content

Commit

Permalink
Merge pull request #9 from jjuarez/feature/gin-tests
Browse files Browse the repository at this point in the history
test: Changes to add more coverage
  • Loading branch information
jjuarez committed Jul 16, 2022
2 parents 0164449 + b1380d5 commit 82aa7cf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
11 changes: 9 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

const envVariableKey = "DEFAULT_NAME"

func main() {
func setupRouter() *gin.Engine {
gin.ForceConsoleColor()
router := gin.Default()

router.GET("/sayhi", func(context *gin.Context) {
Expand All @@ -22,5 +23,11 @@ func main() {
greetingMessage := greeting.Greeting(name)
context.JSON(http.StatusOK, gin.H{"message": greetingMessage})
})
router.Run() // listen and server on 0.0.0.0:8080

return router
}

func main() {
router := setupRouter()
router.Run() // listen and server on 0.0.0.0:${PORT}
}
28 changes: 26 additions & 2 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
package main

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/jjuarez/dagger-golang-example/internal/greeting"
"github.com/stretchr/testify/assert"
)

func TestMain(t *testing.T) {
func TestWithoutName(t *testing.T) {
t.Run("your real tests should go here", func(t *testing.T) {
assert.True(t, true, "This's just a placeholder")
testRouter := setupRouter()
record := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/sayhi", nil)
want := fmt.Sprintf("{\"message\":\"Hi, %s!\"}", greeting.DefaultName)
testRouter.ServeHTTP(record, req)

assert.Equal(t, http.StatusOK, record.Code)
assert.Equal(t, want, record.Body.String())
})
}

func TestWithName(t *testing.T) {
t.Run("your real tests should go here", func(t *testing.T) {
testRouter := setupRouter()
record := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/sayhi/testing", nil)
want := "{\"message\":\"Hi, testing!\"}"
testRouter.ServeHTTP(record, req)

assert.Equal(t, http.StatusOK, record.Code)
assert.Equal(t, want, record.Body.String())
})
}

0 comments on commit 82aa7cf

Please sign in to comment.