internal/httpd/rawcache_test.go

545ba0bcdea3b534903bd78c918c0c025a97eda7
gitbay/internal/httpd/rawcache_test.go

61 lines · 1933 bytes

1 symbol in this file
 1package httpd
 2
 3import (
 4	"testing"
 5
 6	"gitbay.org/gitbay/internal/control"
 7	"gitbay.org/gitbay/internal/gitutil"
 8)
 9
10// Raw content carries its blob id as an ETag: a branch is revalidated and
11// answered with a 304 while the blob is unchanged, a commit sha is
12// immutable, and a different blob gets a different tag.
13func TestRawCaching(t *testing.T) {
14	s := symbolServer(t)
15	h := s.Handler()
16
17	w := get(t, h, "/alice/app/raw/main/main.go", nil)
18	if w.Code != 200 || w.Body.Len() == 0 {
19		t.Fatalf("raw: %d, %d bytes", w.Code, w.Body.Len())
20	}
21	etag := w.Header().Get("ETag")
22	if etag == "" {
23		t.Fatal("no ETag")
24	}
25	if got := w.Header().Get("Cache-Control"); got != "public, no-cache" {
26		t.Errorf("branch Cache-Control = %q", got)
27	}
28
29	w = get(t, h, "/alice/app/raw/main/main.go", map[string]string{"If-None-Match": etag})
30	if w.Code != 304 || w.Body.Len() != 0 {
31		t.Errorf("revalidation: %d, %d bytes", w.Code, w.Body.Len())
32	}
33	w = get(t, h, "/alice/app/raw/main/main.go", map[string]string{"If-None-Match": `W/"0000", ` + etag})
34	if w.Code != 304 {
35		t.Errorf("weak list revalidation: %d", w.Code)
36	}
37
38	sha, err := gitutil.ResolveRef(control.RepoDir(s.cfg.Server.Root, "alice", "app"), "main")
39	if err != nil {
40		t.Fatal(err)
41	}
42	w = get(t, h, "/alice/app/raw/"+sha+"/main.go", nil)
43	if w.Code != 200 {
44		t.Fatalf("raw at sha: %d", w.Code)
45	}
46	if got := w.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" {
47		t.Errorf("sha Cache-Control = %q", got)
48	}
49	if w.Header().Get("ETag") != etag {
50		t.Errorf("same blob, different ETag: %q vs %q", w.Header().Get("ETag"), etag)
51	}
52
53	w = get(t, h, "/alice/app/raw/main/a/dup.go", map[string]string{"If-None-Match": etag})
54	if w.Code != 200 || w.Header().Get("ETag") == etag {
55		t.Errorf("another blob: %d, ETag %q", w.Code, w.Header().Get("ETag"))
56	}
57
58	if w := get(t, h, "/alice/app/raw/main/a", nil); w.Code != 404 {
59		t.Errorf("a tree path: %d", w.Code)
60	}
61}