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 | 79d8348 | 2023-01-02 10:53:24 +0100 | [diff] [blame] | 35 | assert.Equal(t, w.Header().Get("Access-Control-Allow-Origin"), "null") |
| 36 | assert.Equal(t, w.Header().Get("Access-Control-Allow-Credentials"), "null") |
| 37 | assert.Equal(t, w.Header().Get("Vary"), "Origin") |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 38 | assert.Equal(t, "sueddeutsche,http://example.org", w.Body.String()) |
Akron | 906a2c5 | 2022-12-21 11:35:28 +0100 | [diff] [blame] | 39 | |
| 40 | w = httptest.NewRecorder() |
| 41 | req, _ = http.NewRequest(http.MethodGet, "/s11/s12/s14", nil) |
| 42 | |
| 43 | router.ServeHTTP(w, req) |
| 44 | assert.Equal(t, http.StatusNotFound, w.Code) |
| 45 | assert.Equal(t, "No entry found", w.Body.String()) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 46 | } |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 47 | |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 48 | func TestWidgetRoute(t *testing.T) { |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 49 | |
| 50 | router := setupRouter() |
| 51 | |
| 52 | w := httptest.NewRecorder() |
Akron | f1adbe0 | 2022-12-21 10:32:21 +0100 | [diff] [blame] | 53 | req, _ := http.NewRequest(http.MethodGet, "/", nil) |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 54 | |
| 55 | router.ServeHTTP(w, req) |
| 56 | |
| 57 | assert.Equal(t, http.StatusOK, w.Code) |
Akron | 79d8348 | 2023-01-02 10:53:24 +0100 | [diff] [blame] | 58 | assert.Equal(t, w.Header().Get("Access-Control-Allow-Origin"), "null") |
| 59 | assert.Equal(t, w.Header().Get("Access-Control-Allow-Credentials"), "null") |
| 60 | assert.Equal(t, w.Header().Get("Vary"), "Origin") |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 61 | assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de\"") |
| 62 | assert.Contains(t, w.Body.String(), "<title>External Provider</title>") |
| 63 | |
| 64 | os.Setenv("KORAP_SERVER", "https://korap.ids-mannheim.de/instance/test") |
| 65 | |
| 66 | router = setupRouter() |
| 67 | |
| 68 | w = httptest.NewRecorder() |
| 69 | req, _ = http.NewRequest(http.MethodGet, "/", nil) |
| 70 | |
| 71 | router.ServeHTTP(w, req) |
| 72 | |
| 73 | assert.Equal(t, http.StatusOK, w.Code) |
Akron | f1adbe0 | 2022-12-21 10:32:21 +0100 | [diff] [blame] | 74 | 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] | 75 | assert.Contains(t, w.Body.String(), "<title>External Provider</title>") |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 76 | } |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 77 | |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 78 | func TestManifestRoute(t *testing.T) { |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 79 | |
| 80 | router := setupRouter() |
| 81 | |
| 82 | w := httptest.NewRecorder() |
| 83 | req, _ := http.NewRequest(http.MethodGet, "/plugin.json", nil) |
| 84 | |
| 85 | router.ServeHTTP(w, req) |
| 86 | |
| 87 | assert.Equal(t, http.StatusOK, w.Code) |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 88 | assert.Contains(t, w.Header().Get("Content-Type"), "application/json") |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 89 | assert.Contains(t, w.Body.String(), "permissions") |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 90 | assert.Contains(t, w.Body.String(), "/plugin/external") |
| 91 | |
| 92 | os.Setenv("KORAP_EXTERNAL_PROVIDER", "https://korap.ids-mannheim.de/plugin/fun") |
| 93 | |
| 94 | router = setupRouter() |
| 95 | |
| 96 | w = httptest.NewRecorder() |
| 97 | req, _ = http.NewRequest(http.MethodGet, "/plugin.json", nil) |
| 98 | |
| 99 | router.ServeHTTP(w, req) |
| 100 | |
| 101 | assert.Equal(t, http.StatusOK, w.Code) |
| 102 | assert.Contains(t, w.Header().Get("Content-Type"), "application/json") |
| 103 | assert.Contains(t, w.Body.String(), "permissions") |
| 104 | assert.Contains(t, w.Body.String(), "/plugin/fun") |
| 105 | |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 106 | } |