Add cache-control HTTP header to badge response (#2328)

Since we serve the badge as an image using HTTP GET, cache systems
(incl. GitHub's CDN - Fastly) like to cache the image thus the
badge becomes stale rendering it useless. Adding the appropriate
Cache-Control HTTP header we direct cache systems and web browsers
not to cache the contents of the response.
This commit is contained in:
Gregor Krmelj 2019-09-19 00:48:37 +02:00 committed by Alexander Matyushentsev
parent 5ef5ebcf19
commit 70a97c0db8
2 changed files with 7 additions and 0 deletions

View file

@ -121,6 +121,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
badge = replaceFirstGroupSubMatch(rightText1Pattern, badge, rightText)
badge = replaceFirstGroupSubMatch(rightText2Pattern, badge, rightText)
w.Header().Set("Content-Type", "image/svg+xml")
//Ask cache's to not cache the contents in order prevent the badge from becoming stale
w.Header().Set("Cache-Control", "private, no-store")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(badge))
}

View file

@ -54,6 +54,8 @@ func TestHandlerFeatureIsEnabled(t *testing.T) {
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, "private, no-store", rr.Header().Get("Cache-Control"))
response := rr.Body.String()
assert.Equal(t, success, leftPathColorPattern.FindStringSubmatch(response)[1])
assert.Equal(t, success, rightPathColorPattern.FindStringSubmatch(response)[1])
@ -74,6 +76,8 @@ func TestHandlerFeatureIsDisabled(t *testing.T) {
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, "private, no-store", rr.Header().Get("Cache-Control"))
response := rr.Body.String()
assert.Equal(t, unknown, leftPathColorPattern.FindStringSubmatch(response)[1])
assert.Equal(t, unknown, rightPathColorPattern.FindStringSubmatch(response)[1])