-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain_test.go
More file actions
37 lines (27 loc) · 736 Bytes
/
main_test.go
File metadata and controls
37 lines (27 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"net/http"
"net/http/httptest"
"testing"
"gin-gonic/pkg/hello"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestHelloEndpoint(t *testing.T) {
// Set Gin to Test Mode
gin.SetMode(gin.TestMode)
// Use the router setup from main.go
r := setupRouter()
// Create a new HTTP request
req, err := http.NewRequest(http.MethodGet, "/hello", nil)
assert.NoError(t, err)
// Create a response recorder
rr := httptest.NewRecorder()
// Perform the request
r.ServeHTTP(rr, req)
// Check the status code
assert.Equal(t, http.StatusOK, rr.Code)
// Check the response body
expected := `{"message":"` + hello.GetMessage() + `"}`
assert.JSONEq(t, expected, rr.Body.String())
}