summaryrefslogtreecommitdiff
path: root/lexsurf_test.go
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2025-04-11 16:33:59 +0200
committerMartin Fischer <martin@push-f.com>2025-04-13 20:19:48 +0200
commit50ea018252ce69542eab6a107b99ea8179810d1e (patch)
tree25091624110c413bd095581f79b50cd7ab030656 /lexsurf_test.go
parent16b01fd1dfb8797b5529ea444f5b26cd26be9c3c (diff)
refactor: introduce lex-serve package
Diffstat (limited to 'lexsurf_test.go')
-rw-r--r--lexsurf_test.go144
1 files changed, 0 insertions, 144 deletions
diff --git a/lexsurf_test.go b/lexsurf_test.go
deleted file mode 100644
index e647a2f..0000000
--- a/lexsurf_test.go
+++ /dev/null
@@ -1,144 +0,0 @@
-package main
-
-import (
- "net/http/httptest"
- "strings"
- "testing"
-
- "github.com/peter-evans/patience"
-)
-
-func newHandler() handler {
- return handler{
- domain: "lex.example",
- countries: map[string]country{
- "zu": country{
- Name: "Zubrowka",
- SearchURL: "https://lex.gov.zu/?search=%s",
- },
- },
- lawsByCC: map[string]map[string]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&nbsp;national&nbsp;law.</h2>
- <div class=countries>
- <a class=cc-link href="//zu.lex.example" title="Zubrowka">ZU</a>
- </div>
-</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]law{}
- h.lawsByCC["zu"]["zepl"] = 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)
- }
-}