feat: Add Access-Control-Allow-Origin: * response header to badges (#5395)

Signed-off-by: Markus Hinz <markus.hinz@syncier.com>
This commit is contained in:
Markus Hinz 2021-02-04 18:38:24 +01:00 committed by GitHub
parent 564a1221b3
commit 7dfc439086
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View file

@ -155,6 +155,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//Ask cache's to not cache the contents in order prevent the badge from becoming stale
w.Header().Set("Cache-Control", "private, no-store")
//Allow badges to be fetched via XHR from frontend applications without running into CORS issues
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(badge))
}

View file

@ -68,6 +68,7 @@ func TestHandlerFeatureIsEnabled(t *testing.T) {
handler.ServeHTTP(rr, req)
assert.Equal(t, "private, no-store", rr.Header().Get("Cache-Control"))
assert.Equal(t, "*", rr.Header().Get("Access-Control-Allow-Origin"))
response := rr.Body.String()
assert.Equal(t, toRGBString(Green), leftRectColorPattern.FindStringSubmatch(response)[1])
@ -116,6 +117,7 @@ func TestHandlerFeatureProjectIsEnabled(t *testing.T) {
assert.NoError(t, err)
handler.ServeHTTP(rr, req)
assert.Equal(t, "private, no-store", rr.Header().Get("Cache-Control"))
assert.Equal(t, "*", rr.Header().Get("Access-Control-Allow-Origin"))
response := rr.Body.String()
assert.Equal(t, toRGBString(tt.healthColor), leftRectColorPattern.FindStringSubmatch(response)[1])
assert.Equal(t, toRGBString(tt.statusColor), rightRectColorPattern.FindStringSubmatch(response)[1])
@ -182,6 +184,7 @@ func TestHandlerFeatureIsEnabledRevisionIsEnabled(t *testing.T) {
handler.ServeHTTP(rr, req)
assert.Equal(t, "private, no-store", rr.Header().Get("Cache-Control"))
assert.Equal(t, "*", rr.Header().Get("Access-Control-Allow-Origin"))
response := rr.Body.String()
assert.Equal(t, toRGBString(Green), leftRectColorPattern.FindStringSubmatch(response)[1])
@ -204,6 +207,7 @@ func TestHandlerRevisionIsEnabledNoOperationState(t *testing.T) {
handler.ServeHTTP(rr, req)
assert.Equal(t, "private, no-store", rr.Header().Get("Cache-Control"))
assert.Equal(t, "*", rr.Header().Get("Access-Control-Allow-Origin"))
response := rr.Body.String()
assert.Equal(t, toRGBString(Green), leftRectColorPattern.FindStringSubmatch(response)[1])
@ -243,6 +247,7 @@ func TestHandlerFeatureIsDisabled(t *testing.T) {
handler.ServeHTTP(rr, req)
assert.Equal(t, "private, no-store", rr.Header().Get("Cache-Control"))
assert.Equal(t, "*", rr.Header().Get("Access-Control-Allow-Origin"))
response := rr.Body.String()
assert.Equal(t, toRGBString(Purple), leftRectColorPattern.FindStringSubmatch(response)[1])