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

Commit efdcb34

Browse files
committed
Vary by *
1 parent 6df2914 commit efdcb34

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingContext.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using System.Globalization;
7+
using System.Linq;
78
using System.Text;
89
using System.Threading.Tasks;
910
using Microsoft.AspNetCore.Http;
@@ -178,25 +179,39 @@ internal string CreateCacheKey(CachedVaryBy varyBy)
178179
}
179180
}
180181

181-
// Vary by params
182+
// Vary by query params
182183
if (varyBy?.Params.Count > 0)
183184
{
184-
// TODO: resolve key format and delimiters
185-
foreach (var param in varyBy.Params)
185+
if (varyBy.Params.Count == 1 && string.Equals(varyBy.Params[0], "*", StringComparison.OrdinalIgnoreCase))
186186
{
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))
187+
// Vary by all available query params
188+
foreach (var query in _httpContext.Request.Query.OrderBy(q => q.Key))
192189
{
193-
value = "null";
190+
builder.Append(";")
191+
.Append(query.Key)
192+
.Append("=")
193+
.Append(query.Value);
194+
}
195+
}
196+
else
197+
{
198+
// TODO: resolve key format and delimiters
199+
foreach (var param in varyBy.Params)
200+
{
201+
// TODO: Normalization of order, case?
202+
var value = _httpContext.Request.Query[param];
203+
204+
// TODO: How to handle null/empty string?
205+
if (StringValues.IsNullOrEmpty(value))
206+
{
207+
value = "null";
208+
}
209+
210+
builder.Append(";")
211+
.Append(param)
212+
.Append("=")
213+
.Append(value);
194214
}
195-
196-
builder.Append(";")
197-
.Append(param)
198-
.Append("=")
199-
.Append(value);
200215
}
201216
}
202217

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)