forked from content-services/content-sources-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
62 lines (58 loc) · 1.4 KB
/
utils_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
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestMatchedRoute(t *testing.T) {
type TestCase struct {
Name string
Given string
Expected string
}
testCases := []TestCase{
{
Name: "no matching /",
Given: "/",
Expected: "{\"message\":\"Not Found\"}\n",
},
{
Name: "matching /api/test",
Given: "/api/test",
Expected: "/api/test",
},
{
Name: "matching /api/test/:id",
Given: "/api/test/12345",
Expected: "/api/test/:id",
},
{
Name: "no matching /anything",
Given: "/api/anything",
Expected: "{\"message\":\"Not Found\"}\n",
},
{
Name: "no matching /test/",
Given: "/api/test/",
Expected: "{\"message\":\"Not Found\"}\n",
},
}
h := func(c echo.Context) error {
// The context.Path() is filled during serving the request,
// so it is not enough create the context and call to matchedRoute
match := MatchedRoute(c)
return c.String(http.StatusOK, match)
}
for _, testCase := range testCases {
t.Log(testCase.Name)
e := echo.New()
g := e.Group("/api")
g.Add(http.MethodGet, "/test", h)
g.Add(http.MethodGet, "/test/:id", h)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, testCase.Given, http.NoBody))
assert.Equal(t, testCase.Expected, rec.Body.String())
}
}