Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add CORs headers to cache hit responses #53

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,10 @@ func TestE2ETestCachingMdwWithBlockNumberParam(t *testing.T) {
expectKeysNum(t, redisClient, tc.keysNum)
containsKey(t, redisClient, expectedKey)

// verify expected CORs headers added on cache hit
require.Equal(t, "*", resp2.Header.Get("Access-Control-Allow-Origin"))
require.Equal(t, "Content-Length", resp2.Header.Get("Access-Control-Expose-Headers"))

require.JSONEq(t, string(body1), string(body2), "blocks should be the same")
})
}
Expand Down
5 changes: 5 additions & 0 deletions service/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ func createProxyRequestMiddleware(next http.Handler, config config.Config, servi

w.Header().Add(cachemdw.CacheHeaderKey, cachemdw.CacheHitHeaderValue)
w.Header().Add("Content-Type", "application/json")

// TODO: response headers should probably be cached with the original response
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Expose-Headers", "Content-Length")

_, err := w.Write(typedCachedResponse)
if err != nil {
serviceLogger.Logger.Error().Msg(fmt.Sprintf("can't write cached response: %v", err))
Expand Down
Loading