aboutsummaryrefslogtreecommitdiffstats
path: root/web/routing_test.go
blob: a7c5c37bbf064d2baf76faadfd17c9f31a695fae (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package web

import (
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"adammathes.com/neko/config"
)

func TestRouting(t *testing.T) {
	config.Config.DigestPassword = "secret"
	router := NewRouter(&config.Config)

	tests := []struct {
		name           string
		path           string
		method         string
		cookie         *http.Cookie
		expectedStatus int
		containsBody   string
	}{
		{
			name:           "Root serves new UI",
			path:           "/",
			method:         "GET",
			expectedStatus: http.StatusOK,
			containsBody:   "<!doctype html>", // from React dist/v2
		},
		{
			name:           "/v2/ serves new UI",
			path:           "/v2/",
			method:         "GET",
			expectedStatus: http.StatusOK,
			containsBody:   "<!doctype html>",
		},
		{
			name:           "/v3/ serves v3 UI",
			path:           "/v3/",
			method:         "GET",
			expectedStatus: http.StatusOK,
			containsBody:   "<!doctype html>",
		},
		{
			name:           "/v1/ redirects unauthenticated",
			path:           "/v1/",
			method:         "GET",
			expectedStatus: http.StatusTemporaryRedirect,
		},
		{
			name:           "/v1/ serves legacy UI when authenticated",
			path:           "/v1/",
			method:         "GET",
			cookie:         authCookie(),
			expectedStatus: http.StatusOK,
			containsBody:   "<title>neko rss mode</title>", // from legacy ui.html
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			req := httptest.NewRequest(tt.method, tt.path, nil)
			if tt.cookie != nil {
				req.AddCookie(tt.cookie)
			}
			rr := httptest.NewRecorder()
			router.ServeHTTP(rr, req)

			if rr.Code != tt.expectedStatus {
				t.Errorf("Expected status %d, got %d", tt.expectedStatus, rr.Code)
			}

			if tt.containsBody != "" {
				body := strings.ToLower(rr.Body.String())
				if !strings.Contains(body, strings.ToLower(tt.containsBody)) {
					t.Errorf("Expected body to contain %q, but it didn't", tt.containsBody)
				}
			}
		})
	}
}