aboutsummaryrefslogtreecommitdiffstats
path: root/modules/httpcache/httpcache_test.go
blob: 68ac892c91e7f99634900a478c15db5e454d4bd2 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package httpcache

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

	"github.com/stretchr/testify/assert"
)

type mockFileInfo struct {
}

func (m mockFileInfo) Name() string       { return "gitea.test" }
func (m mockFileInfo) Size() int64        { return int64(10) }
func (m mockFileInfo) Mode() os.FileMode  { return os.ModePerm }
func (m mockFileInfo) ModTime() time.Time { return time.Time{} }
func (m mockFileInfo) IsDir() bool        { return false }
func (m mockFileInfo) Sys() interface{}   { return nil }

func countFormalHeaders(h http.Header) (c int) {
	for k := range h {
		// ignore our headers for internal usage
		if strings.HasPrefix(k, "X-Gitea-") {
			continue
		}
		c++
	}
	return c
}

func TestHandleFileETagCache(t *testing.T) {
	fi := mockFileInfo{}
	etag := `"MTBnaXRlYS50ZXN0TW9uLCAwMSBKYW4gMDAwMSAwMDowMDowMCBHTVQ="`

	t.Run("No_If-None-Match", func(t *testing.T) {
		req := &http.Request{Header: make(http.Header)}
		w := httptest.NewRecorder()

		handled := HandleFileETagCache(req, w, fi)

		assert.False(t, handled)
		assert.Equal(t, 2, countFormalHeaders(w.Header()))
		assert.Contains(t, w.Header(), "Cache-Control")
		assert.Contains(t, w.Header(), "Etag")
		assert.Equal(t, etag, w.Header().Get("Etag"))
	})
	t.Run("Wrong_If-None-Match", func(t *testing.T) {
		req := &http.Request{Header: make(http.Header)}
		w := httptest.NewRecorder()

		req.Header.Set("If-None-Match", `"wrong etag"`)

		handled := HandleFileETagCache(req, w, fi)

		assert.False(t, handled)
		assert.Equal(t, 2, countFormalHeaders(w.Header()))
		assert.Contains(t, w.Header(), "Cache-Control")
		assert.Contains(t, w.Header(), "Etag")
		assert.Equal(t, etag, w.Header().Get("Etag"))
	})
	t.Run("Correct_If-None-Match", func(t *testing.T) {
		req := &http.Request{Header: make(http.Header)}
		w := httptest.NewRecorder()

		req.Header.Set("If-None-Match", etag)

		handled := HandleFileETagCache(req, w, fi)

		assert.True(t, handled)
		assert.Equal(t, 1, countFormalHeaders(w.Header()))
		assert.Contains(t, w.Header(), "Etag")
		assert.Equal(t, etag, w.Header().Get("Etag"))
		assert.Equal(t, http.StatusNotModified, w.Code)
	})
}

func TestHandleGenericETagCache(t *testing.T) {
	etag := `"test"`

	t.Run("No_If-None-Match", func(t *testing.T) {
		req := &http.Request{Header: make(http.Header)}
		w := httptest.NewRecorder()

		handled := HandleGenericETagCache(req, w, etag)

		assert.False(t, handled)
		assert.Equal(t, 2, countFormalHeaders(w.Header()))
		assert.Contains(t, w.Header(), "Cache-Control")
		assert.Contains(t, w.Header(), "Etag")
		assert.Equal(t, etag, w.Header().Get("Etag"))
	})
	t.Run("Wrong_If-None-Match", func(t *testing.T) {
		req := &http.Request{Header: make(http.Header)}
		w := httptest.NewRecorder()

		req.Header.Set("If-None-Match", `"wrong etag"`)

		handled := HandleGenericETagCache(req, w, etag)

		assert.False(t, handled)
		assert.Equal(t, 2, countFormalHeaders(w.Header()))
		assert.Contains(t, w.Header(), "Cache-Control")
		assert.Contains(t, w.Header(), "Etag")
		assert.Equal(t, etag, w.Header().Get("Etag"))
	})
	t.Run("Correct_If-None-Match", func(t *testing.T) {
		req := &http.Request{Header: make(http.Header)}
		w := httptest.NewRecorder()

		req.Header.Set("If-None-Match", etag)

		handled := HandleGenericETagCache(req, w, etag)

		assert.True(t, handled)
		assert.Equal(t, 1, countFormalHeaders(w.Header()))
		assert.Contains(t, w.Header(), "Etag")
		assert.Equal(t, etag, w.Header().Get("Etag"))
		assert.Equal(t, http.StatusNotModified, w.Code)
	})
	t.Run("Multiple_Wrong_If-None-Match", func(t *testing.T) {
		req := &http.Request{Header: make(http.Header)}
		w := httptest.NewRecorder()

		req.Header.Set("If-None-Match", `"wrong etag", "wrong etag "`)

		handled := HandleGenericETagCache(req, w, etag)

		assert.False(t, handled)
		assert.Equal(t, 2, countFormalHeaders(w.Header()))
		assert.Contains(t, w.Header(), "Cache-Control")
		assert.Contains(t, w.Header(), "Etag")
		assert.Equal(t, etag, w.Header().Get("Etag"))
	})
	t.Run("Multiple_Correct_If-None-Match", func(t *testing.T) {
		req := &http.Request{Header: make(http.Header)}
		w := httptest.NewRecorder()

		req.Header.Set("If-None-Match", `"wrong etag", `+etag)

		handled := HandleGenericETagCache(req, w, etag)

		assert.True(t, handled)
		assert.Equal(t, 1, countFormalHeaders(w.Header()))
		assert.Contains(t, w.Header(), "Etag")
		assert.Equal(t, etag, w.Header().Get("Etag"))
		assert.Equal(t, http.StatusNotModified, w.Code)
	})
}