diff --git a/router-tests/header_propagation_test.go b/router-tests/header_propagation_test.go index 1a4510159d..ffb7b1765a 100644 --- a/router-tests/header_propagation_test.go +++ b/router-tests/header_propagation_test.go @@ -556,8 +556,9 @@ func TestHeaderPropagation(t *testing.T) { res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{ Query: queryEmployeeWithNoHobby, }) - cc := res.Response.Header.Get("Cache-Control") - require.Equal(t, "", cc) + val, present := res.Response.Header["Cache-Control"] + require.False(t, present) + require.Equal(t, []string(nil), val) require.Equal(t, `{"data":{"employee":{"id":1}}}`, res.Body) }) }) @@ -582,6 +583,26 @@ func TestHeaderPropagation(t *testing.T) { }) }) + t.Run("doesn't set cache control on unrelated requests", func(t *testing.T) { + t.Parallel() + testenv.Run(t, &testenv.Config{ + CacheControlPolicy: config.CacheControlPolicy{ + Subgraphs: []config.SubgraphCacheControlRule{ + {Name: "employees"}, + {Name: "hobbies"}, + }, + }, + Subgraphs: cacheOptions("", ""), + }, func(t *testing.T, xEnv *testenv.Environment) { + res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{ + Query: queryEmployeeWithHobby, + }) + val, present := res.Response.Header["Cache-Control"] + require.False(t, present) + require.Equal(t, []string(nil), val) + require.Equal(t, `{"data":{"employee":{"id":1,"hobbies":[{},{"name":"Counter Strike"},{},{},{}]}}}`, res.Body) + }) + }) t.Run("set operation can override cache control policies", func(t *testing.T) { t.Run("global set operation", func(t *testing.T) { t.Parallel() diff --git a/router/core/header_rule_engine.go b/router/core/header_rule_engine.go index 8ab43ed030..ca7f5b839b 100644 --- a/router/core/header_rule_engine.go +++ b/router/core/header_rule_engine.go @@ -452,9 +452,12 @@ func (h *HeaderPropagation) applyResponseRuleMostRestrictiveCacheControl(res *ht return } - reqDir, _ := cachedirective.ParseRequestCacheControl(res.Request.Header.Get(cacheControlKey)) - resDir, _ := cachedirective.ParseResponseCacheControl(res.Header.Get(cacheControlKey)) - expiresHeader, _ := http.ParseTime(res.Header.Get("Expires")) + reqCacheHeader := res.Request.Header.Get(cacheControlKey) + reqDir, _ := cachedirective.ParseRequestCacheControl(reqCacheHeader) + resCacheHeader := res.Header.Get(cacheControlKey) + resDir, _ := cachedirective.ParseResponseCacheControl(resCacheHeader) + expiresHeaderVal := res.Header.Get("Expires") + expiresHeader, _ := http.ParseTime(expiresHeaderVal) dateHeader, _ := http.ParseTime(res.Header.Get("Date")) lastModifiedHeader, _ := http.ParseTime(res.Header.Get("Last-Modified")) @@ -495,6 +498,9 @@ func (h *HeaderPropagation) applyResponseRuleMostRestrictiveCacheControl(res *ht if rule.Default != "" { propagation.previousCacheControl = defaultCacheControlObj propagation.header.Set(cacheControlKey, rule.Default) + } else if reqCacheHeader == "" && resCacheHeader == "" && expiresHeaderVal == "" { + // There is no default/previous value to set, and since no cache control headers have been set, exit early + return } else { propagation.previousCacheControl = obj propagation.header.Set(cacheControlKey, res.Header.Get(cacheControlKey))