blob: 9101080ffc3a3f2d2253ad52eb8a363646c74ae7 [file] [log] [blame]
Akrone2161562022-12-19 17:05:39 +01001package main
2
3import (
Akrona1f100a2023-03-07 11:03:01 +01004 "compress/gzip"
5 "io"
Akrone2161562022-12-19 17:05:39 +01006 "net/http"
7 "net/http/httptest"
Akronce286af2022-12-28 13:58:26 +01008 "os"
Akrone2161562022-12-19 17:05:39 +01009 "testing"
10
11 "github.com/stretchr/testify/assert"
12)
13
Akron5e1252e2022-12-19 17:57:56 +010014func TestMappingRoute(t *testing.T) {
Akrone2161562022-12-19 17:05:39 +010015
16 dir := t.TempDir()
17
Akrona1f100a2023-03-07 11:03:01 +010018 InitDB(dir)
Akrone2161562022-12-19 17:05:39 +010019 defer closeDB()
20 router := setupRouter()
21
22 w := httptest.NewRecorder()
23 req, _ := http.NewRequest(http.MethodGet, "/s10/s10/s10", nil)
24
25 router.ServeHTTP(w, req)
26
27 assert.Equal(t, http.StatusNotFound, w.Code)
28 assert.Equal(t, "No entry found", w.Body.String())
29
Akrona1f100a2023-03-07 11:03:01 +010030 assert.Nil(t, add(db, "s11", "s12", "s13", "sueddeutsche", "http://example.org"))
Akrone2161562022-12-19 17:05:39 +010031
32 w = httptest.NewRecorder()
33 req, _ = http.NewRequest(http.MethodGet, "/s11/s12/s13", nil)
34
35 router.ServeHTTP(w, req)
36 assert.Equal(t, http.StatusOK, w.Code)
Akron79d83482023-01-02 10:53:24 +010037 assert.Equal(t, w.Header().Get("Access-Control-Allow-Origin"), "null")
38 assert.Equal(t, w.Header().Get("Access-Control-Allow-Credentials"), "null")
39 assert.Equal(t, w.Header().Get("Vary"), "Origin")
Akron6aee7fe2022-12-20 16:00:54 +010040 assert.Equal(t, "sueddeutsche,http://example.org", w.Body.String())
Akron906a2c52022-12-21 11:35:28 +010041
42 w = httptest.NewRecorder()
43 req, _ = http.NewRequest(http.MethodGet, "/s11/s12/s14", nil)
44
45 router.ServeHTTP(w, req)
46 assert.Equal(t, http.StatusNotFound, w.Code)
47 assert.Equal(t, "No entry found", w.Body.String())
Akrone2161562022-12-19 17:05:39 +010048}
Akron5e1252e2022-12-19 17:57:56 +010049
Akron60e11602023-01-02 11:16:43 +010050func TestWidgetRoute(t *testing.T) {
Akron5e1252e2022-12-19 17:57:56 +010051
52 router := setupRouter()
53
54 w := httptest.NewRecorder()
Akronf1adbe02022-12-21 10:32:21 +010055 req, _ := http.NewRequest(http.MethodGet, "/", nil)
Akron5e1252e2022-12-19 17:57:56 +010056
57 router.ServeHTTP(w, req)
58
59 assert.Equal(t, http.StatusOK, w.Code)
Akron79d83482023-01-02 10:53:24 +010060 assert.Equal(t, w.Header().Get("Access-Control-Allow-Origin"), "null")
61 assert.Equal(t, w.Header().Get("Access-Control-Allow-Credentials"), "null")
62 assert.Equal(t, w.Header().Get("Vary"), "Origin")
Akronce286af2022-12-28 13:58:26 +010063 assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de\"")
Akron83b77a72023-03-17 13:37:06 +010064 assert.Contains(t, w.Body.String(), "<title>Full Text</title>")
Akronce286af2022-12-28 13:58:26 +010065
66 os.Setenv("KORAP_SERVER", "https://korap.ids-mannheim.de/instance/test")
67
68 router = setupRouter()
69
70 w = httptest.NewRecorder()
71 req, _ = http.NewRequest(http.MethodGet, "/", nil)
72
73 router.ServeHTTP(w, req)
74
75 assert.Equal(t, http.StatusOK, w.Code)
Akronf1adbe02022-12-21 10:32:21 +010076 assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de/instance/test\"")
Akron83b77a72023-03-17 13:37:06 +010077 assert.Contains(t, w.Body.String(), "<title>Full Text</title>")
78}
79
80func TestWidgetRouteI18n(t *testing.T) {
81
82 router := setupRouter()
83
84 w := httptest.NewRecorder()
85 req, _ := http.NewRequest(http.MethodGet, "/", nil)
86 req.Header.Set("Accept-Language", "de")
87
88 router.ServeHTTP(w, req)
89
90 assert.Equal(t, http.StatusOK, w.Code)
91 assert.Contains(t, w.Body.String(), "<title>Volltext</title>")
92 assert.Contains(t, w.Body.String(), "Volltext angeboten durch")
93 assert.Contains(t, w.Body.String(), "kein externer Anbieter bekannt")
94
95 req, _ = http.NewRequest(http.MethodGet, "/", nil)
96 req.Header.Set("Accept-Language", "pe")
97
98 router.ServeHTTP(w, req)
99
100 assert.Equal(t, http.StatusOK, w.Code)
101 assert.Contains(t, w.Body.String(), "<title>Full Text</title>")
102 assert.Contains(t, w.Body.String(), "Full text provided by")
103 assert.Contains(t, w.Body.String(), "External provider unknown. No access to full text available.")
Akron5e1252e2022-12-19 17:57:56 +0100104}
Akron60e11602023-01-02 11:16:43 +0100105
Akron65503a32023-01-02 12:38:32 +0100106func TestManifestRoute(t *testing.T) {
Akron60e11602023-01-02 11:16:43 +0100107
108 router := setupRouter()
109
110 w := httptest.NewRecorder()
111 req, _ := http.NewRequest(http.MethodGet, "/plugin.json", nil)
112
113 router.ServeHTTP(w, req)
114
115 assert.Equal(t, http.StatusOK, w.Code)
Akron65503a32023-01-02 12:38:32 +0100116 assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
Akron60e11602023-01-02 11:16:43 +0100117 assert.Contains(t, w.Body.String(), "permissions")
Akron65503a32023-01-02 12:38:32 +0100118 assert.Contains(t, w.Body.String(), "/plugin/external")
119
Akronde331792023-01-10 11:36:15 +0100120 os.Setenv("KORAP_EXTERNAL_RESOURCES", "https://korap.ids-mannheim.de/plugin/fun")
Akron65503a32023-01-02 12:38:32 +0100121
122 router = setupRouter()
123
124 w = httptest.NewRecorder()
125 req, _ = http.NewRequest(http.MethodGet, "/plugin.json", nil)
126
127 router.ServeHTTP(w, req)
128
129 assert.Equal(t, http.StatusOK, w.Code)
130 assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
131 assert.Contains(t, w.Body.String(), "permissions")
132 assert.Contains(t, w.Body.String(), "/plugin/fun")
Akrona1f100a2023-03-07 11:03:01 +0100133}
134
135func TestIndexer(t *testing.T) {
136
137 dir := t.TempDir()
138
139 dbx := initDB(dir)
140 defer dbx.Close()
141
142 // Test index plain
143 file, err := os.Open("testdata/sz_mapping_example1.csv")
144 assert.Nil(t, err)
145 defer file.Close()
146 indexDB(file, dbx)
147
148 // Test index gzip
149 file, err = os.Open("testdata/sz_mapping_example2.csv.gz")
150 assert.Nil(t, err)
151 defer file.Close()
152 var gzipr io.Reader
153 gzipr, err = gzip.NewReader(file)
154 assert.Nil(t, err)
155 indexDB(gzipr, dbx)
156
157 txn := dbx.NewTransaction(true)
158 defer txn.Discard()
159
160 item, err := txn.Get([]byte("U92/JAN/00001"))
161 assert.Nil(t, err)
162 err = item.Value(func(val []byte) error {
163 assert.Equal(t, string(val), "S&uuml;ddeutsche Zeitung,https://archiv.szarchiv.de/Portal/restricted/Start.act?articleId=A800000")
164 return nil
165 })
166 assert.Nil(t, err)
167
168 item, err = txn.Get([]byte("U92/JAN/00003"))
169 assert.Nil(t, err)
170 err = item.Value(func(val []byte) error {
171 assert.Equal(t, string(val), "S&uuml;ddeutsche Zeitung,https://archiv.szarchiv.de/Portal/restricted/Start.act?articleId=A800010")
172 return nil
173 })
174 assert.Nil(t, err)
175
176 item, err = txn.Get([]byte("U92/FEB/00003"))
177 assert.Nil(t, err)
178 err = item.Value(func(val []byte) error {
179 assert.Equal(t, string(val), "S&uuml;ddeutsche Zeitung,https://archiv.szarchiv.de/Portal/restricted/Start.act?articleId=A806912")
180 return nil
181 })
182 assert.Nil(t, err)
Akron65503a32023-01-02 12:38:32 +0100183
Akron60e11602023-01-02 11:16:43 +0100184}