11using System ;
22using System . Net . Http ;
3+ using System . Net . Http . Headers ;
34using System . Text . RegularExpressions ;
45
56namespace HttpClientTestHelpers
@@ -96,10 +97,11 @@ public static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, str
9697 /// <summary>
9798 /// Determines whether a specific header is set on a request.
9899 /// </summary>
100+ /// <remarks>This method only checks headers in <see cref="System.Net.Http.Headers.HttpRequestHeaders"/></remarks>
99101 /// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
100102 /// <param name="headerName">The name of the header to locate on the request.</param>
101103 /// <returns>true when the request contains a header with the specified name; otherwise, false.</returns>
102- public static bool HasHeader ( this HttpRequestMessage httpRequestMessage , string headerName )
104+ public static bool HasRequestHeader ( this HttpRequestMessage httpRequestMessage , string headerName )
103105 {
104106 if ( httpRequestMessage == null )
105107 {
@@ -111,17 +113,18 @@ public static bool HasHeader(this HttpRequestMessage httpRequestMessage, string
111113 throw new ArgumentNullException ( nameof ( headerName ) ) ;
112114 }
113115
114- return httpRequestMessage . Headers . Contains ( headerName ) ;
116+ return httpRequestMessage . Headers . HasHeader ( headerName ) ;
115117 }
116118
117119 /// <summary>
118120 /// Determines whether a specific header with a specific value is set on a request.
119121 /// </summary>
122+ /// <remarks>This method only checks headers in <see cref="System.Net.Http.Headers.HttpRequestHeaders"/></remarks>
120123 /// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
121124 /// <param name="headerName">The name of the header to locate on the request.</param>
122125 /// <param name="headerValue">The value the header should have. Wildcard is supported.</param>
123126 /// <returns>true when the request contains a header with the specified name and value; otherwise, false.</returns>
124- public static bool HasHeader ( this HttpRequestMessage httpRequestMessage , string headerName , string headerValue )
127+ public static bool HasRequestHeader ( this HttpRequestMessage httpRequestMessage , string headerName , string headerValue )
125128 {
126129 if ( httpRequestMessage == null )
127130 {
@@ -138,7 +141,77 @@ public static bool HasHeader(this HttpRequestMessage httpRequestMessage, string
138141 throw new ArgumentNullException ( nameof ( headerValue ) ) ;
139142 }
140143
141- if ( httpRequestMessage . Headers . TryGetValues ( headerName , out var values ) )
144+ return httpRequestMessage . Headers . HasHeader ( headerName , headerValue ) ;
145+ }
146+
147+ /// <summary>
148+ /// Determines whether a specific header is set on a request.
149+ /// </summary>
150+ /// <remarks>This method only checks headers in <see cref="System.Net.Http.Headers.HttpContentHeaders"/></remarks>
151+ /// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
152+ /// <param name="headerName">The name of the header to locate on the request.</param>
153+ /// <returns>true when the request contains a header with the specified name; otherwise, false.</returns>
154+ public static bool HasContentHeader ( this HttpRequestMessage httpRequestMessage , string headerName )
155+ {
156+ if ( httpRequestMessage == null )
157+ {
158+ throw new ArgumentNullException ( nameof ( httpRequestMessage ) ) ;
159+ }
160+
161+ if ( string . IsNullOrEmpty ( headerName ) )
162+ {
163+ throw new ArgumentNullException ( nameof ( headerName ) ) ;
164+ }
165+
166+ if ( httpRequestMessage . Content == null )
167+ {
168+ return false ;
169+ }
170+
171+ return httpRequestMessage . Content . Headers . HasHeader ( headerName ) ;
172+ }
173+
174+ /// <summary>
175+ /// Determines whether a specific header with a specific value is set on a request.
176+ /// </summary>
177+ /// <remarks>This method only checks headers in <see cref="System.Net.Http.Headers.HttpContentHeaders"/></remarks>
178+ /// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
179+ /// <param name="headerName">The name of the header to locate on the request.</param>
180+ /// <param name="headerValue">The value the header should have. Wildcard is supported.</param>
181+ /// <returns>true when the request contains a header with the specified name and value; otherwise, false.</returns>
182+ public static bool HasContentHeader ( this HttpRequestMessage httpRequestMessage , string headerName , string headerValue )
183+ {
184+ if ( httpRequestMessage == null )
185+ {
186+ throw new ArgumentNullException ( nameof ( httpRequestMessage ) ) ;
187+ }
188+
189+ if ( string . IsNullOrEmpty ( headerName ) )
190+ {
191+ throw new ArgumentNullException ( nameof ( headerName ) ) ;
192+ }
193+
194+ if ( string . IsNullOrEmpty ( headerValue ) )
195+ {
196+ throw new ArgumentNullException ( nameof ( headerValue ) ) ;
197+ }
198+
199+ if ( httpRequestMessage . Content == null )
200+ {
201+ return false ;
202+ }
203+
204+ return httpRequestMessage . Content . Headers . HasHeader ( headerName , headerValue ) ;
205+ }
206+
207+ private static bool HasHeader ( this HttpHeaders headers , string headerName )
208+ {
209+ return headers . Contains ( headerName ) ;
210+ }
211+
212+ private static bool HasHeader ( this HttpHeaders headers , string headerName , string headerValue )
213+ {
214+ if ( headers . TryGetValues ( headerName , out var values ) )
142215 {
143216 var value = string . Join ( " " , values ) ;
144217 return Matches ( value , headerValue ) ;
0 commit comments