Skip to content

Commit 1a1e665

Browse files
committed
Fixed review comments
1 parent ec6acb5 commit 1a1e665

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/ImageSharp.Web/Commands/PresetRequestParser.cs renamed to src/ImageSharp.Web/Commands/PresetOnlyQueryCollectionRequestParser.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using Microsoft.AspNetCore.Http;
@@ -13,35 +14,40 @@ namespace SixLabors.ImageSharp.Web.Commands
1314
/// <summary>
1415
/// Parses preset name from the request querystring and returns the commands configured for that preset.
1516
/// </summary>
16-
public class PresetRequestParser : IRequestParser
17+
public class PresetOnlyQueryCollectionRequestParser : IRequestParser
1718
{
1819
private readonly IDictionary<string, IDictionary<string, string>> presets;
1920

2021
/// <summary>
21-
/// Initializes a new instance of the <see cref="PresetRequestParser"/> class.
22+
/// Initializes a new instance of the <see cref="PresetOnlyQueryCollectionRequestParser"/> class.
2223
/// </summary>
2324
/// <param name="presetOptions">The preset options.</param>
24-
public PresetRequestParser(IOptions<PresetRequestParserOptions> presetOptions) =>
25+
public PresetOnlyQueryCollectionRequestParser(IOptions<PresetOnlyQueryCollectionRequestParserOptions> presetOptions) =>
2526
this.presets = ParsePresets(presetOptions.Value.Presets);
2627

2728
/// <inheritdoc/>
2829
public IDictionary<string, string> ParseRequestCommands(HttpContext context)
2930
{
30-
var requestedPreset = context.Request.Query["preset"].ToString();
31-
return this.presets.GetValueOrDefault(requestedPreset) ?? new Dictionary<string, string>();
31+
if (context.Request.Query.Count == 0)
32+
{
33+
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
34+
}
35+
36+
var requestedPreset = context.Request.Query["preset"][0];
37+
return this.presets.GetValueOrDefault(requestedPreset) ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
3238
}
3339

3440
private static IDictionary<string, IDictionary<string, string>> ParsePresets(
3541
IDictionary<string, string> unparsedPresets) =>
3642
unparsedPresets
3743
.Select(keyValue =>
3844
new KeyValuePair<string, IDictionary<string, string>>(keyValue.Key, ParsePreset(keyValue.Value)))
39-
.ToDictionary(keyValue => keyValue.Key, keyValue => keyValue.Value);
45+
.ToDictionary(keyValue => keyValue.Key, keyValue => keyValue.Value, StringComparer.OrdinalIgnoreCase);
4046

4147
private static IDictionary<string, string> ParsePreset(string unparsedPresetValue)
4248
{
4349
Dictionary<string, StringValues> parsed = QueryHelpers.ParseQuery(unparsedPresetValue);
44-
var transformed = new Dictionary<string, string>(parsed.Count);
50+
var transformed = new Dictionary<string, string>(parsed.Count, StringComparer.OrdinalIgnoreCase);
4551
foreach (KeyValuePair<string, StringValues> keyValue in parsed)
4652
{
4753
transformed[keyValue.Key] = keyValue.Value.ToString();

src/ImageSharp.Web/Commands/PresetRequestParserOptions.cs renamed to src/ImageSharp.Web/Commands/PresetOnlyQueryCollectionRequestParserOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
namespace SixLabors.ImageSharp.Web.Commands
77
{
88
/// <summary>
9-
/// Configuration options for the <see cref="PresetRequestParser"/>.
9+
/// Configuration options for the <see cref="PresetOnlyQueryCollectionRequestParser"/>.
1010
/// </summary>
11-
public class PresetRequestParserOptions
11+
public class PresetOnlyQueryCollectionRequestParserOptions
1212
{
1313
/// <summary>
1414
/// Gets or sets the presets, which is a Dictionary of preset names to command query strings.

tests/ImageSharp.Web.Tests/Commands/PresetRequestParserTests.cs renamed to tests/ImageSharp.Web.Tests/Commands/PresetOnlyQueryCollectionRequestParserTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
namespace SixLabors.ImageSharp.Web.Tests.Commands
1212
{
13-
public class PresetRequestParserTests
13+
public class PresetOnlyQueryCollectionRequestParserTests
1414
{
1515
[Fact]
16-
public void PresetRequestParserExtractsCommands()
16+
public void PresetOnlyQueryCollectionRequestParserExtractsCommands()
1717
{
1818
IDictionary<string, string> expected = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
1919
{
@@ -22,7 +22,7 @@ public void PresetRequestParserExtractsCommands()
2222
};
2323

2424
HttpContext context = CreateHttpContext("?preset=Preset1");
25-
IDictionary<string, string> actual = new PresetRequestParser(Options.Create(new PresetRequestParserOptions
25+
IDictionary<string, string> actual = new PresetOnlyQueryCollectionRequestParser(Options.Create(new PresetOnlyQueryCollectionRequestParserOptions
2626
{
2727
Presets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
2828
{
@@ -34,12 +34,12 @@ public void PresetRequestParserExtractsCommands()
3434
}
3535

3636
[Fact]
37-
public void PresetRequestParserCommandsWithoutPresetParam()
37+
public void PresetOnlyQueryCollectionRequestParserCommandsWithoutPresetParam()
3838
{
3939
IDictionary<string, string> expected = new Dictionary<string, string>();
4040

4141
HttpContext context = CreateHttpContext("?test=test");
42-
IDictionary<string, string> actual = new PresetRequestParser(Options.Create(new PresetRequestParserOptions
42+
IDictionary<string, string> actual = new PresetOnlyQueryCollectionRequestParser(Options.Create(new PresetOnlyQueryCollectionRequestParserOptions
4343
{
4444
Presets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
4545
{
@@ -51,12 +51,12 @@ public void PresetRequestParserCommandsWithoutPresetParam()
5151
}
5252

5353
[Fact]
54-
public void PresetRequestParserCommandsWithoutMatchingPreset()
54+
public void PresetOnlyQueryCollectionRequestParserCommandsWithoutMatchingPreset()
5555
{
5656
IDictionary<string, string> expected = new Dictionary<string, string>();
5757

5858
HttpContext context = CreateHttpContext("?preset=Preset2");
59-
IDictionary<string, string> actual = new PresetRequestParser(Options.Create(new PresetRequestParserOptions
59+
IDictionary<string, string> actual = new PresetOnlyQueryCollectionRequestParser(Options.Create(new PresetOnlyQueryCollectionRequestParserOptions
6060
{
6161
Presets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
6262
{

0 commit comments

Comments
 (0)