blob: 34e58b260fd065afdadee8fb76e818be80233974 [file] [log] [blame]
Akrone2161562022-12-19 17:05:39 +01001package main
2
3import (
4 "net/http"
5 "net/http/httptest"
Akronce286af2022-12-28 13:58:26 +01006 "os"
Akrone2161562022-12-19 17:05:39 +01007 "testing"
8
9 "github.com/stretchr/testify/assert"
10)
11
Akron5e1252e2022-12-19 17:57:56 +010012func TestMappingRoute(t *testing.T) {
Akrone2161562022-12-19 17:05:39 +010013
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
Akron6aee7fe2022-12-20 16:00:54 +010028 assert.Nil(t, add("s11", "s12", "s13", "sueddeutsche", "http://example.org"))
Akrone2161562022-12-19 17:05:39 +010029
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)
Akron79d83482023-01-02 10:53:24 +010035 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")
Akron6aee7fe2022-12-20 16:00:54 +010038 assert.Equal(t, "sueddeutsche,http://example.org", w.Body.String())
Akron906a2c52022-12-21 11:35:28 +010039
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())
Akrone2161562022-12-19 17:05:39 +010046}
Akron5e1252e2022-12-19 17:57:56 +010047
Akron60e11602023-01-02 11:16:43 +010048func TestWidgetRoute(t *testing.T) {
Akron5e1252e2022-12-19 17:57:56 +010049
50 router := setupRouter()
51
52 w := httptest.NewRecorder()
Akronf1adbe02022-12-21 10:32:21 +010053 req, _ := http.NewRequest(http.MethodGet, "/", nil)
Akron5e1252e2022-12-19 17:57:56 +010054
55 router.ServeHTTP(w, req)
56
57 assert.Equal(t, http.StatusOK, w.Code)
Akron79d83482023-01-02 10:53:24 +010058 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")
Akronce286af2022-12-28 13:58:26 +010061 assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de\"")
Akronde331792023-01-10 11:36:15 +010062 assert.Contains(t, w.Body.String(), "<title>External Resources</title>")
Akronce286af2022-12-28 13:58:26 +010063
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)
Akronf1adbe02022-12-21 10:32:21 +010074 assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de/instance/test\"")
Akronde331792023-01-10 11:36:15 +010075 assert.Contains(t, w.Body.String(), "<title>External Resources</title>")
Akron5e1252e2022-12-19 17:57:56 +010076}
Akron60e11602023-01-02 11:16:43 +010077
Akron65503a32023-01-02 12:38:32 +010078func TestManifestRoute(t *testing.T) {
Akron60e11602023-01-02 11:16:43 +010079
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)
Akron65503a32023-01-02 12:38:32 +010088 assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
Akron60e11602023-01-02 11:16:43 +010089 assert.Contains(t, w.Body.String(), "permissions")
Akron65503a32023-01-02 12:38:32 +010090 assert.Contains(t, w.Body.String(), "/plugin/external")
91
Akronde331792023-01-10 11:36:15 +010092 os.Setenv("KORAP_EXTERNAL_RESOURCES", "https://korap.ids-mannheim.de/plugin/fun")
Akron65503a32023-01-02 12:38:32 +010093
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
Akron60e11602023-01-02 11:16:43 +0100106}