blob: 08a576b4cabbcbe20fa046873ddfd68fda052d6f [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\"")
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)
Akronf1adbe02022-12-21 10:32:21 +010074 assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de/instance/test\"")
Akronebdfa002022-12-20 13:48:33 +010075 assert.Contains(t, w.Body.String(), "<title>External Provider</title>")
Akron5e1252e2022-12-19 17:57:56 +010076}
Akron60e11602023-01-02 11:16:43 +010077
78func TestAssetRoute(t *testing.T) {
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)
88 assert.Equal(t, w.Header().Get("Content-Type"), "application/json")
89 assert.Contains(t, w.Body.String(), "permissions")
90}