blob: 7304ae7232d132249ec082386318053597737e41 [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)
Akron6aee7fe2022-12-20 16:00:54 +010035 assert.Equal(t, "sueddeutsche,http://example.org", w.Body.String())
Akron906a2c52022-12-21 11:35:28 +010036
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())
Akrone2161562022-12-19 17:05:39 +010043}
Akron5e1252e2022-12-19 17:57:56 +010044
45func TestAssetRoute(t *testing.T) {
46
47 router := setupRouter()
48
49 w := httptest.NewRecorder()
Akronf1adbe02022-12-21 10:32:21 +010050 req, _ := http.NewRequest(http.MethodGet, "/", nil)
Akron5e1252e2022-12-19 17:57:56 +010051
52 router.ServeHTTP(w, req)
53
54 assert.Equal(t, http.StatusOK, w.Code)
Akronce286af2022-12-28 13:58:26 +010055 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)
Akronf1adbe02022-12-21 10:32:21 +010068 assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de/instance/test\"")
Akronebdfa002022-12-20 13:48:33 +010069 assert.Contains(t, w.Body.String(), "<title>External Provider</title>")
Akron5e1252e2022-12-19 17:57:56 +010070}