11using System ;
22using System . Collections . Generic ;
3- using System . IO ;
43using System . Linq ;
54using System . Net ;
6- using System . Net . Http ;
7- using System . Net . Http . Headers ;
85#if NETSTANDARD2_0
96using Mockolate . Internals . Polyfills ;
107#endif
@@ -16,83 +13,6 @@ namespace Mockolate.Web;
1613/// </summary>
1714public 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}
0 commit comments