Skip to content

Commit 6efe471

Browse files
committed
Added tests
1 parent ec1fae1 commit 6efe471

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 PresetRequestParserTests
14+
{
15+
[Fact]
16+
public void PresetRequestParserExtractsCommands()
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 PresetRequestParser(Options.Create(new PresetRequestParserOptions
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 PresetRequestParserCommandsWithoutPresetParam()
38+
{
39+
IDictionary<string, string> expected = new Dictionary<string, string>();
40+
41+
HttpContext context = CreateHttpContext("?test=test");
42+
IDictionary<string, string> actual = new PresetRequestParser(Options.Create(new PresetRequestParserOptions
43+
{
44+
Presets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
45+
{
46+
{ "Preset1", "width=400&height=200" }
47+
}
48+
})).ParseRequestCommands(context);
49+
50+
Assert.Equal(expected, actual);
51+
}
52+
53+
[Fact]
54+
public void PresetRequestParserCommandsWithoutMatchingPreset()
55+
{
56+
IDictionary<string, string> expected = new Dictionary<string, string>();
57+
58+
HttpContext context = CreateHttpContext("?preset=Preset2");
59+
IDictionary<string, string> actual = new PresetRequestParser(Options.Create(new PresetRequestParserOptions
60+
{
61+
Presets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
62+
{
63+
{ "Preset1", "width=400&height=200" }
64+
}
65+
})).ParseRequestCommands(context);
66+
67+
Assert.Equal(expected, actual);
68+
}
69+
70+
private static HttpContext CreateHttpContext(string query)
71+
{
72+
var httpContext = new DefaultHttpContext();
73+
httpContext.Request.Path = "/testwebsite.com/image-12345.jpeg";
74+
httpContext.Request.QueryString = new QueryString(query);
75+
return httpContext;
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)