diff --git a/src/Faithlife.WebRequests/ByteRange.cs b/src/Faithlife.WebRequests/ByteRange.cs index dd904c2..90e321e 100644 --- a/src/Faithlife.WebRequests/ByteRange.cs +++ b/src/Faithlife.WebRequests/ByteRange.cs @@ -53,6 +53,6 @@ public ByteRange(long from, long to) /// public bool HasEnd => m_to.HasValue; - readonly long? m_to; + private readonly long? m_to; } } diff --git a/src/Faithlife.WebRequests/CookieManager.cs b/src/Faithlife.WebRequests/CookieManager.cs index 6af2854..2cc93fd 100644 --- a/src/Faithlife.WebRequests/CookieManager.cs +++ b/src/Faithlife.WebRequests/CookieManager.cs @@ -45,7 +45,7 @@ public CookieManager(IEnumerable seqCookies) } /// - /// Returns a reference to the for this instance. + /// Returns a reference to the for this instance. /// public CookieContainer CookieContainer { get; } @@ -114,7 +114,7 @@ public void ExpireCookies() /// public event EventHandler? CookiesChanged; - readonly object m_objLock; - readonly HashSet m_setUris; + private readonly object m_objLock; + private readonly HashSet m_setUris; } } diff --git a/src/Faithlife.WebRequests/CookieUtility.cs b/src/Faithlife.WebRequests/CookieUtility.cs index 258e0bc..52391ca 100644 --- a/src/Faithlife.WebRequests/CookieUtility.cs +++ b/src/Faithlife.WebRequests/CookieUtility.cs @@ -10,7 +10,7 @@ namespace Faithlife.WebRequests public static class CookieUtility { /// - /// Returns a representing the specified cookie, in the format prescribed by + /// Returns a representing the specified cookie, in the format prescribed by /// RFC 2965 for the Set-Cookie HTTP header value. /// /// The cookie. diff --git a/src/Faithlife.WebRequests/HttpHeadersUtility.cs b/src/Faithlife.WebRequests/HttpHeadersUtility.cs new file mode 100644 index 0000000..489a270 --- /dev/null +++ b/src/Faithlife.WebRequests/HttpHeadersUtility.cs @@ -0,0 +1,23 @@ +using System.Net; +using System.Net.Http.Headers; +using Faithlife.Utility; + +namespace Faithlife.WebRequests +{ + internal static class HttpHeadersUtility + { + public static void AddWebHeaders(this HttpHeaders self, WebHeaderCollection headers) + { + foreach (var headerName in headers.AllKeys) + self.Add(headerName, headers[headerName]); + } + + public static WebHeaderCollection ToWebHeaderCollection(this HttpHeaders self) + { + var collection = new WebHeaderCollection(); + foreach (var pair in self) + collection.Add(pair.Key, pair.Value.Join("; ")); + return collection; + } + } +} diff --git a/src/Faithlife.WebRequests/HttpResponseMessageUtility.cs b/src/Faithlife.WebRequests/HttpResponseMessageUtility.cs index 700d78a..9f592c3 100644 --- a/src/Faithlife.WebRequests/HttpResponseMessageUtility.cs +++ b/src/Faithlife.WebRequests/HttpResponseMessageUtility.cs @@ -16,8 +16,8 @@ public static class HttpResponseMessageUtility /// /// The response. /// The message. - /// The content preview. /// The inner exception. + /// The content preview. /// A new exception. public static WebServiceException CreateWebServiceException(HttpResponseMessage response, string? message = null, Exception? innerException = null, string? contentPreview = null) { @@ -72,6 +72,6 @@ public static async Task CreateWebServiceExceptionWithConte } } - const int c_contentPreviewCharacterCount = 2000; + private const int c_contentPreviewCharacterCount = 2000; } } diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs index c08b9c2..5a94664 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs @@ -5,10 +5,10 @@ using System.Net; using System.Net.Http; using System.Reflection; +using System.Threading; using System.Threading.Tasks; -using Faithlife.Utility; using Faithlife.Json; -using System.Threading; +using Faithlife.Utility; namespace Faithlife.WebRequests.Json { @@ -96,8 +96,8 @@ protected override async Task HandleResponseCoreAsync(WebServiceResponseHa // read headers foreach (var header in webResponse.Headers) { - string headerName = header.Key; // remove hyphens before looking for property setter by name + string headerName = header.Key; string propertyName = headerName.Replace("-", ""); var headerProperty = GetProperty(responseType, propertyName); if (headerProperty?.CanWrite ?? false) @@ -235,7 +235,7 @@ protected override void Dispose(bool disposing) } } - readonly HttpResponseMessage m_webResponse; + private readonly HttpResponseMessage m_webResponse; } } } diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs index e91ae89..15134d4 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs @@ -60,12 +60,12 @@ internal Task OnResponseHandledAsync(WebServiceResponseHandlerInfo info) return OnResponseHandledCoreAsync(info); } - string? m_requestMethod; - Uri? m_requestUri; - HttpStatusCode? m_responseStatusCode; - HttpHeaders? m_responseHeaders; - string? m_responseContentType; - long? m_responseContentLength; - string? m_responseContentPreview; + private string? m_requestMethod; + private Uri? m_requestUri; + private HttpStatusCode? m_responseStatusCode; + private HttpHeaders? m_responseHeaders; + private string? m_responseContentType; + private long? m_responseContentLength; + private string? m_responseContentPreview; } } diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs index 39bf31e..64c8333 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs @@ -16,7 +16,8 @@ public static class AutoWebServiceResponseUtility /// The AutoWebServiceResponse to read the property from. /// The func that reads the desired property. /// The value of the property. - public static TProperty GetExpectedResult(this TResponse response, Func getProperty) where TResponse : AutoWebServiceResponse + public static TProperty GetExpectedResult(this TResponse response, Func getProperty) + where TResponse : AutoWebServiceResponse { TProperty propertyValue = getProperty(response); @@ -33,7 +34,8 @@ public static TProperty GetExpectedResult(this TResponse r /// The type of the property. /// The AutoWebServiceResponse to read the property from. /// The func that reads the desired property. - public static void VerifyResultIsExpected(this TResponse response, Func getProperty) where TResponse : AutoWebServiceResponse + public static void VerifyResultIsExpected(this TResponse response, Func getProperty) + where TResponse : AutoWebServiceResponse { GetExpectedResult(response, getProperty); } diff --git a/src/Faithlife.WebRequests/Json/GenericStatusCodeResponse.cs b/src/Faithlife.WebRequests/Json/GenericStatusCodeResponse.cs index 082f6b9..a15ff90 100644 --- a/src/Faithlife.WebRequests/Json/GenericStatusCodeResponse.cs +++ b/src/Faithlife.WebRequests/Json/GenericStatusCodeResponse.cs @@ -5,7 +5,7 @@ namespace Faithlife.WebRequests.Json { /// - /// AutoWebServiceRequest responses with an explicitly set status code. + /// AutoWebServiceRequest responses with an explicitly set status code. /// public class GenericStatusCodeResponse : AutoWebServiceResponse { diff --git a/src/Faithlife.WebRequests/Json/JsonWebResponseUtility.cs b/src/Faithlife.WebRequests/Json/JsonWebResponseUtility.cs index 3298250..e3bf343 100644 --- a/src/Faithlife.WebRequests/Json/JsonWebResponseUtility.cs +++ b/src/Faithlife.WebRequests/Json/JsonWebResponseUtility.cs @@ -2,8 +2,8 @@ using System.IO; using System.Net.Http; using System.Threading.Tasks; -using Faithlife.Utility; using Faithlife.Json; +using Faithlife.Utility; using Newtonsoft.Json; namespace Faithlife.WebRequests.Json diff --git a/src/Faithlife.WebRequests/Json/JsonWebServiceClientBase.cs b/src/Faithlife.WebRequests/Json/JsonWebServiceClientBase.cs index c6ffaf6..48d4687 100644 --- a/src/Faithlife.WebRequests/Json/JsonWebServiceClientBase.cs +++ b/src/Faithlife.WebRequests/Json/JsonWebServiceClientBase.cs @@ -154,7 +154,7 @@ private AutoWebServiceRequest DoCreateRequest(Uri uri) return request; } - readonly Uri m_baseUri; - readonly JsonWebServiceClientSettings m_clientSettings; + private readonly Uri m_baseUri; + private readonly JsonWebServiceClientSettings m_clientSettings; } } diff --git a/src/Faithlife.WebRequests/Json/JsonWebServiceContent.cs b/src/Faithlife.WebRequests/Json/JsonWebServiceContent.cs index 303fc6a..b78336c 100644 --- a/src/Faithlife.WebRequests/Json/JsonWebServiceContent.cs +++ b/src/Faithlife.WebRequests/Json/JsonWebServiceContent.cs @@ -5,8 +5,8 @@ using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; -using Faithlife.Utility; using Faithlife.Json; +using Faithlife.Utility; namespace Faithlife.WebRequests.Json { @@ -100,13 +100,14 @@ protected override bool TryComputeLength(out long length) return Json is object; } - string? m_json; + private string? m_json; } /// /// JSON web service content. /// /// The type of the value. + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Generic.")] public class JsonWebServiceContent : JsonWebServiceContent { /// @@ -145,6 +146,6 @@ protected internal JsonWebServiceContent(TValue value, JsonSettings? settings) /// The JSON. protected override string GenerateJson() => JsonUtility.ToJson(Value, m_outputSettings); - readonly JsonSettings? m_outputSettings; + private readonly JsonSettings? m_outputSettings; } } diff --git a/src/Faithlife.WebRequests/Json/JsonWebServiceRequest.cs b/src/Faithlife.WebRequests/Json/JsonWebServiceRequest.cs index 7ee40a9..caf3cd7 100644 --- a/src/Faithlife.WebRequests/Json/JsonWebServiceRequest.cs +++ b/src/Faithlife.WebRequests/Json/JsonWebServiceRequest.cs @@ -1,11 +1,12 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; -using Faithlife.Utility; using Faithlife.Json; +using Faithlife.Utility; using Newtonsoft.Json; namespace Faithlife.WebRequests.Json @@ -70,6 +71,7 @@ protected override async Task CreateResponseAsync(WebService /// A JSON web service request. /// /// The type of the response value. + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Generic.")] public class JsonWebServiceRequest : JsonWebServiceRequest { /// diff --git a/src/Faithlife.WebRequests/Json/JsonWebServiceRequestUtility.cs b/src/Faithlife.WebRequests/Json/JsonWebServiceRequestUtility.cs index d38561a..03c5ffe 100644 --- a/src/Faithlife.WebRequests/Json/JsonWebServiceRequestUtility.cs +++ b/src/Faithlife.WebRequests/Json/JsonWebServiceRequestUtility.cs @@ -10,12 +10,8 @@ public static class JsonWebServiceRequestUtility /// /// Sets the Content of the WebServiceRequest. /// - /// The type of the web service request. - /// The type of the content value. - /// The request. - /// The content value. - /// The request. - public static TWebServiceRequest WithJsonContent(this TWebServiceRequest request, TContentValue contentValue) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithJsonContent(this TWebServiceRequest request, TContentValue contentValue) + where TWebServiceRequest : WebServiceRequestBase { request.Content = JsonWebServiceContent.FromValue(contentValue); return request; @@ -24,25 +20,16 @@ public static TWebServiceRequest WithJsonContent /// Sets the Content of the WebServiceRequest. /// - /// The type of the web service request. - /// The type of the content value. - /// The request. - /// The content value. - /// The settings. - /// The request. - public static TWebServiceRequest WithJsonContent(this TWebServiceRequest request, TContentValue contentValue, JsonSettings settings) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithJsonContent(this TWebServiceRequest request, TContentValue contentValue, JsonSettings settings) + where TWebServiceRequest : WebServiceRequestBase { request.Content = JsonWebServiceContent.FromValue(contentValue, settings); return request; } /// - /// Sets the InputSettings of the WebServiceRequest. + /// Sets the JsonSettings of the WebServiceRequest. /// - /// The type of the response content. - /// The request. - /// The settings. - /// The request. public static JsonWebServiceRequest WithJsonSettings(this JsonWebServiceRequest request, JsonSettings settings) { request.JsonSettings = settings; diff --git a/src/Faithlife.WebRequests/Json/JsonWebServiceResponse.cs b/src/Faithlife.WebRequests/Json/JsonWebServiceResponse.cs index a395671..1c87f9d 100644 --- a/src/Faithlife.WebRequests/Json/JsonWebServiceResponse.cs +++ b/src/Faithlife.WebRequests/Json/JsonWebServiceResponse.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Net; using System.Net.Http; using System.Net.Http.Headers; @@ -39,6 +40,7 @@ public string? Json /// A JSON web response. /// /// The type of the value. + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Generic.")] public class JsonWebServiceResponse : JsonWebServiceResponse { /// diff --git a/src/Faithlife.WebRequests/Json/JsonWebServiceResponseUtility.cs b/src/Faithlife.WebRequests/Json/JsonWebServiceResponseUtility.cs index 1205714..28d40bd 100644 --- a/src/Faithlife.WebRequests/Json/JsonWebServiceResponseUtility.cs +++ b/src/Faithlife.WebRequests/Json/JsonWebServiceResponseUtility.cs @@ -1,7 +1,7 @@ using System.IO; using System.Threading.Tasks; -using Faithlife.Utility; using Faithlife.Json; +using Faithlife.Utility; using Newtonsoft.Json; namespace Faithlife.WebRequests.Json diff --git a/src/Faithlife.WebRequests/WebServiceErrorKind.cs b/src/Faithlife.WebRequests/WebServiceErrorKind.cs index 4cb6998..8130838 100644 --- a/src/Faithlife.WebRequests/WebServiceErrorKind.cs +++ b/src/Faithlife.WebRequests/WebServiceErrorKind.cs @@ -13,6 +13,6 @@ public enum WebServiceErrorKind /// /// The error occurred while handling the response. /// - Response + Response, } } diff --git a/src/Faithlife.WebRequests/WebServiceException.cs b/src/Faithlife.WebRequests/WebServiceException.cs index 1606bd1..d4c68cd 100644 --- a/src/Faithlife.WebRequests/WebServiceException.cs +++ b/src/Faithlife.WebRequests/WebServiceException.cs @@ -144,7 +144,7 @@ public override string Message } /// - /// Returns a that represents this instance. + /// Returns a that represents this instance. /// [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Don't want exceptions thrown from ToString.")] public override string ToString() diff --git a/src/Faithlife.WebRequests/WebServiceRequest.cs b/src/Faithlife.WebRequests/WebServiceRequest.cs index 24cf301..8952ab0 100644 --- a/src/Faithlife.WebRequests/WebServiceRequest.cs +++ b/src/Faithlife.WebRequests/WebServiceRequest.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Net; @@ -14,6 +15,7 @@ namespace Faithlife.WebRequests /// /// A web service request. /// + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Generic.")] public class WebServiceRequest : WebServiceRequestBase { /// @@ -109,8 +111,20 @@ protected override async Task HandleResponseCoreAsync(WebServiceResponseHa return true; } - static readonly IReadOnlyList s_defaultAcceptedStatusCodes = new[] { HttpStatusCode.OK, HttpStatusCode.Created }.AsReadOnly(); - static readonly IReadOnlyList s_defaultAcceptedStatusCodesWithRedirect = new[] { HttpStatusCode.OK, HttpStatusCode.Created, - HttpStatusCode.Moved, HttpStatusCode.Redirect, HttpStatusCode.RedirectMethod, HttpStatusCode.RedirectKeepVerb }.AsReadOnly(); + private static readonly IReadOnlyList s_defaultAcceptedStatusCodes = new[] + { + HttpStatusCode.OK, + HttpStatusCode.Created, + }.AsReadOnly(); + + private static readonly IReadOnlyList s_defaultAcceptedStatusCodesWithRedirect = new[] + { + HttpStatusCode.OK, + HttpStatusCode.Created, + HttpStatusCode.Moved, + HttpStatusCode.Redirect, + HttpStatusCode.RedirectMethod, + HttpStatusCode.RedirectKeepVerb, + }.AsReadOnly(); } } diff --git a/src/Faithlife.WebRequests/WebServiceRequestBase.cs b/src/Faithlife.WebRequests/WebServiceRequestBase.cs index 360fe6d..03d07a6 100644 --- a/src/Faithlife.WebRequests/WebServiceRequestBase.cs +++ b/src/Faithlife.WebRequests/WebServiceRequestBase.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Net; using System.Net.Http; @@ -110,7 +111,6 @@ public string Method /// /// True if the request content compression is allowed. /// - /// True if the request content compression is allowed. /// If it significantly reduces the content length, the request content will be compressed /// and the content type will be wrapped with application/x-vnd.logos.compressed; type="...". public bool AllowsRequestContentCompression { get; set; } @@ -126,12 +126,13 @@ public string Method /// public bool DisableAutoRedirect { get; set; } - string? m_method; + private string? m_method; } /// /// A base class for web service requests. /// + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Generic.")] public abstract class WebServiceRequestBase : WebServiceRequestBase { /// @@ -340,23 +341,6 @@ exception is InvalidDataException || exception is ProtocolViolationException; } - const string c_octetStreamContentType = "application/octet-stream"; - } - - internal static class HttpHeadersUtility - { - public static void AddWebHeaders(this HttpHeaders self, WebHeaderCollection headers) - { - foreach (var headerName in headers.AllKeys) - self.Add(headerName, headers[headerName]); - } - - public static WebHeaderCollection ToWebHeaderCollection(this HttpHeaders self) - { - var collection = new WebHeaderCollection(); - foreach (var pair in self) - collection.Add(pair.Key, pair.Value.Join("; ")); - return collection; - } + private const string c_octetStreamContentType = "application/octet-stream"; } } diff --git a/src/Faithlife.WebRequests/WebServiceRequestSettings.cs b/src/Faithlife.WebRequests/WebServiceRequestSettings.cs index e2d4a49..c404946 100644 --- a/src/Faithlife.WebRequests/WebServiceRequestSettings.cs +++ b/src/Faithlife.WebRequests/WebServiceRequestSettings.cs @@ -59,7 +59,6 @@ public WebServiceRequestSettings() /// /// True if 100-Continue behavior should be disabled. /// - /// True if 100-Continue behavior should be disabled. /// May not be supported on all platforms. public bool Disable100Continue { get; set; } diff --git a/src/Faithlife.WebRequests/WebServiceRequestUtility.cs b/src/Faithlife.WebRequests/WebServiceRequestUtility.cs index a332c9a..4d3348e 100644 --- a/src/Faithlife.WebRequests/WebServiceRequestUtility.cs +++ b/src/Faithlife.WebRequests/WebServiceRequestUtility.cs @@ -16,11 +16,8 @@ public static class WebServiceRequestUtility /// /// Sets the Settings of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The settings. - /// The request. - public static TWebServiceRequest WithSettings(this TWebServiceRequest request, WebServiceRequestSettings? settings) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithSettings(this TWebServiceRequest request, WebServiceRequestSettings? settings) + where TWebServiceRequest : WebServiceRequestBase { request.Settings = settings; return request; @@ -29,7 +26,8 @@ public static TWebServiceRequest WithSettings(this TWebServi /// /// Add authorization header to /// - public static TWebServiceRequest WithAuthorizationHeader(this TWebServiceRequest request, string authorizationHeader) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithAuthorizationHeader(this TWebServiceRequest request, string authorizationHeader) + where TWebServiceRequest : WebServiceRequestBase { var additionalHeaders = new WebHeaderCollection(); additionalHeaders[HttpRequestHeader.Authorization] = authorizationHeader; @@ -39,37 +37,28 @@ public static TWebServiceRequest WithAuthorizationHeader(thi /// /// Sets the Accept of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The accept. - /// The request. - public static TWebServiceRequest WithAccept(this TWebServiceRequest request, string accept) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithAccept(this TWebServiceRequest request, string accept) + where TWebServiceRequest : WebServiceRequestBase { request.Accept = accept; return request; } /// - /// Sets the ErrorStrategy of the WebServiceRequest. + /// Sets the AcceptedStatusCodes of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The accepted status codes. - /// The request. - public static TWebServiceRequest WithAcceptedStatusCodes(this TWebServiceRequest request, params HttpStatusCode[] acceptedStatusCodes) where TWebServiceRequest : WebServiceRequest + public static TWebServiceRequest WithAcceptedStatusCodes(this TWebServiceRequest request, params HttpStatusCode[] acceptedStatusCodes) + where TWebServiceRequest : WebServiceRequest { request.AcceptedStatusCodes = acceptedStatusCodes?.AsReadOnly(); return request; } /// - /// Sets the ErrorStrategy of the WebServiceRequest. + /// Sets the AcceptedStatusCodes of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The accepted status codes. - /// The request. - public static TWebServiceRequest WithAcceptedStatusCodes(this TWebServiceRequest request, IEnumerable? acceptedStatusCodes) where TWebServiceRequest : WebServiceRequest + public static TWebServiceRequest WithAcceptedStatusCodes(this TWebServiceRequest request, IEnumerable? acceptedStatusCodes) + where TWebServiceRequest : WebServiceRequest { request.AcceptedStatusCodes = acceptedStatusCodes?.ToList().AsReadOnly(); return request; @@ -78,10 +67,8 @@ public static TWebServiceRequest WithAcceptedStatusCodes(thi /// /// Allows request content compression. /// - /// The type of the web service request. - /// The request. - /// The request. - public static TWebServiceRequest WithContentCompression(this TWebServiceRequest request) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithContentCompression(this TWebServiceRequest request) + where TWebServiceRequest : WebServiceRequestBase { request.AllowsRequestContentCompression = true; return request; @@ -90,11 +77,8 @@ public static TWebServiceRequest WithContentCompression(this /// /// Allows request content compression. /// - /// The type of the web service request. - /// The request. - /// True if content compression should be allowed.. - /// The request. - public static TWebServiceRequest WithContentCompression(this TWebServiceRequest request, bool isEnabled) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithContentCompression(this TWebServiceRequest request, bool isEnabled) + where TWebServiceRequest : WebServiceRequestBase { request.AllowsRequestContentCompression = isEnabled; return request; @@ -103,11 +87,8 @@ public static TWebServiceRequest WithContentCompression(this /// /// Sets the Method of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The method. - /// The request. - public static TWebServiceRequest WithMethod(this TWebServiceRequest request, string method) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithMethod(this TWebServiceRequest request, string method) + where TWebServiceRequest : WebServiceRequestBase { request.Method = method; return request; @@ -116,10 +97,8 @@ public static TWebServiceRequest WithMethod(this TWebService /// /// Sets the Method of the WebServiceRequest to "PATCH". /// - /// The type of the web service request. - /// The request. - /// The request. - public static TWebServiceRequest WithPatchMethod(this TWebServiceRequest request) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithPatchMethod(this TWebServiceRequest request) + where TWebServiceRequest : WebServiceRequestBase { request.Method = "PATCH"; return request; @@ -128,10 +107,8 @@ public static TWebServiceRequest WithPatchMethod(this TWebSe /// /// Sets the Method of the WebServiceRequest to "POST". /// - /// The type of the web service request. - /// The request. - /// The request. - public static TWebServiceRequest WithPostMethod(this TWebServiceRequest request) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithPostMethod(this TWebServiceRequest request) + where TWebServiceRequest : WebServiceRequestBase { request.Method = "POST"; return request; @@ -140,10 +117,8 @@ public static TWebServiceRequest WithPostMethod(this TWebSer /// /// Sets the Method of the WebServiceRequest to "PUT". /// - /// The type of the web service request. - /// The request. - /// The request. - public static TWebServiceRequest WithPutMethod(this TWebServiceRequest request) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithPutMethod(this TWebServiceRequest request) + where TWebServiceRequest : WebServiceRequestBase { request.Method = "PUT"; return request; @@ -152,10 +127,8 @@ public static TWebServiceRequest WithPutMethod(this TWebServ /// /// Sets the Method of the WebServiceRequest to "DELETE". /// - /// The type of the web service request. - /// The request. - /// The request. - public static TWebServiceRequest WithDeleteMethod(this TWebServiceRequest request) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithDeleteMethod(this TWebServiceRequest request) + where TWebServiceRequest : WebServiceRequestBase { request.Method = "DELETE"; return request; @@ -164,10 +137,8 @@ public static TWebServiceRequest WithDeleteMethod(this TWebS /// /// Sets the Method of the WebServiceRequest to "HEAD". /// - /// The type of the web service request. - /// The request. - /// The request. - public static TWebServiceRequest WithHeadMethod(this TWebServiceRequest request) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithHeadMethod(this TWebServiceRequest request) + where TWebServiceRequest : WebServiceRequestBase { request.Method = "HEAD"; return request; @@ -176,11 +147,8 @@ public static TWebServiceRequest WithHeadMethod(this TWebSer /// /// Sets the Content of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The content. - /// The request. - public static TWebServiceRequest WithContent(this TWebServiceRequest request, HttpContent content) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithContent(this TWebServiceRequest request, HttpContent content) + where TWebServiceRequest : WebServiceRequestBase { request.Content = content; return request; @@ -189,11 +157,8 @@ public static TWebServiceRequest WithContent(this TWebServic /// /// Sets the IfMatch of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The ETag. - /// The request. - public static TWebServiceRequest WithIfMatch(this TWebServiceRequest request, string eTag) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithIfMatch(this TWebServiceRequest request, string eTag) + where TWebServiceRequest : WebServiceRequestBase { request.IfMatch = eTag; return request; @@ -202,11 +167,8 @@ public static TWebServiceRequest WithIfMatch(this TWebServic /// /// Sets the IfModifiedSince of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The ifModifiedSince. - /// The request. - public static TWebServiceRequest WithIfModifiedSince(this TWebServiceRequest request, DateTime? ifModifiedSince) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithIfModifiedSince(this TWebServiceRequest request, DateTime? ifModifiedSince) + where TWebServiceRequest : WebServiceRequestBase { request.IfModifiedSince = ifModifiedSince; return request; @@ -215,11 +177,8 @@ public static TWebServiceRequest WithIfModifiedSince(this TW /// /// Sets the IfNoneMatch of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The ETag. - /// The request. - public static TWebServiceRequest WithIfNoneMatch(this TWebServiceRequest request, string eTag) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithIfNoneMatch(this TWebServiceRequest request, string eTag) + where TWebServiceRequest : WebServiceRequestBase { request.IfNoneMatch = eTag; return request; @@ -228,11 +187,8 @@ public static TWebServiceRequest WithIfNoneMatch(this TWebSe /// /// Sets the Timeout of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The timeout. - /// The request. - public static TWebServiceRequest WithTimeout(this TWebServiceRequest request, TimeSpan? timeout) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithTimeout(this TWebServiceRequest request, TimeSpan? timeout) + where TWebServiceRequest : WebServiceRequestBase { request.Timeout = timeout; return request; @@ -241,10 +197,6 @@ public static TWebServiceRequest WithTimeout(this TWebServic /// /// Adds to the Handlers of the WebServiceRequest. /// - /// The type of the web service response. - /// The request. - /// The handler. - /// The request. public static WebServiceRequest WithHandler(this WebServiceRequest request, Func, Task> handler) { request.Handlers.Add(handler); @@ -254,11 +206,8 @@ public static WebServiceRequest WithHandler /// Adds to the Additional Headers of the WebServiceRequest. /// - /// The type of the web service request. - /// The request. - /// The additional headers. - /// The request. - public static TWebServiceRequest WithAdditionalHeaders(this TWebServiceRequest request, WebHeaderCollection headers) where TWebServiceRequest : WebServiceRequestBase + public static TWebServiceRequest WithAdditionalHeaders(this TWebServiceRequest request, WebHeaderCollection headers) + where TWebServiceRequest : WebServiceRequestBase { if (headers is null) throw new ArgumentNullException(nameof(headers)); diff --git a/src/Faithlife.WebRequests/WebServiceResponseHandlerInfo.cs b/src/Faithlife.WebRequests/WebServiceResponseHandlerInfo.cs index 550ff24..c56102a 100644 --- a/src/Faithlife.WebRequests/WebServiceResponseHandlerInfo.cs +++ b/src/Faithlife.WebRequests/WebServiceResponseHandlerInfo.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Net.Http; using System.Threading; @@ -28,7 +29,6 @@ protected WebServiceResponseHandlerInfo(HttpResponseMessage webResponse, Cancell /// /// True if the content has been read from the web response. /// - /// True if the content has been read from the web response. public bool IsContentRead { get; private set; } /// @@ -57,6 +57,7 @@ protected WebServiceResponseHandlerInfo(HttpResponseMessage webResponse, Cancell /// /// Information provided to web service response handlers. /// + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Generic.")] public sealed class WebServiceResponseHandlerInfo : WebServiceResponseHandlerInfo { /// diff --git a/tests/Faithlife.WebRequests.Tests/CreateRequestTests.cs b/tests/Faithlife.WebRequests.Tests/CreateRequestTests.cs index a58b76b..b628b50 100644 --- a/tests/Faithlife.WebRequests.Tests/CreateRequestTests.cs +++ b/tests/Faithlife.WebRequests.Tests/CreateRequestTests.cs @@ -38,24 +38,24 @@ private MockWebServiceClient CreateMockClient() return new MockWebServiceClient(s_baseUri, new JsonWebServiceClientSettings()); } - private static Uri s_baseUri = new Uri("https://testapi.faithlife.com/v1/"); - } - - sealed class MockWebServiceClient : JsonWebServiceClientBase - { - public MockWebServiceClient(Uri baseUri, JsonWebServiceClientSettings clientSettings) - : base(baseUri, clientSettings) - { - } - - public new AutoWebServiceRequest CreateRequest(string relativeUriPattern, IEnumerable> uriParameters) + private sealed class MockWebServiceClient : JsonWebServiceClientBase { - return base.CreateRequest(relativeUriPattern, uriParameters); + public MockWebServiceClient(Uri baseUri, JsonWebServiceClientSettings clientSettings) + : base(baseUri, clientSettings) + { + } + + public new AutoWebServiceRequest CreateRequest(string relativeUriPattern, IEnumerable> uriParameters) + { + return base.CreateRequest(relativeUriPattern, uriParameters); + } + + public new AutoWebServiceRequest CreateRequest(string relativeUriPattern, params string[] parameters) + { + return base.CreateRequest(relativeUriPattern, parameters); + } } - public new AutoWebServiceRequest CreateRequest(string relativeUriPattern, params string[] parameters) - { - return base.CreateRequest(relativeUriPattern, parameters); - } + private static readonly Uri s_baseUri = new Uri("https://testapi.faithlife.com/v1/"); } } diff --git a/tests/Faithlife.WebRequests.Tests/EchoTests.cs b/tests/Faithlife.WebRequests.Tests/EchoTests.cs index eb9b76f..35e4e7e 100644 --- a/tests/Faithlife.WebRequests.Tests/EchoTests.cs +++ b/tests/Faithlife.WebRequests.Tests/EchoTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Net; @@ -12,9 +13,12 @@ namespace Faithlife.WebRequests.Tests { [TestFixture] + [SuppressMessage("Design", "CA1001:Types that own disposable fields should be disposable", Justification = "Test fixture.")] public sealed class EchoTests { [OneTimeSetUp] + [SuppressMessage("Security", "CA5394:Do not use insecure randomness", Justification = "Doesn't need to be secure.")] + [SuppressMessage("Maintainability", "CA1508:Avoid dead conditional code", Justification = "Analyzer bug.")] public void CreateEchoServer() { var random = new Random(); @@ -47,9 +51,9 @@ public void CreateEchoServer() while (m_listener.IsListening) { var getContext = m_listener.GetContextAsync(); - await Task.WhenAny(getContext, waitIndefinitely); + await Task.WhenAny(getContext, waitIndefinitely).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); - var context = await getContext; + var context = await getContext.ConfigureAwait(false); EchoRequestDto? requestDto = null; if (context.Request.HasEntityBody && context.Request.ContentType == "application/json") { @@ -63,10 +67,10 @@ public void CreateEchoServer() var responseDto = new EchoResponseDto { Method = context.Request.HttpMethod, - Path = context.Request.Url.AbsolutePath, + Path = context.Request.Url!.AbsolutePath, Query = context.Request.Url.Query, RequestHeaders = context.Request.Headers.Cast() - .ToDictionary(name => name, name => string.Join("; ", context.Request.Headers.GetValues(name))), + .ToDictionary(name => name, name => string.Join("; ", context.Request.Headers.GetValues(name)!)), Message = requestDto?.Message, }; context.Response.ContentType = "application/json"; @@ -90,7 +94,7 @@ public async Task EchoWithMessageAsync() var request = new AutoWebServiceRequest(new Uri(m_uriPrefix + "echo")) .WithPostMethod() .WithJsonContent(new EchoRequestDto { Message = "hello" }); - var response = await request.GetResponseAsync(); + var response = await request.GetResponseAsync().ConfigureAwait(false); Assert.IsNotNull(response.OK); Assert.AreEqual("POST", response.OK!.Method); Assert.AreEqual("hello", response.OK.Message); @@ -101,7 +105,7 @@ public async Task EchoWithAcceptHeaderAsync() { var request = new AutoWebServiceRequest(new Uri(m_uriPrefix + "echo")) .WithAccept("text/plain"); - var response = await request.GetResponseAsync(); + var response = await request.GetResponseAsync().ConfigureAwait(false); Assert.IsNotNull(response.OK); Assert.AreEqual("text/plain", response.OK!.RequestHeaders!.GetValueOrDefault("Accept")); } @@ -111,7 +115,7 @@ public async Task EchoNoContentAsync() { var request = new AutoWebServiceRequest(new Uri(m_uriPrefix + "echo")) .WithJsonContent(new EchoRequestDto { StatusCode = 204, SendContent = false }); - var response = await request.GetResponseAsync(); + var response = await request.GetResponseAsync().ConfigureAwait(false); Assert.IsTrue(response.NoContent); } @@ -119,9 +123,9 @@ public async Task EchoNoContentAsync() public async Task EchoPlainTextResponseAsync() { var request = new WebServiceRequest(new Uri(m_uriPrefix + "echo")); - var response = await request.GetResponseAsync(); + var response = await request.GetResponseAsync().ConfigureAwait(false); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); - var contentString = await response.Content!.ReadAsStringAsync(); + var contentString = await response.Content!.ReadAsStringAsync().ConfigureAwait(false); Assert.IsNotEmpty(contentString); } @@ -130,8 +134,8 @@ public void EchoInternalError() { var request = new AutoWebServiceRequest(new Uri(m_uriPrefix + "echo")) .WithJsonContent(new EchoRequestDto { StatusCode = 500 }); - var exception = Assert.ThrowsAsync(async () => await request.GetResponseAsync()); - Assert.AreEqual(HttpStatusCode.InternalServerError, exception.ResponseStatusCode); + var exception = Assert.ThrowsAsync(async () => await request.GetResponseAsync().ConfigureAwait(false)); + Assert.AreEqual(HttpStatusCode.InternalServerError, exception!.ResponseStatusCode); } [TestCase(HttpStatusCode.OK)] @@ -149,7 +153,7 @@ public async Task EchoGenericStatusCodeResponse(HttpStatusCode statusCode) var request = new AutoWebServiceRequest(new Uri(m_uriPrefix + "echo")) .WithJsonContent(new EchoRequestDto { StatusCode = (int) statusCode }); - var response = await request.GetResponseAsync(); + var response = await request.GetResponseAsync().ConfigureAwait(false); Assert.AreEqual(statusCode, response.StatusCode); CollectionAssert.IsNotEmpty(response.Headers); } @@ -169,18 +173,18 @@ public async Task EchoGenericStatusCodeAsIntResponse(HttpStatusCode statusCode) var request = new AutoWebServiceRequest(new Uri(m_uriPrefix + "echo")) .WithJsonContent(new EchoRequestDto { StatusCode = (int) statusCode }); - var response = await request.GetResponseAsync(); + var response = await request.GetResponseAsync().ConfigureAwait(false); Assert.AreEqual((int) statusCode, response.StatusCode); } - class EchoRequestDto + private class EchoRequestDto { public string? Message { get; set; } public int? StatusCode { get; set; } public bool? SendContent { get; set; } } - class EchoResponseDto + private class EchoResponseDto { public string? Method { get; set; } public string? Path { get; set; } @@ -189,20 +193,20 @@ class EchoResponseDto public string? Message { get; set; } } - class EchoResponseResponse + private class EchoResponseResponse { public EchoResponseDto? OK { get; set; } public bool NoContent { get; set; } } - class EchoResponseGenericStatusCodeAsIntResponse : AutoWebServiceResponse + private class EchoResponseGenericStatusCodeAsIntResponse : AutoWebServiceResponse { public int StatusCode { get; internal set; } } - HttpListener? m_listener; - string? m_uriPrefix; - Task? m_listenerTask; - CancellationTokenSource? m_listenerCts; + private HttpListener? m_listener; + private string? m_uriPrefix; + private Task? m_listenerTask; + private CancellationTokenSource? m_listenerCts; } }