Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit 837b982

Browse files
committed
Vary by *
1 parent 6df2914 commit 837b982

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingContext.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,39 @@ internal string CreateCacheKey(CachedVaryBy varyBy)
178178
}
179179
}
180180

181-
// Vary by params
181+
// Vary by query params
182182
if (varyBy?.Params.Count > 0)
183183
{
184-
// TODO: resolve key format and delimiters
185-
foreach (var param in varyBy.Params)
184+
if (varyBy.Params.Count == 1 && string.Equals(varyBy.Params[0], "*", StringComparison.OrdinalIgnoreCase))
186185
{
187-
// TODO: Normalization of order, case?
188-
var value = _httpContext.Request.Query[param];
189-
190-
// TODO: How to handle null/empty string?
191-
if (StringValues.IsNullOrEmpty(value))
186+
// Vary by all available query params
187+
foreach (var query in _httpContext.Request.Query)
192188
{
193-
value = "null";
189+
builder.Append(";")
190+
.Append(query.Key)
191+
.Append("=")
192+
.Append(query.Value);
193+
}
194+
}
195+
else
196+
{
197+
// TODO: resolve key format and delimiters
198+
foreach (var param in varyBy.Params)
199+
{
200+
// TODO: Normalization of order, case?
201+
var value = _httpContext.Request.Query[param];
202+
203+
// TODO: How to handle null/empty string?
204+
if (StringValues.IsNullOrEmpty(value))
205+
{
206+
value = "null";
207+
}
208+
209+
builder.Append(";")
210+
.Append(param)
211+
.Append("=")
212+
.Append(value);
194213
}
195-
196-
builder.Append(";")
197-
.Append(param)
198-
.Append("=")
199-
.Append(value);
200214
}
201215
}
202216

test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingContextTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ public void CreateCacheKey_Includes_ListedVaryByParamsOnly()
205205
}));
206206
}
207207

208+
[Fact]
209+
public void CreateCacheKey_Includes_AllQueryParamsGivenAsterisk()
210+
{
211+
var httpContext = new DefaultHttpContext();
212+
httpContext.Request.Method = "GET";
213+
httpContext.Request.Path = "/";
214+
httpContext.Request.QueryString = new QueryString("?ParamA=ValueA&ParamB=ValueB");
215+
var context = CreateTestContext(httpContext);
216+
217+
Assert.Equal("GET;/;ParamA=ValueA;ParamB=ValueB", context.CreateCacheKey(new CachedVaryBy()
218+
{
219+
Params = new string[] { "*" }
220+
}));
221+
}
222+
208223
[Fact]
209224
public void CreateCacheKey_Includes_ListedVaryByHeadersAndParams()
210225
{

0 commit comments

Comments
 (0)