internal/httpd/apibusy_test.go

e6cd75b5f28bacf51620bb531320c30fd4e66bfd
gitbay/internal/httpd/apibusy_test.go history · blame · raw

52 lines · 1609 bytes

1 symbol in this file
 1package httpd
 2
 3import (
 4	"net/http"
 5	"net/http/httptest"
 6	"strings"
 7	"testing"
 8
 9	"gitbay.org/gitbay/internal/control"
10	"gitbay.org/gitbay/internal/gitutil"
11	"gitbay.org/gitbay/internal/store"
12)
13
14// repo download turned away by a full pack limit is 503 with
15// Retry-After on both API endpoints, not a 500.
16func TestAPIBusyDownloadIs503(t *testing.T) {
17	s := busyServer(t)
18	s.apiLimit = newAPILimiter(0)
19	if err := gitutil.InitBare(control.RepoDir(s.cfg.Server.Root, "alice", "app"), "main", t.TempDir()); err != nil {
20		t.Fatal(err)
21	}
22	seed(t, s, 10)
23	alice, err := s.st.UserByUsername("alice")
24	if err != nil {
25		t.Fatal(err)
26	}
27	if err := s.st.CreateAPIToken(alice.ID, "t", store.HashToken("secret"), "full", nil, 0); err != nil {
28		t.Fatal(err)
29	}
30	check := func(what string, w *httptest.ResponseRecorder) {
31		t.Helper()
32		if w.Code != http.StatusServiceUnavailable || w.Header().Get("Retry-After") != "60" ||
33			!strings.Contains(w.Body.String(), "busy") {
34			t.Fatalf("%s: status %d, Retry-After %q, body %s", what, w.Code, w.Header().Get("Retry-After"), w.Body.String())
35		}
36		if w.Header().Get("ETag") != "" {
37			t.Fatalf("%s: a busy answer carries an ETag", what)
38		}
39	}
40
41	r := httptest.NewRequest("POST", "/api/v1/cmd", strings.NewReader(`{"argv":["repo","download","alice/app"]}`))
42	r.Header.Set("Authorization", "Bearer secret")
43	w := httptest.NewRecorder()
44	s.apiCmd(w, r)
45	check("POST", w)
46
47	r = httptest.NewRequest("GET", "/api/v1/read?argv=repo&argv=download&argv=alice/app", nil)
48	r.Header.Set("Authorization", "Bearer secret")
49	w = httptest.NewRecorder()
50	s.apiRead(w, r)
51	check("GET", w)
52}