Class HttpRequestMessageBuilder
- Namespace
- CSF.Screenplay.WebApis
- Assembly
- CSF.Screenplay.WebApis.dll
An object which can create an HttpRequestMessage for use with an HTTP client.
public record HttpRequestMessageBuilder : IHasName, IFormattableValue, IEquatable<HttpRequestMessageBuilder>
- Inheritance
-
HttpRequestMessageBuilder
- Implements
- Derived
- Inherited Members
Remarks
Please note that this type differs in form depending upon the .NET version under which it is consumed.
If consuming this type from logic which targets .NET Standard 2.0 or .NET Framework 4.6.2 then this type is a class.
In that scenario its properties are mutable and the developer should take care to ensure that they do not mutate/alter its
state inadvertently. Developers should ensure that they manually copy the state from the current instance into a new instance
instead of modifying an existing instance.
In these target frameworks, a Clone
method has been provided to assist with this.
When consuming this from .NET 5 or higher, this type is instead a record and is immutable by design. All properties are init-only. Additionally, in .NET 5+, some additional properties are available, supporting features of HttpRequestMessage which are unavailable in lower .NET versions. When using .NET 5, developers may use nondestructive mutation with the with keyword/expression to create a copy of the current instance but with some differences.
Properties
Content
Gets or sets the content which will be sent with the request: the request body.
public HttpContent Content { get; init; }
Property Value
Headers
Gets or sets the HTTP headers which will be sent with the request, corresponding to Headers.
public NameValueRecordCollection<string, string> Headers { get; init; }
Property Value
Method
Gets or sets the HTTP method (aka "verb") which shall be used to send the request.
public HttpMethod Method { get; init; }
Property Value
Name
Gets the human-readable name of the current object.
public string Name { get; init; }
Property Value
Remarks
null is strongly discouraged here. All types which implement IHasName should return a non-null response from this property.
Options
Gets or sets the HTTP web request options, corresponding to Options.
public NameValueRecordCollection<string, object> Options { get; init; }
Property Value
Remarks
Note that this property is supported only for .NET 5 and above. It is unavailable in lower .NET versions, including .NET Standard and .NET Framework.
RequestUri
Gets or sets the Uri to which the HTTP request shall be sent.
public Uri RequestUri { get; init; }
Property Value
Timeout
Gets or sets an optional timeout duration for requests built from this builder.
public TimeSpan? Timeout { get; init; }
Property Value
Remarks
If this set to a non-null value, then the HTTP client used to make the request will include cancellation after an amount of time (equal to this timespan) has passed. This logic is handled within the MakeWebApiRequests action. If this action is not used then this timeout might not be honoured.
Version
Gets or sets the HTTP version which shall be used by the message.
public Version Version { get; init; }
Property Value
Remarks
The documentation for the Version property notes that it would default to 2.0
for
.NET Core 2.1 or 2.2, and defaults to 1.1
for all other versions of .NET or .NET Framework.
This property will always default to 1.1
regardless of the target framework.
VersionPolicy
Gets or sets the HTTP version policy, corresponding to VersionPolicy.
public HttpVersionPolicy VersionPolicy { get; init; }
Property Value
Remarks
Note that this property is supported only for .NET 5 and above. It is unavailable in lower .NET versions, including .NET Standard and .NET Framework.
Methods
CreateRequestMessage()
Creates and returns an HTTP request message.
public HttpRequestMessage CreateRequestMessage()
Returns
- HttpRequestMessage
An HTTP request message
Remarks
The request message is typically created from the state of the current object instance. Multiple usages of this method must create a new request message each time. Unless some state has been altered between usages, though, each of these messages is likely to have the same data/property values.
If the state of the current instance does not specify a Method; IE the method is null, then Get will be used.
FormatForReport()
Gets a human-readable formatted string which represents the current object instance, suitable to be used in a Screenplay report.
public string FormatForReport()
Returns
- string
A formatted string which represents the current instance.