This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
180 lines (145 loc) Β· 3.96 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// π Fiber is an Express inspired web framework written in Go with π
// π API Documentation: https://fiber.wiki
// π Github Repository: https://github.com/gofiber/fiber
package rewrite
import (
"fmt"
"io"
"net/http"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)
func Test_New(t *testing.T) {
// Test with no config
m := New()
if m == nil {
t.Error("Expected middleware to be returned, got nil")
}
// Test with config
m = New(Config{
Rules: map[string]string{
"/old": "/new",
},
})
if m == nil {
t.Error("Expected middleware to be returned, got nil")
}
// Test with full config
m = New(Config{
Filter: func(*fiber.Ctx) bool {
return true
},
Rules: map[string]string{
"/old": "/new",
},
})
if m == nil {
t.Error("Expected middleware to be returned, got nil")
}
}
func Test_Rewrite(t *testing.T) {
// Case 1: filter function always returns true
app := fiber.New()
app.Use(New(Config{
Filter: func(*fiber.Ctx) bool {
return true
},
Rules: map[string]string{
"/old": "/new",
},
}))
app.Get("/old", func(c *fiber.Ctx) error {
return c.SendString("Rewrite Successful")
})
req, _ := http.NewRequest("GET", "/old", nil)
resp, err := app.Test(req)
body, _ := io.ReadAll(resp.Body)
bodyString := string(body)
if err != nil {
t.Error(err)
}
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, "Rewrite Successful", bodyString)
// Case 2: filter function always returns false
app = fiber.New()
app.Use(New(Config{
Filter: func(*fiber.Ctx) bool {
return false
},
Rules: map[string]string{
"/old": "/new",
},
}))
app.Get("/new", func(c *fiber.Ctx) error {
return c.SendString("Rewrite Successful")
})
req, _ = http.NewRequest("GET", "/old", nil)
resp, err = app.Test(req)
body, _ = io.ReadAll(resp.Body)
bodyString = string(body)
if err != nil {
t.Error(err)
}
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, "Rewrite Successful", bodyString)
// Case 3: check for captured tokens in rewrite rule
app = fiber.New()
app.Use(New(Config{
Rules: map[string]string{
"/users/*/orders/*": "/user/$1/order/$2",
},
}))
app.Get("/user/:userID/order/:orderID", func(c *fiber.Ctx) error {
return c.SendString(fmt.Sprintf("User ID: %s, Order ID: %s", c.Params("userID"), c.Params("orderID")))
})
req, _ = http.NewRequest("GET", "/users/123/orders/456", nil)
resp, err = app.Test(req)
body, _ = io.ReadAll(resp.Body)
bodyString = string(body)
if err != nil {
t.Error(err)
}
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, "User ID: 123, Order ID: 456", bodyString)
// Case 4: Send non-matching request, handled by default route
app = fiber.New()
app.Use(New(Config{
Rules: map[string]string{
"/users/*/orders/*": "/user/$1/order/$2",
},
}))
app.Get("/user/:userID/order/:orderID", func(c *fiber.Ctx) error {
return c.SendString(fmt.Sprintf("User ID: %s, Order ID: %s", c.Params("userID"), c.Params("orderID")))
})
app.Use(func(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
})
req, _ = http.NewRequest("GET", "/not-matching-any-rule", nil)
resp, err = app.Test(req)
body, _ = io.ReadAll(resp.Body)
bodyString = string(body)
if err != nil {
t.Error(err)
}
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, "OK", bodyString)
// Case 4: Send non-matching request, with no default route
app = fiber.New()
app.Use(New(Config{
Rules: map[string]string{
"/users/*/orders/*": "/user/$1/order/$2",
},
}))
app.Get("/user/:userID/order/:orderID", func(c *fiber.Ctx) error {
return c.SendString(fmt.Sprintf("User ID: %s, Order ID: %s", c.Params("userID"), c.Params("orderID")))
})
req, _ = http.NewRequest("GET", "/not-matching-any-rule", nil)
resp, err = app.Test(req)
body, _ = io.ReadAll(resp.Body)
bodyString = string(body)
if err != nil {
t.Error(err)
}
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
}