From 1f19e18bd2ae88cf417efbe6db9367d14639cafd Mon Sep 17 00:00:00 2001 From: Eddie Lichtman Date: Tue, 31 Jan 2023 15:43:17 -0600 Subject: [PATCH 1/3] Fixes some documentation Adds more Parameter Tests --- src/RestSharp/Enum.cs | 67 ++++++++++++++++++- .../Parameters/UrlSegmentParameter.cs | 2 +- test/RestSharp.Tests/ParametersTests.cs | 46 +++++++++++++ 3 files changed, 111 insertions(+), 4 deletions(-) diff --git a/src/RestSharp/Enum.cs b/src/RestSharp/Enum.cs index e046d4864..bf7c89c28 100644 --- a/src/RestSharp/Enum.cs +++ b/src/RestSharp/Enum.cs @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +using System.Net; namespace RestSharp; @@ -19,9 +20,44 @@ namespace RestSharp; /// public enum ParameterType { /// - /// Cookie parameter + /// A that will added to the QueryString for GET, DELETE, OPTIONS and HEAD requests; and form for POST and PUT requests. /// - GetOrPost, UrlSegment, HttpHeader, RequestBody, QueryString + /// + /// See . + /// + GetOrPost, + + /// + /// A that will be added to part of the url by replacing a {placeholder} within the absolute path. + /// + /// + /// See . + /// + UrlSegment, + + /// + /// A that will be added as a request header + /// + /// + /// See . + /// + HttpHeader, + + /// + /// A that will be added to the request body + /// + /// + /// See . + /// + RequestBody, + + /// + /// A that will be added to the query string + /// + /// + /// See . + /// + QueryString } /// @@ -55,4 +91,29 @@ public struct DateFormat { /// /// Status for responses (surprised?) /// -public enum ResponseStatus { None, Completed, Error, TimedOut, Aborted } \ No newline at end of file +public enum ResponseStatus { + /// + /// Not Applicable, for when the Request has not yet been made + /// + None, + + /// + /// for when the request is passes as a result of being true, or when the response is + /// + Completed, + + /// + /// for when the request fails due as a result of being false except for the case when the response is + /// + Error, + + /// + /// for when the Operation is cancelled due to the request taking longer than the length of time prescribed by or due to the timing out. + /// + TimedOut, + + /// + /// for when the Operation is cancelled, due to reasons other than + /// + Aborted +} \ No newline at end of file diff --git a/src/RestSharp/Parameters/UrlSegmentParameter.cs b/src/RestSharp/Parameters/UrlSegmentParameter.cs index 04b60d65c..deb0b3bdc 100644 --- a/src/RestSharp/Parameters/UrlSegmentParameter.cs +++ b/src/RestSharp/Parameters/UrlSegmentParameter.cs @@ -16,7 +16,7 @@ namespace RestSharp; public record UrlSegmentParameter : NamedParameter { /// - /// Instantiates a new query parameter instance that will be added to the request URL part of the query string. + /// Instantiates a new query parameter instance that will be added to the request URL by replacing part of the absolute path. /// The request resource should have a placeholder {name} that will be replaced with the parameter value when the request is made. /// /// Parameter name diff --git a/test/RestSharp.Tests/ParametersTests.cs b/test/RestSharp.Tests/ParametersTests.cs index 8bfc6edf3..6d8f84685 100644 --- a/test/RestSharp.Tests/ParametersTests.cs +++ b/test/RestSharp.Tests/ParametersTests.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.IO; namespace RestSharp.Tests; @@ -26,10 +27,55 @@ public void AddDefaultHeadersUsingDictionary() { public void AddUrlSegmentWithInt() { const string name = "foo"; + var request = new RestRequest().AddUrlSegment(name, 1); var actual = request.Parameters.FirstOrDefault(x => x.Name == name); var expected = new UrlSegmentParameter(name, "1"); expected.Should().BeEquivalentTo(actual); } + + [Fact] + public void AddUrlSegmentModifiesUrlSegmentWithInt() { + const string name = "foo"; + var path = $"/{{{name}}}/resource"; + var urlSegmentValue = 1; + + var request = new RestRequest(path).AddUrlSegment(name, urlSegmentValue); + + var expected = GetFormattedUrlSegmentString(path, request.Parameters); + + var client = new RestClient(BaseUrl); + + var actual = client.BuildUri(request).AbsolutePath; + + + expected.Should().BeEquivalentTo(actual); + } + + [Fact] + public void AddUrlSegmentModifiesUrlSegmentWithString() { + const string name = "foo"; + var path = $"/{{{name}}}/resource"; + var urlSegmentValue = "bar"; + + var request = new RestRequest(path).AddUrlSegment(name, urlSegmentValue); + + var expected = GetFormattedUrlSegmentString(path, request.Parameters); + + var client = new RestClient(BaseUrl); + + var actual = client.BuildUri(request).AbsolutePath; + + expected.Should().BeEquivalentTo(actual); + + } + + private string GetFormattedUrlSegmentString(string str, ParametersCollection parametersCollection) { + var parameters = parametersCollection + .Where(x => x.Type == ParameterType.UrlSegment) + .ToDictionary(x => "{" + x.Name + "}", x => x.Value); + + return parameters.Aggregate(str, (current, parameter) => current.Replace(parameter.Key, parameter.Value.ToString())); + } } \ No newline at end of file From c75ba2b1a0af36fa9ec0d89846cd1e553f85a9ab Mon Sep 17 00:00:00 2001 From: Eddie Lichtman Date: Wed, 1 Feb 2023 08:57:10 -0600 Subject: [PATCH 2/3] Makes the new tests more readable and understandable --- test/RestSharp.Tests/ParametersTests.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/test/RestSharp.Tests/ParametersTests.cs b/test/RestSharp.Tests/ParametersTests.cs index 6d8f84685..719e96ed5 100644 --- a/test/RestSharp.Tests/ParametersTests.cs +++ b/test/RestSharp.Tests/ParametersTests.cs @@ -38,12 +38,13 @@ public void AddUrlSegmentWithInt() { [Fact] public void AddUrlSegmentModifiesUrlSegmentWithInt() { const string name = "foo"; - var path = $"/{{{name}}}/resource"; + var pathTemplate = "/{0}/resource"; + var path = String.Format(pathTemplate, "{" + name + "}"); var urlSegmentValue = 1; var request = new RestRequest(path).AddUrlSegment(name, urlSegmentValue); - var expected = GetFormattedUrlSegmentString(path, request.Parameters); + var expected = String.Format(pathTemplate, urlSegmentValue); var client = new RestClient(BaseUrl); @@ -56,12 +57,13 @@ public void AddUrlSegmentModifiesUrlSegmentWithInt() { [Fact] public void AddUrlSegmentModifiesUrlSegmentWithString() { const string name = "foo"; - var path = $"/{{{name}}}/resource"; + var pathTemplate = "/{0}/resource"; + var path = String.Format(pathTemplate, "{" + name + "}"); var urlSegmentValue = "bar"; var request = new RestRequest(path).AddUrlSegment(name, urlSegmentValue); - var expected = GetFormattedUrlSegmentString(path, request.Parameters); + var expected = String.Format(pathTemplate, urlSegmentValue); var client = new RestClient(BaseUrl); @@ -70,12 +72,4 @@ public void AddUrlSegmentModifiesUrlSegmentWithString() { expected.Should().BeEquivalentTo(actual); } - - private string GetFormattedUrlSegmentString(string str, ParametersCollection parametersCollection) { - var parameters = parametersCollection - .Where(x => x.Type == ParameterType.UrlSegment) - .ToDictionary(x => "{" + x.Name + "}", x => x.Value); - - return parameters.Aggregate(str, (current, parameter) => current.Replace(parameter.Key, parameter.Value.ToString())); - } } \ No newline at end of file From 4d4927aa4c842b1e13a192d464083690ab70904e Mon Sep 17 00:00:00 2001 From: Eddie Lichtman Date: Wed, 22 Feb 2023 10:49:05 -0600 Subject: [PATCH 3/3] Fixes Documentation per request --- src/RestSharp/Enum.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RestSharp/Enum.cs b/src/RestSharp/Enum.cs index bf7c89c28..fd584bc8c 100644 --- a/src/RestSharp/Enum.cs +++ b/src/RestSharp/Enum.cs @@ -28,7 +28,7 @@ public enum ParameterType { GetOrPost, /// - /// A that will be added to part of the url by replacing a {placeholder} within the absolute path. + /// A that will be added to part of the url by replacing a {placeholder} within the absolute path. /// /// /// See .