Skip to content

V109 #2010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2023
Merged

V109 #2010

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions src/RestSharp/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,9 +20,44 @@ namespace RestSharp;
/// </summary>
public enum ParameterType {
/// <summary>
/// Cookie parameter
/// A <see cref="Parameter"/> that will added to the QueryString for GET, DELETE, OPTIONS and HEAD requests; and form for POST and PUT requests.
/// </summary>
GetOrPost, UrlSegment, HttpHeader, RequestBody, QueryString
/// <remarks>
/// See <see cref="GetOrPostParameter"/>.
/// </remarks>
GetOrPost,

/// <summary>
/// A <see cref="Parameter"/> that will be added to part of the url by replacing a <c>{placeholder}</c> within the absolute path.
/// </summary>
/// <remarks>
/// See <see cref="UrlSegmentParameter"/>.
/// </remarks>
UrlSegment,

/// <summary>
/// A <see cref="Parameter"/> that will be added as a request header
/// </summary>
/// <remarks>
/// See <see cref="HeaderParameter"/>.
/// </remarks>
HttpHeader,

/// <summary>
/// A <see cref="Parameter"/> that will be added to the request body
/// </summary>
/// <remarks>
/// See <see cref="BodyParameter"/>.
/// </remarks>
RequestBody,

/// <summary>
/// A <see cref="Parameter"/> that will be added to the query string
/// </summary>
/// <remarks>
/// See <see cref="QueryParameter"/>.
/// </remarks>
QueryString
}

/// <summary>
Expand Down Expand Up @@ -55,4 +91,29 @@ public struct DateFormat {
/// <summary>
/// Status for responses (surprised?)
/// </summary>
public enum ResponseStatus { None, Completed, Error, TimedOut, Aborted }
public enum ResponseStatus {
/// <summary>
/// Not Applicable, for when the Request has not yet been made
/// </summary>
None,

/// <summary>
/// <see cref="ResponseStatus"/> for when the request is passes as a result of <see cref="HttpResponseMessage.IsSuccessStatusCode"/> being true, or when the response is <see cref="HttpStatusCode.NotFound"/>
/// </summary>
Completed,

/// <summary>
/// <see cref="ResponseStatus"/> for when the request fails due as a result of <see cref="HttpResponseMessage.IsSuccessStatusCode"/> being false except for the case when the response is <see cref="HttpStatusCode.NotFound"/>
/// </summary>
Error,

/// <summary>
/// <see cref="ResponseStatus"/> for when the Operation is cancelled due to the request taking longer than the length of time prescribed by <see cref="RestRequest.Timeout"/> or due to the <see cref="HttpClient"/> timing out.
/// </summary>
TimedOut,

/// <summary>
/// <see cref="ResponseStatus"/> for when the Operation is cancelled, due to reasons other than <see cref="TimedOut"/>
/// </summary>
Aborted
}
2 changes: 1 addition & 1 deletion src/RestSharp/Parameters/UrlSegmentParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace RestSharp;

public record UrlSegmentParameter : NamedParameter {
/// <summary>
/// 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.
/// </summary>
/// <param name="name">Parameter name</param>
Expand Down
40 changes: 40 additions & 0 deletions test/RestSharp.Tests/ParametersTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.IO;

namespace RestSharp.Tests;

Expand Down Expand Up @@ -26,10 +27,49 @@ 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 pathTemplate = "/{0}/resource";
var path = String.Format(pathTemplate, "{" + name + "}");
var urlSegmentValue = 1;

var request = new RestRequest(path).AddUrlSegment(name, urlSegmentValue);

var expected = String.Format(pathTemplate, urlSegmentValue);

var client = new RestClient(BaseUrl);

var actual = client.BuildUri(request).AbsolutePath;


expected.Should().BeEquivalentTo(actual);
}

[Fact]
public void AddUrlSegmentModifiesUrlSegmentWithString() {
const string name = "foo";
var pathTemplate = "/{0}/resource";
var path = String.Format(pathTemplate, "{" + name + "}");
var urlSegmentValue = "bar";

var request = new RestRequest(path).AddUrlSegment(name, urlSegmentValue);

var expected = String.Format(pathTemplate, urlSegmentValue);

var client = new RestClient(BaseUrl);

var actual = client.BuildUri(request).AbsolutePath;

expected.Should().BeEquivalentTo(actual);

}
}