1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package test

import (
	"net/http"
	"net/http/httptest"
	"rss_parrot/server"
	"rss_parrot/shared"
	"rss_parrot/texts"
	"strings"
	"testing"
)

// The web pages and their assets are compiled into the binary, so they are
// served whatever the working directory is; the test runs from a temporary
// one that holds no www/.
func TestWebServedFromAnyWorkingDirectory(t *testing.T) {
	t.Chdir(t.TempDir())

	cfg := &shared.Config{Host: "kakapo.example.org", Birb: &shared.UserInfo{User: "birb"}}
	logger := newTestLogger()
	repo := newTestRepo(t, cfg)
	web := server.NewWebHandlerGroup(cfg, logger, repo, texts.NewTexts())
	router := server.NewMux([]server.IHandlerGroup{web}, logger)

	for _, tc := range []struct {
		path, contains string
		status         int
	}{
		{"/", "kakapo.example.org", http.StatusOK},
		{"/web/about", "<html", http.StatusOK},
		{"/assets/style.css", "@font-face", http.StatusOK},
		{"/favicon.ico", "", http.StatusOK},
		{"/index.tmpl", "", http.StatusNotFound},
		{"/version.txt", "", http.StatusNotFound},
		{"/assets/nope.css", "", http.StatusNotFound},
	} {
		rec := httptest.NewRecorder()
		router.ServeHTTP(rec, httptest.NewRequest("GET", tc.path, nil))
		if rec.Code != tc.status {
			t.Errorf("GET %s: status %d, want %d", tc.path, rec.Code, tc.status)
			continue
		}
		if !strings.Contains(rec.Body.String(), tc.contains) {
			t.Errorf("GET %s: body does not contain %q", tc.path, tc.contains)
		}
	}
}