Repository navigation
Expand file tree
/
Copy pathadapter_alias_test.go
More file actions
88 lines (67 loc) · 2.63 KB
/
Copy pathadapter_alias_test.go
File metadata and controls
88 lines (67 loc) · 2.63 KB
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
package forge_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xraph/forge"
)
// externalAdapter stands in for an adapter written outside this module. If it
// compiles, the public aliases are complete enough for a third party to
// implement the wide interface.
type externalAdapter struct{ configured bool }
func (a *externalAdapter) Handle(method, path string, h http.Handler) {}
func (a *externalAdapter) Mount(path string, h http.Handler) {}
func (a *externalAdapter) UseGlobal(m func(http.Handler) http.Handler) {}
func (a *externalAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
func (a *externalAdapter) Close() error { return nil }
func (a *externalAdapter) Capabilities() forge.Capabilities {
return forge.Capabilities{MethodNotAllowed: true, TypedParams: true}
}
func (a *externalAdapter) Configure(cfg forge.MatcherConfig) error {
a.configured = true
return nil
}
func (a *externalAdapter) HandleRoute(spec forge.RouteSpec) error { return nil }
func TestPublicAliasesAllowAnExternalExtendedAdapter(t *testing.T) {
var adapter forge.ExtendedAdapter = &externalAdapter{}
require.NotNil(t, adapter)
caps := adapter.Capabilities()
assert.True(t, caps.MethodNotAllowed)
assert.False(t, caps.Constraints)
require.NoError(t, adapter.Configure(forge.MatcherConfig{
NotFound: http.NotFoundHandler(),
}))
require.NoError(t, adapter.HandleRoute(forge.RouteSpec{
Method: http.MethodGet,
Kind: forge.KindSSE,
}))
}
func TestPublicRouteKindConstants(t *testing.T) {
assert.Equal(t, "sse", forge.KindSSE.String())
assert.True(t, forge.KindWebSocket.LongLived())
assert.False(t, forge.KindHTTP.LongLived())
}
func TestNewForgeMuxAdapter(t *testing.T) {
adapter := forge.NewForgeMuxAdapter()
require.NotNil(t, adapter)
caps := adapter.Capabilities()
assert.True(t, caps.Constraints)
assert.True(t, caps.MethodNotAllowed)
}
func TestRouterWithForgeMux(t *testing.T) {
r := forge.NewRouter(forge.WithAdapter(forge.NewForgeMuxAdapter()))
require.NoError(t, r.GET("/users/{id:int}", func(ctx forge.Context) error {
return ctx.String(http.StatusOK, ctx.Param("id"))
}))
require.NoError(t, r.GET("/users/me", func(ctx forge.Context) error {
return ctx.String(http.StatusOK, "me")
}))
for path, want := range map[string]string{"/users/42": "42", "/users/me": "me"} {
rec := httptest.NewRecorder()
r.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
require.Equalf(t, http.StatusOK, rec.Code, "path %s", path)
assert.Equal(t, want, rec.Body.String())
}
}