PeanutButter
Loading...
Searching...
No Matches
PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder Class Reference

Builds an http request. More...

Inheritance diagram for PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder:
PeanutButter.TestUtils.AspNetCore.Builders.RandomizableBuilder< TBuilder, TSubject > PeanutButter.TestUtils.AspNetCore.Builders.Builder< TBuilder, TSubject >

Public Member Functions

 HttpRequestBuilder ()
void WithJsonEncoder (Func< object, string > encoder)
 Sets up a JSON encoder to use on this instance of HttpRequestBuilder only, to be used when generating a request body as JSON from an object.
override HttpRequestBuilder Randomize ()
 Randomizes the output.
HttpRequestBuilder WithRemoteAddress (string ipAddress)
 Sets the HttpContext.Connection.RemoteIpAddress value.
HttpRequestBuilder WithRemoteAddress (IPAddress address)
 Sets the HttpContext.Connection.RemoteIpAddress value.
HttpRequestBuilder WithRandomForm ()
 Adds a random form to the request.
HttpRequestBuilder WithRandomMethod ()
 Selects a random http method for the request.
HttpRequestBuilder WithRandomScheme ()
 Selects a random scheme (http|https) for the request.
HttpRequestBuilder WithRandomPath ()
 Selects a random path for the request.
HttpRequestBuilder WithRandomHost ()
 Selects a random hostname for the request.
HttpRequestBuilder WithRandomPort ()
 Selects a random port (80-10000) for the request.
HttpRequestBuilder WithRandomHeaders ()
 Adds some random X- prefixed headers for the request.
HttpRequestBuilder WithRandomCookies ()
 Adds some random cookies to the request.
HttpRequestBuilder WithBody (string body)
 Sets the body for the request. If possible, form elements are derived from the body.
HttpRequestBuilder WithJsonBody< T > (T data)
HttpRequestBuilder WithBody (byte[] body)
 Sets the body for the request. If possible, form elements are derived from the body.
HttpRequestBuilder WithBody (Stream body)
 Sets the body for the request. If possible, form elements are derived from the body.
HttpRequestBuilder WithCookie (string name, string value)
 Sets a cookie on the request. Will overwrite an existing cookie with the same name.
HttpRequestBuilder WithCookies (IDictionary< string, string > cookies)
 Sets a bunch of cookies on the request. Will overwrite existing cookies with the same name. Will not remove any other existing cookies.
HttpRequestBuilder WithNoCookies ()
 Clears cookies on the request.
HttpRequestBuilder WithCookies (IRequestCookieCollection cookies)
 Sets the cookie collection on the request.
HttpRequestBuilder WithNoHeaders ()
 Clears headers on the request.
HttpRequestBuilder WithHeader (string header, string value)
 Sets a header on the request. Any existing header with the same name is overwritten.
HttpRequestBuilder WithHeaders (IDictionary< string, string > headers)
 Sets a bunch of headers on the request. Existing cookies with the same names are overwritten. Other existing cookies are left intact.
HttpRequestBuilder WithHeaders (IHeaderDictionary headers)
 Sets the header dictionary on the request.
HttpRequestBuilder WithQuery (IQueryCollection query)
 Sets the query collection on the request.
HttpRequestBuilder WithQueryParameter (string key, string value)
 Sets a query parameter on the request.
HttpRequestBuilder WithQueryParameters (IDictionary< string, string > parameters)
 Set multiple query parameters from a dictionary.
HttpRequestBuilder WithPath (string path)
HttpRequestBuilder WithBasePath (string basePath)
 Sets the base path on the request.
HttpRequestBuilder WithHost (HostString host)
 Sets the host on the path.
HttpRequestBuilder WithPort (int port)
 Sets the port on the request.
HttpRequestBuilder WithHost (string host)
 Sets the host on the request.
HttpRequestBuilder WithQueryString (string queryString)
 Sets the query string on the request.
HttpRequestBuilder WithQueryString (QueryString queryString)
 Sets the query string on the request.
HttpRequestBuilder WithScheme (string scheme)
 Sets the scheme on the request.
HttpRequestBuilder WithMethod (HttpMethod method)
 Sets the method on the request.
HttpRequestBuilder WithMethod (string method)
 Sets the method on the request.
HttpRequestBuilder WithHttpContext (HttpContext context)
 Sets the http context on the request.
HttpRequestBuilder WithHttpContext (Func< HttpContext > accessor)
 Sets the http context accessor on the request.
HttpRequestBuilder WithForm (IFormCollection formCollection)
 Sets the form on the request.
HttpRequestBuilder WithFormField (string key, string value)
 Set a field on the form of the request.
HttpRequestBuilder WithFormFields (IDictionary< string, string > fields)
 Sets a collection of fields on the form of the request.
HttpRequestBuilder WithFormFile (IFormFile file)
 Adds a file to the form of the request.
HttpRequestBuilder WithFormFile (string content, string name, string fileName)
 Adds a form file with string content and the provided name and filename.
HttpRequestBuilder WithFormFile (byte[] content, string name, string fileName)
 Adds a form file with binary content and the provided name and filename.
HttpRequestBuilder WithUrl (string url)
 Set the full url for the request.
HttpRequestBuilder WithUrl (Uri url)
 Set the full url for the request.
HttpRequestBuilder WithFormFile (Stream content, string name, string fileName)
 Adds a form file with text content and the provided name and filename.
HttpRequestBuilder WithContentType (string contentType)
 Sets the Content-Type header for the request. Note that this shouldn't be necessary most of the time - automatic content type detection is provided for requests with forms or JSON bodies.
HttpRequestBuilder WithSelfOrigin ()
 Sets the origin header to be the root of the request's uri.
HttpRequestBuilder WithRandomUrl ()
 Sets a random url for this request.
HttpRequestBuilder WithOrigin (string origin)
 Sets the Origin header.
HttpRequestBuilder WithHttpContextMutator (Action< FakeHttpContext > mutator)
 Facilitates easier http context mutations.
Public Member Functions inherited from PeanutButter.TestUtils.AspNetCore.Builders.RandomizableBuilder< TBuilder, TSubject >
TBuilder Randomize ()
 Derivatives must implement this so that BuildRandom can work.
Public Member Functions inherited from PeanutButter.TestUtils.AspNetCore.Builders.Builder< TBuilder, TSubject >
TBuilder With (Action< TSubject > action)
 Adds a mutator for the artifact.
virtual TSubject Build ()
 Builds the subject artifact.
 Mutator (Action< T > action, string identifier)

Static Public Member Functions

static string DefaultJsonEncoder< T > (T value)
 Unless otherwise specified, this will be used when encoding json, eg when creating a JSON request from an object. Under the hood, it uses System.Text.Json.JsonSerializer with default options, which may or may not suit your workload.
static void SetDefaultJsonEncoder (Func< object, string > encoder)
 Allows setting the default JSON encoder for all HttpRequestBuilder instances.
Static Public Member Functions inherited from PeanutButter.TestUtils.AspNetCore.Builders.RandomizableBuilder< TBuilder, TSubject >
static TSubject BuildRandom ()
 Builds a random variant output artifact.
Static Public Member Functions inherited from PeanutButter.TestUtils.AspNetCore.Builders.Builder< TBuilder, TSubject >
static TBuilder Create ()
 Returns a new instance of the builder.
static TSubject BuildDefault ()
 Builds the default output artifact.

Protected Member Functions

override HttpRequest ConstructEntity ()
 Constructs the fake http request.
Protected Member Functions inherited from PeanutButter.TestUtils.AspNetCore.Builders.Builder< TBuilder, TSubject >
TBuilder With (Action< TSubject > action, string identifier)
 Adds an identified mutator for the artifact - if a mutator with the same identity already exists, it will be removed.
TBuilder With< TCast > (Action< TCast > action)
 Adds a mutator for the artifact, cast to TCast.
TBuilder With< TCast > (Action< TCast > action, string identifier)
 Adds an identified mutator for the artifact, cast to TCast.
TBuilder WithRandomTimes (Action< TSubject > action)
 Applies the given mutator a random (1-4) number of times.
TBuilder WithRandomTimes< TCast > (Action< TCast > action)
 Applies the given mutator on a cast item a random (1-4) number of times.
TBuilder WithPreCursor (Action toRun)
 Run a precursor before your entity is constructed - use when your entity requires constructor parameters you'd like to make user-tweakable via builder methods.
TBuilder WithPostBuild (Action< TSubject > action)
 Add post-build actions.

Additional Inherited Members

Static Protected Member Functions inherited from PeanutButter.TestUtils.AspNetCore.Builders.Builder< TBuilder, TSubject >
static void WarnIf (bool condition, string message)
 Print a traced warning if the condition is found to be false.
static void ErrorIf (bool condition, string message)
 Print a traced warning if the condition is found to be false.
Protected Attributes inherited from PeanutButter.TestUtils.AspNetCore.Builders.Builder< TBuilder, TSubject >
TSubject CurrentEntity
 During build, CurrentEntity will be set to the currently-building entity. You may implement actualizer(s) in your derivative to pull this value in lazily to consumers.
Properties inherited from PeanutButter.TestUtils.AspNetCore.Builders.Builder< TBuilder, TSubject >
Action< T > Action [get]
string Identifier [get]

Detailed Description

Builds an http request.

Member Function Documentation

◆ ConstructEntity()

override HttpRequest PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.ConstructEntity ( )
protectedvirtual

Constructs the fake http request.

Returns

Reimplemented from PeanutButter.TestUtils.AspNetCore.Builders.Builder< TBuilder, TSubject >.

◆ DefaultJsonEncoder< T >()

string PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.DefaultJsonEncoder< T > ( T value)
static

Unless otherwise specified, this will be used when encoding json, eg when creating a JSON request from an object. Under the hood, it uses System.Text.Json.JsonSerializer with default options, which may or may not suit your workload.

Parameters
value
Template Parameters
T
Returns

◆ Randomize()

override HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.Randomize ( )

Randomizes the output.

Returns

◆ SetDefaultJsonEncoder()

void PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.SetDefaultJsonEncoder ( Func< object, string > encoder)
static

Allows setting the default JSON encoder for all HttpRequestBuilder instances.

Parameters
encoder

◆ WithBasePath()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithBasePath ( string basePath)

Sets the base path on the request.

Parameters
basePath
Returns

◆ WithBody() [1/3]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithBody ( byte[] body)

Sets the body for the request. If possible, form elements are derived from the body.

Parameters
body
Returns

◆ WithBody() [2/3]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithBody ( Stream body)

Sets the body for the request. If possible, form elements are derived from the body.

Parameters
body
Returns

◆ WithBody() [3/3]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithBody ( string body)

Sets the body for the request. If possible, form elements are derived from the body.

Parameters
body
Returns

◆ WithContentType()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithContentType ( string contentType)

Sets the Content-Type header for the request. Note that this shouldn't be necessary most of the time - automatic content type detection is provided for requests with forms or JSON bodies.

Parameters
contentType
Returns

◆ WithCookie()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithCookie ( string name,
string value )

Sets a cookie on the request. Will overwrite an existing cookie with the same name.

Parameters
name
value
Returns

◆ WithCookies() [1/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithCookies ( IDictionary< string, string > cookies)

Sets a bunch of cookies on the request. Will overwrite existing cookies with the same name. Will not remove any other existing cookies.

Parameters
cookies
Returns
Exceptions
ArgumentNullException

◆ WithCookies() [2/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithCookies ( IRequestCookieCollection cookies)

Sets the cookie collection on the request.

Parameters
cookies
Returns

◆ WithForm()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithForm ( IFormCollection formCollection)

Sets the form on the request.

Parameters
formCollection
Returns

◆ WithFormField()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithFormField ( string key,
string value )

Set a field on the form of the request.

Parameters
key
value
Returns

◆ WithFormFields()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithFormFields ( IDictionary< string, string > fields)

Sets a collection of fields on the form of the request.

Parameters
fields
Returns
Exceptions
ArgumentNullException

◆ WithFormFile() [1/4]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithFormFile ( byte[] content,
string name,
string fileName )

Adds a form file with binary content and the provided name and filename.

Parameters
content
name
fileName
Returns

◆ WithFormFile() [2/4]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithFormFile ( IFormFile file)

Adds a file to the form of the request.

Parameters
file
Returns

◆ WithFormFile() [3/4]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithFormFile ( Stream content,
string name,
string fileName )

Adds a form file with text content and the provided name and filename.

Parameters
content
name
fileName
Returns

◆ WithFormFile() [4/4]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithFormFile ( string content,
string name,
string fileName )

Adds a form file with string content and the provided name and filename.

Parameters
content
name
fileName
Returns

◆ WithHeader()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithHeader ( string header,
string value )

Sets a header on the request. Any existing header with the same name is overwritten.

Parameters
header
value
Returns

◆ WithHeaders() [1/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithHeaders ( IDictionary< string, string > headers)

Sets a bunch of headers on the request. Existing cookies with the same names are overwritten. Other existing cookies are left intact.

Parameters
headers
Returns
Exceptions
ArgumentNullException

◆ WithHeaders() [2/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithHeaders ( IHeaderDictionary headers)

Sets the header dictionary on the request.

Parameters
headers
Returns

◆ WithHost() [1/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithHost ( HostString host)

Sets the host on the path.

Parameters
host
Returns

◆ WithHost() [2/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithHost ( string host)

Sets the host on the request.

Parameters
host
Returns

◆ WithHttpContext() [1/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithHttpContext ( Func< HttpContext > accessor)

Sets the http context accessor on the request.

Parameters
accessor
Returns

◆ WithHttpContext() [2/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithHttpContext ( HttpContext context)

Sets the http context on the request.

Parameters
context
Returns

◆ WithHttpContextMutator()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithHttpContextMutator ( Action< FakeHttpContext > mutator)

Facilitates easier http context mutations.

Parameters
mutator
Returns

◆ WithJsonBody< T >()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithJsonBody< T > ( T data)
Parameters
data
Template Parameters
T
Returns

◆ WithJsonEncoder()

void PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithJsonEncoder ( Func< object, string > encoder)

Sets up a JSON encoder to use on this instance of HttpRequestBuilder only, to be used when generating a request body as JSON from an object.

Parameters
encoder

◆ WithMethod() [1/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithMethod ( HttpMethod method)

Sets the method on the request.

Parameters
method
Returns

◆ WithMethod() [2/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithMethod ( string method)

Sets the method on the request.

Parameters
method
Returns

◆ WithNoCookies()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithNoCookies ( )

Clears cookies on the request.

Returns

◆ WithNoHeaders()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithNoHeaders ( )

Clears headers on the request.

Returns

◆ WithOrigin()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithOrigin ( string origin)

Sets the Origin header.

Parameters
origin
Returns

◆ WithPath()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithPath ( string path)
Parameters
path
Returns

◆ WithPort()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithPort ( int port)

Sets the port on the request.

Parameters
port
Returns

◆ WithQuery()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithQuery ( IQueryCollection query)

Sets the query collection on the request.

Parameters
query
Returns

◆ WithQueryParameter()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithQueryParameter ( string key,
string value )

Sets a query parameter on the request.

Parameters
key
value
Returns

◆ WithQueryParameters()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithQueryParameters ( IDictionary< string, string > parameters)

Set multiple query parameters from a dictionary.

Parameters
parameters
Returns

◆ WithQueryString() [1/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithQueryString ( QueryString queryString)

Sets the query string on the request.

Parameters
queryString
Returns

◆ WithQueryString() [2/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithQueryString ( string queryString)

Sets the query string on the request.

Parameters
queryString
Returns

◆ WithRandomCookies()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRandomCookies ( )

Adds some random cookies to the request.

Returns

◆ WithRandomForm()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRandomForm ( )

Adds a random form to the request.

Returns

◆ WithRandomHeaders()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRandomHeaders ( )

Adds some random X- prefixed headers for the request.

Returns

◆ WithRandomHost()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRandomHost ( )

Selects a random hostname for the request.

Returns

◆ WithRandomMethod()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRandomMethod ( )

Selects a random http method for the request.

Returns

◆ WithRandomPath()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRandomPath ( )

Selects a random path for the request.

Returns

◆ WithRandomPort()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRandomPort ( )

Selects a random port (80-10000) for the request.

Returns

◆ WithRandomScheme()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRandomScheme ( )

Selects a random scheme (http|https) for the request.

Returns

◆ WithRandomUrl()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRandomUrl ( )

Sets a random url for this request.

Returns

◆ WithRemoteAddress() [1/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRemoteAddress ( IPAddress address)

Sets the HttpContext.Connection.RemoteIpAddress value.

Parameters
address
Returns

◆ WithRemoteAddress() [2/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithRemoteAddress ( string ipAddress)

Sets the HttpContext.Connection.RemoteIpAddress value.

Parameters
ipAddress
Returns

◆ WithScheme()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithScheme ( string scheme)

Sets the scheme on the request.

Parameters
scheme
Returns

◆ WithSelfOrigin()

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithSelfOrigin ( )

Sets the origin header to be the root of the request's uri.

Returns

◆ WithUrl() [1/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithUrl ( string url)

Set the full url for the request.

Parameters
url
Returns

◆ WithUrl() [2/2]

HttpRequestBuilder PeanutButter.TestUtils.AspNetCore.Builders.HttpRequestBuilder.WithUrl ( Uri url)

Set the full url for the request.

Parameters
url
Returns

The documentation for this class was generated from the following file:
  • source/TestUtils/PeanutButter.TestUtils.AspNetCore/Builders/HttpRequestBuilder.cs