-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
94 lines (77 loc) · 2.07 KB
/
main_test.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/anshg1214/CacheRocket/config"
"github.com/anshg1214/CacheRocket/controller"
"github.com/anshg1214/CacheRocket/utils"
)
var router *gin.Engine
// TestMain is the entry point for all tests
func TestMain(t *testing.T) {
tests := []struct {
name string
path string
status int
cacheStatus bool
}{
{
name: "Test Get Post",
path: "/posts/1",
status: http.StatusOK,
},
{
name: "Test Get Post 404",
path: "/posts/10000",
status: http.StatusNotFound,
},
{
name: "Test Get Todo",
path: "/todos/1",
status: http.StatusOK,
},
{
name: "Test Get Todo 404",
path: "/todos/10000",
status: http.StatusNotFound,
},
}
gin.SetMode(gin.TestMode)
// Setup redis
err := config.PingRedis()
assert.NoError(t, err, "❌ Redis is not running")
// Flush redis
err = utils.FlushCache()
assert.NoError(t, err, "Error flushing cache: %v", err)
router = gin.Default()
// Routes
router.GET("/", controller.GetHome)
router.GET("/posts/:id", controller.GetPost)
router.GET("/todos/:id", controller.GetTodo)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testGet(t, test.path, test.status, test.cacheStatus)
})
}
}
func testGet(t *testing.T, path string, status int, cacheStatus bool) {
// Create a request to send to the above route
req, err := http.NewRequest("GET", path, nil)
assert.NoError(t, err, "Error creating request: %v", err)
// Create a response recorder
w := httptest.NewRecorder()
// Send the request to the router
router.ServeHTTP(w, req)
// Check the status code is what we expect
assert.Equal(t, status, w.Code, "handler returned wrong status code: got %v want %v", w.Code, status)
// Check the response body is what we expect
body, err := io.ReadAll(w.Body)
assert.NoError(t, err, "Error reading response body: %v", err)
checkValidJson := json.Valid(body)
assert.Equal(t, true, checkValidJson)
}