-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathapp_test.go
165 lines (144 loc) · 4.99 KB
/
app_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
package gnoweb
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gnolang/gno/gno.land/pkg/integration"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/tm2/pkg/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRoutes(t *testing.T) {
const (
ok = http.StatusOK
found = http.StatusFound
notFound = http.StatusNotFound
BadRequest = http.StatusBadRequest
)
routes := []struct {
route string
status int
substring string
}{
{"/", ok, "Welcome"}, // Check if / returns 200 (OK) and contains "Welcome".
{"/about", ok, "blockchain"},
{"/r/gnoland/blog", ok, ""}, // Any content
{"/r/gnoland/blog$help", ok, "AdminSetAdminAddr"},
{"/r/gnoland/blog/", ok, "admin.gno"},
{"/r/gnoland/blog/admin.gno", ok, ">func<"},
{"/r/gnoland/blog$help&func=Render", ok, "Render(path)"},
{"/r/gnoland/blog$help&func=Render&path=foo/bar", ok, `value="foo/bar"`},
// {"/r/gnoland/blog$help&func=NonExisting", ok, "NonExisting not found"}, // XXX(TODO)
{"/r/gnoland/users/v1:archives", ok, "Address"},
{"/r/gnoland/users/v1", ok, "registry"},
{"/r/gnoland/users/v1/users.gno", ok, "reValidUsername"},
{"/r/demo/deep/very/deep", ok, "it works!"},
{"/r/demo/deep/very/deep?arg1=val1&arg2=val2", ok, "hi ?arg1=val1&arg2=val2"},
{"/r/demo/deep/very/deep:bob", ok, "hi bob"},
{"/r/demo/deep/very/deep:bob?arg1=val1&arg2=val2", ok, "hi bob?arg1=val1&arg2=val2"},
{"/r/demo/deep/very/deep$help", ok, "Render"},
{"/r/demo/deep/very/deep/", ok, "render.gno"},
{"/r/demo/deep/very/deep/render.gno", ok, ">package<"},
{"/contribute", ok, "Game of Realms"},
{"/game-of-realms", found, "/contribute"},
{"/gor", found, "/contribute"},
{"/blog", found, "/r/gnoland/blog"},
{"/r/docs/optional_render", ok, "Directory"},
{"/r/docs/optional_render/", ok, "Directory"},
{"/r/not/found/", notFound, ""},
{"/z/bad/request", BadRequest, ""}, // not realm or pure
{"/아스키문자가아닌경로", notFound, ""},
{"/%ED%85%8C%EC%8A%A4%ED%8A%B8", notFound, ""},
{"/グノー", notFound, ""},
{"/\u269B\uFE0F", notFound, ""}, // Unicode
{"/p/demo/flow/LICENSE", ok, "BSD 3-Clause"},
// Test assets
{"/public/styles.css", ok, ""},
{"/public/js/index.js", ok, ""},
{"/public/_chroma/style.css", ok, ""},
{"/public/imgs/gnoland.svg", ok, ""},
// Test Toc
{"/", ok, `href="#learn-about-gnoland"`},
}
rootdir := gnoenv.RootDir()
println(rootdir)
genesis := integration.LoadDefaultGenesisTXsFile(t, "tendermint_test", rootdir)
config, _ := integration.TestingNodeConfig(t, rootdir, genesis...)
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewTestingLogger(t), config)
defer node.Stop()
cfg := NewDefaultAppConfig()
cfg.NodeRemote = remoteAddr
logger := log.NewTestingLogger(t)
// Initialize the router with the current node's remote address
router, err := NewRouter(logger, cfg)
require.NoError(t, err)
for _, r := range routes {
t.Run(fmt.Sprintf("test route %s", r.route), func(t *testing.T) {
t.Logf("input: %q", r.route)
request := httptest.NewRequest(http.MethodGet, r.route, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
assert.Equal(t, r.status, response.Code)
assert.Contains(t, response.Body.String(), r.substring)
})
}
}
func TestAnalytics(t *testing.T) {
routes := []string{
// Special realms
"/", // Home
"/about",
"/start",
// Redirects
"/game-of-realms",
"/getting-started",
"/blog",
"/boards",
// Realm, source, help page
"/r/gnoland/blog",
"/r/gnoland/blog/admin.gno",
"/r/gnoland/users/v1",
"/r/gnoland/blog$help",
// Special pages
"/404-not-found",
}
rootdir := gnoenv.RootDir()
genesis := integration.LoadDefaultGenesisTXsFile(t, "tendermint_test", rootdir)
config, _ := integration.TestingNodeConfig(t, rootdir, genesis...)
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewTestingLogger(t), config)
defer node.Stop()
t.Run("enabled", func(t *testing.T) {
for _, route := range routes {
t.Run(route, func(t *testing.T) {
cfg := NewDefaultAppConfig()
cfg.NodeRemote = remoteAddr
cfg.Analytics = true
logger := log.NewTestingLogger(t)
router, err := NewRouter(logger, cfg)
require.NoError(t, err)
request := httptest.NewRequest(http.MethodGet, route, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
assert.Contains(t, response.Body.String(), "sa.gno.services")
})
}
})
t.Run("disabled", func(t *testing.T) {
for _, route := range routes {
t.Run(route, func(t *testing.T) {
cfg := NewDefaultAppConfig()
cfg.NodeRemote = remoteAddr
cfg.Analytics = false
logger := log.NewTestingLogger(t)
router, err := NewRouter(logger, cfg)
require.NoError(t, err)
request := httptest.NewRequest(http.MethodGet, route, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
assert.NotContains(t, response.Body.String(), "sa.gno.services")
})
}
})
}