Skip to content

Commit 71d0ef2

Browse files
committed
Remove obsolete methods
1 parent 32b4888 commit 71d0ef2

7 files changed

Lines changed: 125 additions & 154 deletions

File tree

Source/Mockolate/Web/ItExtensions.HttpContent.WithFormData.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,14 @@ namespace Mockolate.Web;
1212
#pragma warning disable S2325 // Methods and properties that don't access instance data should be static
1313
public static partial class ItExtensions
1414
{
15-
private static IEnumerable<(string Key, string Value)> ParseFormDataParameters(string input)
16-
=> input.TrimStart('?')
17-
.Split('&')
18-
.Select(pair => pair.Split('=', 2))
19-
.Where(pair => !string.IsNullOrWhiteSpace(pair[0]))
20-
.Select(pair =>
21-
(
22-
WebUtility.UrlDecode(pair[0]),
23-
pair.Length == 2 ? WebUtility.UrlDecode(pair[1]) : ""
24-
)
25-
);
26-
2715
/// <inheritdoc cref="IHttpContentParameter" />
2816
extension(IHttpContentParameter parameter)
2917
{
3018
/// <summary>
3119
/// Expects the form data content to contain the given <paramref name="key" />-<paramref name="value" /> pair.
3220
/// </summary>
3321
public IFormDataContentParameter WithFormData(string key, HttpFormDataValue value)
34-
=> parameter.WithFormData([(key, value)]);
22+
=> parameter.WithFormData([(key, value),]);
3523

3624
/// <summary>
3725
/// Expects the form data content to contain the given <paramref name="values" />.
@@ -47,7 +35,7 @@ public IFormDataContentParameter WithFormData(params IEnumerable<(string Key, Ht
4735
/// Expects the form data content to contain the given <paramref name="values" />.
4836
/// </summary>
4937
public IFormDataContentParameter WithFormData(string values)
50-
=> parameter.WithFormData(ParseFormDataParameters(values)
38+
=> parameter.WithFormData(FormDataMatcher.ParseFormDataParameters(values)
5139
.Select(pair => (pair.Key, new HttpFormDataValue(pair.Value))));
5240
}
5341

@@ -92,6 +80,18 @@ public bool Matches(string content)
9280
public void Exactly()
9381
=> _isExactly = true;
9482

83+
internal static IEnumerable<(string Key, string Value)> ParseFormDataParameters(string input)
84+
=> input.TrimStart('?')
85+
.Split('&')
86+
.Select(pair => pair.Split('=', 2))
87+
.Where(pair => !string.IsNullOrWhiteSpace(pair[0]))
88+
.Select(pair =>
89+
(
90+
WebUtility.UrlDecode(pair[0]),
91+
pair.Length == 2 ? WebUtility.UrlDecode(pair[1]) : ""
92+
)
93+
);
94+
9595
private IEnumerable<(string, string)> GetFormData(string rawContent)
9696
{
9797
string rawFormData = ExtractFormDataFromMultipartContent(rawContent);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Net.Http.Headers;
7+
#if NETSTANDARD2_0
8+
using Mockolate.Internals.Polyfills;
9+
#endif
10+
11+
namespace Mockolate.Web;
12+
13+
/// <summary>
14+
/// Extensions for parameter matchers for HTTP-related types.
15+
/// </summary>
16+
public static partial class ItExtensions
17+
{
18+
/// <inheritdoc cref="IHttpContentParameter" />
19+
extension<TParameter>(IHttpHeaderParameter<TParameter> parameter)
20+
{
21+
/// <summary>
22+
/// Expects the <see cref="HttpContent" /> to contain a header matching the <paramref name="name" /> and
23+
/// <paramref name="value" />.
24+
/// </summary>
25+
public TParameter WithHeaders(string name, HttpHeaderValue value)
26+
=> parameter.WithHeaders((name, value));
27+
28+
/// <summary>
29+
/// Expects the <see cref="HttpContent" /> to contain the given <paramref name="headers" />.
30+
/// </summary>
31+
public TParameter WithHeaders(string headers)
32+
{
33+
List<(string, HttpHeaderValue)> headerList = new();
34+
using StringReader reader = new(headers);
35+
string? line = reader.ReadLine();
36+
while (!string.IsNullOrWhiteSpace(line))
37+
{
38+
string[] parts = line.Split(':', 2);
39+
40+
if (parts.Length != 2)
41+
{
42+
// ReSharper disable once LocalizableElement
43+
throw new ArgumentException("The header contained an invalid line: " + line, nameof(headers));
44+
}
45+
46+
headerList.Add((parts[0].Trim(), parts[1].TrimStart(' ')));
47+
line = reader.ReadLine();
48+
}
49+
50+
return parameter.WithHeaders(headerList);
51+
}
52+
}
53+
54+
/// <summary>
55+
/// Further expectations on the <see cref="HttpContent" />.
56+
/// </summary>
57+
public interface IHttpHeaderParameter<out TParameter>
58+
{
59+
/// <summary>
60+
/// Expects the <see cref="HttpContent" /> to contain the given <paramref name="headers" />.
61+
/// </summary>
62+
TParameter WithHeaders(params IEnumerable<(string Name, HttpHeaderValue Value)> headers);
63+
}
64+
65+
private sealed class HttpHeadersMatcher
66+
{
67+
private readonly List<(string Name, HttpHeaderValue Value)> _requiredHeaders = [];
68+
69+
public bool IncludeRequestHeaders { get; private set; }
70+
71+
public void AddRequiredHeader(IEnumerable<(string Name, HttpHeaderValue Value)> headers)
72+
=> _requiredHeaders.AddRange(headers);
73+
74+
public bool Matches(HttpHeaders messageHeaders, HttpHeaders? alternativeHeaders = null)
75+
=> alternativeHeaders is null
76+
? _requiredHeaders.All(header => MatchesHeader(header.Name, header.Value, messageHeaders))
77+
: _requiredHeaders.All(header => MatchesHeader(header.Name, header.Value, messageHeaders) ||
78+
MatchesHeader(header.Name, header.Value, alternativeHeaders));
79+
80+
private static bool MatchesHeader(string name, HttpHeaderValue value, HttpHeaders messageHeaders)
81+
{
82+
if (!messageHeaders.TryGetValues(name, out IEnumerable<string>? values))
83+
{
84+
return false;
85+
}
86+
87+
return values.Any(value.Matches);
88+
}
89+
90+
public void IncludingRequestHeaders()
91+
=> IncludeRequestHeaders = true;
92+
}
93+
}

Source/Mockolate/Web/ItExtensions.HttpRequestMessage.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,13 @@ private abstract class HttpRequestMessageParameter<TParameter>
6464
/// </summary>
6565
protected abstract TParameter GetThis { get; }
6666

67-
public TParameter WithHeaders(string name, HttpHeaderValue value)
68-
{
69-
_headers ??= new HttpHeadersMatcher();
70-
_headers.AddRequiredHeader(name, value);
71-
return GetThis;
72-
}
73-
7467
public TParameter WithHeaders(params IEnumerable<(string Name, HttpHeaderValue Value)> headers)
7568
{
7669
_headers ??= new HttpHeadersMatcher();
7770
_headers.AddRequiredHeader(headers);
7871
return GetThis;
7972
}
8073

81-
public TParameter WithHeaders(string headers)
82-
{
83-
_headers ??= new HttpHeadersMatcher();
84-
_headers.AddRequiredHeader(headers);
85-
return GetThis;
86-
}
87-
8874
/// <inheritdoc cref="IParameter{T}.Do(Action{T})" />
8975
public IParameter<HttpRequestMessage> Do(Action<HttpRequestMessage> callback)
9076
{
Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
54
using System.Net;
6-
using System.Net.Http;
7-
using System.Net.Http.Headers;
85
#if NETSTANDARD2_0
96
using Mockolate.Internals.Polyfills;
107
#endif
@@ -16,83 +13,6 @@ namespace Mockolate.Web;
1613
/// </summary>
1714
public static partial class ItExtensions
1815
{
19-
private static List<(string Name, HttpHeaderValue Value)> ExtractHeaders(string headers)
20-
{
21-
List<(string, HttpHeaderValue)> headerList = new();
22-
using StringReader reader = new(headers);
23-
string? line = reader.ReadLine();
24-
while (!string.IsNullOrWhiteSpace(line))
25-
{
26-
string[] parts = line.Split(':', 2);
27-
28-
if (parts.Length != 2)
29-
{
30-
throw new ArgumentException("The header contained an invalid line: " + line, nameof(headers));
31-
}
32-
33-
headerList.Add((parts[0].Trim(), parts[1].TrimStart(' ')));
34-
line = reader.ReadLine();
35-
}
36-
37-
return headerList;
38-
}
39-
40-
private sealed class HttpHeadersMatcher
41-
{
42-
private readonly List<(string Name, HttpHeaderValue Value)> _requiredHeaders = [];
43-
44-
public bool IncludeRequestHeaders { get; private set; }
45-
46-
public void AddRequiredHeader(string name, HttpHeaderValue value)
47-
=> _requiredHeaders.Add((name, value));
48-
49-
public void AddRequiredHeader(IEnumerable<(string Name, HttpHeaderValue Value)> headers)
50-
=> _requiredHeaders.AddRange(headers);
51-
52-
public void AddRequiredHeader(string headers)
53-
=> _requiredHeaders.AddRange(ExtractHeaders(headers));
54-
55-
public bool Matches(HttpHeaders messageHeaders, HttpHeaders? alternativeHeaders = null)
56-
=> alternativeHeaders is null
57-
? _requiredHeaders.All(header => MatchesHeader(header.Name, header.Value, messageHeaders))
58-
: _requiredHeaders.All(header => MatchesHeader(header.Name, header.Value, messageHeaders) ||
59-
MatchesHeader(header.Name, header.Value, alternativeHeaders));
60-
61-
private static bool MatchesHeader(string name, HttpHeaderValue value, HttpHeaders messageHeaders)
62-
{
63-
if (!messageHeaders.TryGetValues(name, out IEnumerable<string>? values))
64-
{
65-
return false;
66-
}
67-
68-
return values.Any(value.Matches);
69-
}
70-
71-
private static List<(string, HttpHeaderValue)> ExtractHeaders(string headers)
72-
{
73-
List<(string, HttpHeaderValue)> headerList = new();
74-
using StringReader reader = new(headers);
75-
string? line = reader.ReadLine();
76-
while (!string.IsNullOrWhiteSpace(line))
77-
{
78-
string[] parts = line.Split(':', 2);
79-
80-
if (parts.Length != 2)
81-
{
82-
throw new ArgumentException("The header contained an invalid line: " + line, nameof(headers));
83-
}
84-
85-
headerList.Add((parts[0].Trim(), parts[1].TrimStart(' ')));
86-
line = reader.ReadLine();
87-
}
88-
89-
return headerList;
90-
}
91-
92-
public void IncludingRequestHeaders()
93-
=> IncludeRequestHeaders = true;
94-
}
95-
9616
private sealed class HttpQueryMatcher
9717
{
9818
private readonly List<(string Name, HttpQueryParameterValue Value)> _requiredQueryParameters = [];
@@ -129,32 +49,4 @@ public bool Matches(Uri uri)
12949
)
13050
);
13151
}
132-
133-
/// <summary>
134-
/// Further expectations on the <see cref="HttpContent" />.
135-
/// </summary>
136-
public interface IHttpHeaderParameter<out TParameter>
137-
{
138-
/// <summary>
139-
/// Expects the <see cref="HttpContent" /> to contain the given <paramref name="headers" />.
140-
/// </summary>
141-
TParameter WithHeaders(params IEnumerable<(string Name, HttpHeaderValue Value)> headers);
142-
}
143-
144-
/// <inheritdoc cref="IHttpContentParameter" />
145-
extension<TParameter>(IHttpHeaderParameter<TParameter> parameter)
146-
{
147-
/// <summary>
148-
/// Expects the <see cref="HttpContent" /> to contain a header matching the <paramref name="name" /> and
149-
/// <paramref name="value" />.
150-
/// </summary>
151-
public TParameter WithHeaders(string name, HttpHeaderValue value)
152-
=> parameter.WithHeaders((name, value));
153-
154-
/// <summary>
155-
/// Expects the <see cref="HttpContent" /> to contain the given <paramref name="headers" />.
156-
/// </summary>
157-
public TParameter WithHeaders(string headers)
158-
=> parameter.WithHeaders(ExtractHeaders(headers));
159-
}
16052
}

Tests/Mockolate.Api.Tests/Expected/Mockolate_net10.0.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,12 +1916,6 @@ namespace Mockolate.Web
19161916
Mockolate.Web.ItExtensions.IHttpRequestMessageParameter IsHttpRequestMessage(System.Net.Http.HttpMethod? method = null);
19171917
Mockolate.Web.ItExtensions.IUriParameter IsUri(string? pattern = null);
19181918
}
1919-
extension<TParameter>(Mockolate.Web.ItExtensions.IHttpHeaderParameter<TParameter> parameter)
1920-
where TParameter : notnull
1921-
{
1922-
TParameter WithHeaders(string name, Mockolate.Web.HttpHeaderValue value);
1923-
TParameter WithHeaders(string headers);
1924-
}
19251919
extension(Mockolate.Web.ItExtensions.IHttpContentParameter parameter)
19261920
{
19271921
Mockolate.Web.ItExtensions.IHttpContentParameter WithBytes(byte[] bytes);
@@ -1933,5 +1927,11 @@ namespace Mockolate.Web
19331927
Mockolate.Web.ItExtensions.IStringContentBodyParameter WithString(string expected);
19341928
Mockolate.Web.ItExtensions.IStringContentBodyMatchingParameter WithStringMatching(string pattern);
19351929
}
1930+
extension<TParameter>(Mockolate.Web.ItExtensions.IHttpHeaderParameter<TParameter> parameter)
1931+
where TParameter : notnull
1932+
{
1933+
TParameter WithHeaders(string name, Mockolate.Web.HttpHeaderValue value);
1934+
TParameter WithHeaders(string headers);
1935+
}
19361936
}
19371937
}

Tests/Mockolate.Api.Tests/Expected/Mockolate_net8.0.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,12 +1915,6 @@ namespace Mockolate.Web
19151915
Mockolate.Web.ItExtensions.IHttpRequestMessageParameter IsHttpRequestMessage(System.Net.Http.HttpMethod? method = null);
19161916
Mockolate.Web.ItExtensions.IUriParameter IsUri(string? pattern = null);
19171917
}
1918-
extension<TParameter>(Mockolate.Web.ItExtensions.IHttpHeaderParameter<TParameter> parameter)
1919-
where TParameter : notnull
1920-
{
1921-
TParameter WithHeaders(string name, Mockolate.Web.HttpHeaderValue value);
1922-
TParameter WithHeaders(string headers);
1923-
}
19241918
extension(Mockolate.Web.ItExtensions.IHttpContentParameter parameter)
19251919
{
19261920
Mockolate.Web.ItExtensions.IHttpContentParameter WithBytes(byte[] bytes);
@@ -1932,5 +1926,11 @@ namespace Mockolate.Web
19321926
Mockolate.Web.ItExtensions.IStringContentBodyParameter WithString(string expected);
19331927
Mockolate.Web.ItExtensions.IStringContentBodyMatchingParameter WithStringMatching(string pattern);
19341928
}
1929+
extension<TParameter>(Mockolate.Web.ItExtensions.IHttpHeaderParameter<TParameter> parameter)
1930+
where TParameter : notnull
1931+
{
1932+
TParameter WithHeaders(string name, Mockolate.Web.HttpHeaderValue value);
1933+
TParameter WithHeaders(string headers);
1934+
}
19351935
}
19361936
}

Tests/Mockolate.Api.Tests/Expected/Mockolate_netstandard2.0.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,12 +1856,6 @@ namespace Mockolate.Web
18561856
Mockolate.Web.ItExtensions.IHttpRequestMessageParameter IsHttpRequestMessage(System.Net.Http.HttpMethod? method = null);
18571857
Mockolate.Web.ItExtensions.IUriParameter IsUri(string? pattern = null);
18581858
}
1859-
extension<TParameter>(Mockolate.Web.ItExtensions.IHttpHeaderParameter<TParameter> parameter)
1860-
where TParameter : notnull
1861-
{
1862-
TParameter WithHeaders(string name, Mockolate.Web.HttpHeaderValue value);
1863-
TParameter WithHeaders(string headers);
1864-
}
18651859
extension(Mockolate.Web.ItExtensions.IHttpContentParameter parameter)
18661860
{
18671861
Mockolate.Web.ItExtensions.IHttpContentParameter WithBytes(byte[] bytes);
@@ -1873,5 +1867,11 @@ namespace Mockolate.Web
18731867
Mockolate.Web.ItExtensions.IStringContentBodyParameter WithString(string expected);
18741868
Mockolate.Web.ItExtensions.IStringContentBodyMatchingParameter WithStringMatching(string pattern);
18751869
}
1870+
extension<TParameter>(Mockolate.Web.ItExtensions.IHttpHeaderParameter<TParameter> parameter)
1871+
where TParameter : notnull
1872+
{
1873+
TParameter WithHeaders(string name, Mockolate.Web.HttpHeaderValue value);
1874+
TParameter WithHeaders(string headers);
1875+
}
18761876
}
18771877
}

0 commit comments

Comments
 (0)