Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 6 | "os" |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | ) |
| 11 | |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 12 | func TestMappingRoute(t *testing.T) { |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 13 | |
| 14 | dir := t.TempDir() |
| 15 | |
| 16 | initDB(dir) |
| 17 | defer closeDB() |
| 18 | router := setupRouter() |
| 19 | |
| 20 | w := httptest.NewRecorder() |
| 21 | req, _ := http.NewRequest(http.MethodGet, "/s10/s10/s10", nil) |
| 22 | |
| 23 | router.ServeHTTP(w, req) |
| 24 | |
| 25 | assert.Equal(t, http.StatusNotFound, w.Code) |
| 26 | assert.Equal(t, "No entry found", w.Body.String()) |
| 27 | |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 28 | assert.Nil(t, add("s11", "s12", "s13", "sueddeutsche", "http://example.org")) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 29 | |
| 30 | w = httptest.NewRecorder() |
| 31 | req, _ = http.NewRequest(http.MethodGet, "/s11/s12/s13", nil) |
| 32 | |
| 33 | router.ServeHTTP(w, req) |
| 34 | assert.Equal(t, http.StatusOK, w.Code) |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 35 | assert.Equal(t, "sueddeutsche,http://example.org", w.Body.String()) |
Akron | 906a2c5 | 2022-12-21 11:35:28 +0100 | [diff] [blame] | 36 | |
| 37 | w = httptest.NewRecorder() |
| 38 | req, _ = http.NewRequest(http.MethodGet, "/s11/s12/s14", nil) |
| 39 | |
| 40 | router.ServeHTTP(w, req) |
| 41 | assert.Equal(t, http.StatusNotFound, w.Code) |
| 42 | assert.Equal(t, "No entry found", w.Body.String()) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 43 | } |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 44 | |
| 45 | func TestAssetRoute(t *testing.T) { |
| 46 | |
| 47 | router := setupRouter() |
| 48 | |
| 49 | w := httptest.NewRecorder() |
Akron | f1adbe0 | 2022-12-21 10:32:21 +0100 | [diff] [blame] | 50 | req, _ := http.NewRequest(http.MethodGet, "/", nil) |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 51 | |
| 52 | router.ServeHTTP(w, req) |
| 53 | |
| 54 | assert.Equal(t, http.StatusOK, w.Code) |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 55 | assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de\"") |
| 56 | assert.Contains(t, w.Body.String(), "<title>External Provider</title>") |
| 57 | |
| 58 | os.Setenv("KORAP_SERVER", "https://korap.ids-mannheim.de/instance/test") |
| 59 | |
| 60 | router = setupRouter() |
| 61 | |
| 62 | w = httptest.NewRecorder() |
| 63 | req, _ = http.NewRequest(http.MethodGet, "/", nil) |
| 64 | |
| 65 | router.ServeHTTP(w, req) |
| 66 | |
| 67 | assert.Equal(t, http.StatusOK, w.Code) |
Akron | f1adbe0 | 2022-12-21 10:32:21 +0100 | [diff] [blame] | 68 | assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de/instance/test\"") |
Akron | ebdfa00 | 2022-12-20 13:48:33 +0100 | [diff] [blame] | 69 | assert.Contains(t, w.Body.String(), "<title>External Provider</title>") |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 70 | } |