From 907f53a721ee27bb7a67fde718655daa50b1e8b9 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Mon, 19 Dec 2022 19:15:23 +0100 Subject: [PATCH] Implemented first test --- internal/ping_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 internal/ping_test.go diff --git a/internal/ping_test.go b/internal/ping_test.go new file mode 100644 index 0000000..a205140 --- /dev/null +++ b/internal/ping_test.go @@ -0,0 +1,39 @@ +package internal + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "testing" + + "github.com/gin-gonic/gin" + "github.com/joho/godotenv" +) + +type pingFormat struct { + Message string `json:"message"` +} + +func TestPing(t *testing.T) { + godotenv.Load("../.env") + + r := gin.Default() + r.GET("/ping/", Ping) + go r.Run(":" + os.Getenv("API_PORT")) + + req, err := http.Get(fmt.Sprintf("http://localhost:%v/ping/", os.Getenv("API_PORT"))) + if err != nil { + t.Fatal(err.Error()) + } + defer req.Body.Close() + + var exceptedResp = pingFormat{} + body, _ := io.ReadAll(req.Body) + json.Unmarshal(body, &exceptedResp) + + if exceptedResp.Message != "pong" { + t.Fatalf(`/ping/ resulted in "%v" instead of "pong"`, exceptedResp.Message) + } +}