|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | +using SixLabors.ImageSharp.Web.Commands; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace SixLabors.ImageSharp.Web.Tests.Commands |
| 12 | +{ |
| 13 | + public class PresetOnlyQueryCollectionRequestParserTests |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public void PresetOnlyQueryCollectionRequestParserExtractsCommands() |
| 17 | + { |
| 18 | + IDictionary<string, string> expected = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| 19 | + { |
| 20 | + { "width", "400" }, |
| 21 | + { "height", "200" } |
| 22 | + }; |
| 23 | + |
| 24 | + HttpContext context = CreateHttpContext("?preset=Preset1"); |
| 25 | + IDictionary<string, string> actual = new PresetOnlyQueryCollectionRequestParser(Options.Create(new PresetOnlyQueryCollectionRequestParserOptions |
| 26 | + { |
| 27 | + Presets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| 28 | + { |
| 29 | + { "Preset1", "width=400&height=200" } |
| 30 | + } |
| 31 | + })).ParseRequestCommands(context); |
| 32 | + |
| 33 | + Assert.Equal(expected, actual); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void PresetOnlyQueryCollectionRequestParserExtractsCommandsWithOtherCasing() |
| 38 | + { |
| 39 | + IDictionary<string, string> expected = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| 40 | + { |
| 41 | + { "width", "400" }, |
| 42 | + { "height", "200" } |
| 43 | + }; |
| 44 | + |
| 45 | + HttpContext context = CreateHttpContext("?PRESET=PRESET1"); |
| 46 | + IDictionary<string, string> actual = new PresetOnlyQueryCollectionRequestParser(Options.Create(new PresetOnlyQueryCollectionRequestParserOptions |
| 47 | + { |
| 48 | + Presets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| 49 | + { |
| 50 | + { "Preset1", "width=400&height=200" } |
| 51 | + } |
| 52 | + })).ParseRequestCommands(context); |
| 53 | + |
| 54 | + Assert.Equal(expected, actual); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void PresetOnlyQueryCollectionRequestParserCommandsWithoutPresetParam() |
| 60 | + { |
| 61 | + IDictionary<string, string> expected = new Dictionary<string, string>(); |
| 62 | + |
| 63 | + HttpContext context = CreateHttpContext("?test=test"); |
| 64 | + IDictionary<string, string> actual = new PresetOnlyQueryCollectionRequestParser(Options.Create(new PresetOnlyQueryCollectionRequestParserOptions |
| 65 | + { |
| 66 | + Presets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| 67 | + { |
| 68 | + { "Preset1", "width=400&height=200" } |
| 69 | + } |
| 70 | + })).ParseRequestCommands(context); |
| 71 | + |
| 72 | + Assert.Equal(expected, actual); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void PresetOnlyQueryCollectionRequestParserCommandsWithoutMatchingPreset() |
| 77 | + { |
| 78 | + IDictionary<string, string> expected = new Dictionary<string, string>(); |
| 79 | + |
| 80 | + HttpContext context = CreateHttpContext("?preset=Preset2"); |
| 81 | + IDictionary<string, string> actual = new PresetOnlyQueryCollectionRequestParser(Options.Create(new PresetOnlyQueryCollectionRequestParserOptions |
| 82 | + { |
| 83 | + Presets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| 84 | + { |
| 85 | + { "Preset1", "width=400&height=200" } |
| 86 | + } |
| 87 | + })).ParseRequestCommands(context); |
| 88 | + |
| 89 | + Assert.Equal(expected, actual); |
| 90 | + } |
| 91 | + |
| 92 | + private static HttpContext CreateHttpContext(string query) |
| 93 | + { |
| 94 | + var httpContext = new DefaultHttpContext(); |
| 95 | + httpContext.Request.Path = "/testwebsite.com/image-12345.jpeg"; |
| 96 | + httpContext.Request.QueryString = new QueryString(query); |
| 97 | + return httpContext; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments