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
|
package main
import (
"log/slog"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/peter-evans/patience"
"push-f.com/lex-surf/internal/lex"
)
func newHandler() handler {
return handler{
logger: slog.New(slog.NewTextHandler(os.Stderr, nil)),
domain: "lex.example",
countries: map[string]country{
"zu": country{
Name: "Zubrowka",
SearchURL: "https://lex.gov.zu/?search=%s",
},
},
lawsByCC: map[string]map[string]lex.Law{},
}
}
func TestStartPage(t *testing.T) {
h := newHandler()
req := httptest.NewRequest("GET", "/", nil)
req.Host = "lex.example"
w := httptest.NewRecorder()
h.handle(w, req)
resp := w.Result()
if resp.StatusCode != 200 {
t.Errorf("expected status 200 OK, got %d", resp.StatusCode)
}
expected := `<!doctype html>
<html>
<head>
<title>Lex.surf: Portal to National Law</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<link rel="stylesheet" href="/assets/style.css" />
</head>
<body>
<img alt="lex.surf" height=140 class=logo src="/assets/logo.svg">
<h2>The portal to national law.</h2>
<div>1st tier (autocompletion and short links)</div>
<div class=countries>
</div>
<div>2nd tier (search form)</div>
<div class=countries>
<a class=cc-link href="//zu.lex.example" title="Zubrowka">ZU</a>
</div>
<div>3rd tier (just a link)</div>
<div class=countries>
</div>
<p><a href="https://git.push-f.com/lex-surf">source code</a></p>
</body>
</html>
`
assertEqual(t, w.Body.String(), expected)
}
func TestSearch(t *testing.T) {
h := newHandler()
req := httptest.NewRequest("GET", "/", nil)
req.Host = "zu.lex.example"
w := httptest.NewRecorder()
h.handle(w, req)
resp := w.Result()
if resp.StatusCode != 200 {
t.Errorf("expected status 200 OK, got %d", resp.StatusCode)
}
expected := `<!doctype html>
<html>
<head>
<title>National Law of Zubrowka</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<link rel="stylesheet" href="//lex.example/assets/style.css" />
</head>
<body>
<a href="//lex.example"><img alt="lex.surf" height=140 class=logo src="//lex.example/assets/logo.svg"></a>
<h1>National Law of Zubrowka</h1>
<form>
<input id=search name=q aria-label="Search" autocomplete="off" autofocus >
<ul id=suggestions></ul>
</form>
<script src="//lex.example/assets/script.js"></script>
</body>
</html>
`
assertEqual(t, w.Body.String(), expected)
}
func TestSearchRedirect(t *testing.T) {
h := newHandler()
req := httptest.NewRequest("GET", "/?q=tourism", nil)
req.Host = "zu.lex.example"
w := httptest.NewRecorder()
h.handle(w, req)
resp := w.Result()
if resp.StatusCode != 302 {
t.Errorf("expected status 302, got %d", resp.StatusCode)
}
if resp.Header.Get("Location") != "https://lex.gov.zu/?search=tourism" {
t.Errorf("wrong location, got %s", resp.Header.Get("Location"))
}
}
func TestLawRedirect(t *testing.T) {
h := newHandler()
h.lawsByCC["zu"] = map[string]lex.Law{}
h.lawsByCC["zu"]["zepl"] = lex.Law{
URL: "https://lex.gov.zu/zeppelin-code",
}
req := httptest.NewRequest("GET", "/zepl", nil)
req.Host = "zu.lex.example"
w := httptest.NewRecorder()
h.handle(w, req)
resp := w.Result()
if resp.StatusCode != 302 {
t.Errorf("expected status 302, got %d", resp.StatusCode)
}
if resp.Header.Get("Location") != "https://lex.gov.zu/zeppelin-code" {
t.Errorf("wrong location, got %s", resp.Header.Get("Location"))
}
}
func assertEqual(t *testing.T, received string, expected string) {
if received != expected {
a := strings.Split(received, "\n")
b := strings.Split(expected, "\n")
diffs := patience.Diff(a, b)
unidiff := patience.UnifiedDiffText(diffs)
t.Error(unidiff)
}
}
|