Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Akron | a1f100a | 2023-03-07 11:03:01 +0100 | [diff] [blame] | 4 | "compress/gzip" |
| 5 | "io" |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 6 | "net/http" |
| 7 | "net/http/httptest" |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 8 | "os" |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 9 | "testing" |
| 10 | |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | ) |
| 13 | |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 14 | func TestMappingRoute(t *testing.T) { |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 15 | |
| 16 | dir := t.TempDir() |
| 17 | |
Akron | a1f100a | 2023-03-07 11:03:01 +0100 | [diff] [blame] | 18 | InitDB(dir) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 19 | 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 | |
Akron | a1f100a | 2023-03-07 11:03:01 +0100 | [diff] [blame] | 30 | assert.Nil(t, add(db, "s11", "s12", "s13", "sueddeutsche", "http://example.org")) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 31 | |
| 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) |
Akron | 79d8348 | 2023-01-02 10:53:24 +0100 | [diff] [blame] | 37 | 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") |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 40 | assert.Equal(t, "sueddeutsche,http://example.org", w.Body.String()) |
Akron | 906a2c5 | 2022-12-21 11:35:28 +0100 | [diff] [blame] | 41 | |
| 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()) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 48 | } |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 49 | |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 50 | func TestWidgetRoute(t *testing.T) { |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 51 | |
| 52 | router := setupRouter() |
| 53 | |
| 54 | w := httptest.NewRecorder() |
Akron | f1adbe0 | 2022-12-21 10:32:21 +0100 | [diff] [blame] | 55 | req, _ := http.NewRequest(http.MethodGet, "/", nil) |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 56 | |
| 57 | router.ServeHTTP(w, req) |
| 58 | |
| 59 | assert.Equal(t, http.StatusOK, w.Code) |
Akron | 79d8348 | 2023-01-02 10:53:24 +0100 | [diff] [blame] | 60 | 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") |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 63 | assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de\"") |
Akron | 83b77a7 | 2023-03-17 13:37:06 +0100 | [diff] [blame] | 64 | assert.Contains(t, w.Body.String(), "<title>Full Text</title>") |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 65 | |
| 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) |
Akron | f1adbe0 | 2022-12-21 10:32:21 +0100 | [diff] [blame] | 76 | assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de/instance/test\"") |
Akron | 83b77a7 | 2023-03-17 13:37:06 +0100 | [diff] [blame] | 77 | assert.Contains(t, w.Body.String(), "<title>Full Text</title>") |
| 78 | } |
| 79 | |
| 80 | func 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.") |
Akron | 6c16c64 | 2023-03-24 11:52:11 +0100 | [diff] [blame^] | 104 | |
| 105 | req, _ = http.NewRequest(http.MethodGet, "/", nil) |
| 106 | req.Header.Set("Accept-Language", "de-DE;q=0.8,en-US;q=0.5,en;q=0.3") |
| 107 | |
| 108 | router.ServeHTTP(w, req) |
| 109 | |
| 110 | assert.Equal(t, http.StatusOK, w.Code) |
| 111 | assert.Contains(t, w.Body.String(), "<title>Volltext</title>") |
| 112 | assert.Contains(t, w.Body.String(), "Volltext angeboten durch") |
| 113 | assert.Contains(t, w.Body.String(), "kein externer Anbieter bekannt") |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 114 | } |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 115 | |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 116 | func TestManifestRoute(t *testing.T) { |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 117 | |
| 118 | router := setupRouter() |
| 119 | |
| 120 | w := httptest.NewRecorder() |
| 121 | req, _ := http.NewRequest(http.MethodGet, "/plugin.json", nil) |
| 122 | |
| 123 | router.ServeHTTP(w, req) |
| 124 | |
| 125 | assert.Equal(t, http.StatusOK, w.Code) |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 126 | assert.Contains(t, w.Header().Get("Content-Type"), "application/json") |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 127 | assert.Contains(t, w.Body.String(), "permissions") |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 128 | assert.Contains(t, w.Body.String(), "/plugin/external") |
| 129 | |
Akron | de33179 | 2023-01-10 11:36:15 +0100 | [diff] [blame] | 130 | os.Setenv("KORAP_EXTERNAL_RESOURCES", "https://korap.ids-mannheim.de/plugin/fun") |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 131 | |
| 132 | router = setupRouter() |
| 133 | |
| 134 | w = httptest.NewRecorder() |
| 135 | req, _ = http.NewRequest(http.MethodGet, "/plugin.json", nil) |
| 136 | |
| 137 | router.ServeHTTP(w, req) |
| 138 | |
| 139 | assert.Equal(t, http.StatusOK, w.Code) |
| 140 | assert.Contains(t, w.Header().Get("Content-Type"), "application/json") |
| 141 | assert.Contains(t, w.Body.String(), "permissions") |
| 142 | assert.Contains(t, w.Body.String(), "/plugin/fun") |
Akron | a1f100a | 2023-03-07 11:03:01 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | func TestIndexer(t *testing.T) { |
| 146 | |
| 147 | dir := t.TempDir() |
| 148 | |
| 149 | dbx := initDB(dir) |
| 150 | defer dbx.Close() |
| 151 | |
| 152 | // Test index plain |
| 153 | file, err := os.Open("testdata/sz_mapping_example1.csv") |
| 154 | assert.Nil(t, err) |
| 155 | defer file.Close() |
| 156 | indexDB(file, dbx) |
| 157 | |
| 158 | // Test index gzip |
| 159 | file, err = os.Open("testdata/sz_mapping_example2.csv.gz") |
| 160 | assert.Nil(t, err) |
| 161 | defer file.Close() |
| 162 | var gzipr io.Reader |
| 163 | gzipr, err = gzip.NewReader(file) |
| 164 | assert.Nil(t, err) |
| 165 | indexDB(gzipr, dbx) |
| 166 | |
| 167 | txn := dbx.NewTransaction(true) |
| 168 | defer txn.Discard() |
| 169 | |
| 170 | item, err := txn.Get([]byte("U92/JAN/00001")) |
| 171 | assert.Nil(t, err) |
| 172 | err = item.Value(func(val []byte) error { |
| 173 | assert.Equal(t, string(val), "Süddeutsche Zeitung,https://archiv.szarchiv.de/Portal/restricted/Start.act?articleId=A800000") |
| 174 | return nil |
| 175 | }) |
| 176 | assert.Nil(t, err) |
| 177 | |
| 178 | item, err = txn.Get([]byte("U92/JAN/00003")) |
| 179 | assert.Nil(t, err) |
| 180 | err = item.Value(func(val []byte) error { |
| 181 | assert.Equal(t, string(val), "Süddeutsche Zeitung,https://archiv.szarchiv.de/Portal/restricted/Start.act?articleId=A800010") |
| 182 | return nil |
| 183 | }) |
| 184 | assert.Nil(t, err) |
| 185 | |
| 186 | item, err = txn.Get([]byte("U92/FEB/00003")) |
| 187 | assert.Nil(t, err) |
| 188 | err = item.Value(func(val []byte) error { |
| 189 | assert.Equal(t, string(val), "Süddeutsche Zeitung,https://archiv.szarchiv.de/Portal/restricted/Start.act?articleId=A806912") |
| 190 | return nil |
| 191 | }) |
| 192 | assert.Nil(t, err) |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 193 | |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 194 | } |