Skip to main content

Definition

Assembly: Microsoft.Restier.Breakdance.dll Namespace: Microsoft.Restier.Breakdance Inheritance: System.Object

Syntax

Microsoft.Restier.Breakdance.RestierTestHelpers

Summary

A set of methods that make it easier to pull out Restier runtime components for unit testing.

Remarks

See RestierTestHelperTests.cs for more examples of how to use these methods.

Methods

ExecuteTestRequest

Configures the Restier pipeline in-memory and executes a test request against a given service, returning an HttpResponseMessage for inspection.

Syntax

public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> ExecuteTestRequest<TApi>(System.Net.Http.HttpMethod httpMethod, string host = "http://localhost/", string routeName = "api/tests", string routePrefix = "api/tests/", string resource = null, System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null, string acceptHeader = "application/json;odata.metadata=minimal", Microsoft.AspNet.OData.Query.DefaultQuerySettings defaultQuerySettings = null, System.TimeZoneInfo timeZoneInfo = null, object payload = null, Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = null, bool useEndpointRouting = false) where TApi : Microsoft.Restier.Core.ApiBase

Parameters

NameTypeDescription
httpMethodSystem.Net.Http.HttpMethodThe HttpMethod to use for the request.
hoststringThe protocol and host to connect to in order to run the tests. Must end with a forward-slash. Defaults to “http://localhost/”, and should not normally be changed. NOTE: This should
NOT be the same as any of your actual running environments, and does not require a port assignment in order to function.
routeNamestringThe name that will be assigned to the route in the route configuration dictionary.
routePrefixstringThe string that will be appended in between the Host and the Resource when constructing a URL. NOTE: DO NOT set this to the same URL as your deployment environments.
The prefix is irrelevant, is only for internal testing, and should ONLY be changed if you are testing more than one API in a test method (which is not recommended).
resourcestringThe specific resource on the endpoint that will be called. Must start with a forward-slash.
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-
acceptHeaderstringThe “Accept” header that should be added to the request. Defaults to “application/json;odata.metadata=full”.
defaultQuerySettingsMicrosoft.AspNet.OData.Query.DefaultQuerySettingsA DefaultQuerySettings instabce that defines how OData operations should work. Defaults to everything enabled with a MaxTop of 10.
timeZoneInfoSystem.TimeZoneInfoA TimeZoneInfo instenace specifying what time zone should be used to translate time payloads into. Defaults to Utc.
payloadobjectWhen the httpMethod is Post or Put, this object is serialized to JSON and inserted into the Content.
jsonSerializerSettingsNewtonsoft.Json.JsonSerializerSettingsA JsonSerializerSettings or JsonSerializerOptions instance defining how the payload should be serialized into the request body. Defaults to using Zulu time and will include all properties in the payload, even null ones.
useEndpointRoutingboolOn ASP.NET Core, determines whether or not to use EndpointRouting for the request. Not used on ASP.NET Classic.

Returns

Type: System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> An HttpResponseMessage that contains the managed response for the request for inspection.

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.

GetApiMetadataAsync

Executes a test request against the configured API endpoint and retrieves the content from the /$metadata endpoint.

Syntax

public static System.Threading.Tasks.Task<System.Xml.Linq.XDocument> GetApiMetadataAsync<TApi>(string host = "http://localhost/", string routeName = "api/tests", string routePrefix = "api/tests/", System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null, bool useEndpointRouting = false) where TApi : Microsoft.Restier.Core.ApiBase

Parameters

NameTypeDescription
hoststring-
routeNamestringThe name that will be assigned to the route in the route configuration dictionary.
routePrefixstringThe string that will be appended in between the Host and the Resource when constructing a URL.
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-
useEndpointRoutingboolOn ASP.NET Core, determines whether or not to use EndpointRouting for the request. Not used on ASP.NET Classic.

Returns

Type: System.Threading.Tasks.Task<System.Xml.Linq.XDocument> An XDocument containing the results of the metadata request.

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.

GetModelBuilderHierarchy

Gets a list of fully-qualified builder instances that are registered down the ModelBuilder chain. The order is really important, so this is a great way to troubleshoot.

Syntax

public static System.Threading.Tasks.Task<System.Collections.Generic.List<string>> GetModelBuilderHierarchy<TApi>(string routeName = "api/tests", string routePrefix = "api/tests/", System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null, bool useEndpointRouting = false) where TApi : Microsoft.Restier.Core.ApiBase

Parameters

NameTypeDescription
routeNamestringThe name that will be assigned to the route in the route configuration dictionary.
routePrefixstringThe string that will be appended in between the Host and the Resource when constructing a URL.
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-
useEndpointRoutingboolOn ASP.NET Core, determines whether or not to use EndpointRouting for the request. Not used on ASP.NET Classic.

Returns

Type: System.Threading.Tasks.Task<System.Collections.Generic.List<string>>

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.

GetTestableApiInstance

Retrieves the instance of the Restier API (inheriting from ApiBase from the Dependency Injection container.

Syntax

public static System.Threading.Tasks.Task<TApi> GetTestableApiInstance<TApi>(string routeName = "api/tests", string routePrefix = "api/tests/", System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null, bool useEndpointRouting = false) where TApi : Microsoft.Restier.Core.ApiBase

Parameters

NameTypeDescription
routeNamestringThe name that will be assigned to the route in the route configuration dictionary.
routePrefixstringThe string that will be appendedin between the Host and the Resource when constructing a URL.
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-
useEndpointRoutingboolOn ASP.NET Core, determines whether or not to use EndpointRouting for the request. Not used on ASP.NET Classic.

Returns

Type: System.Threading.Tasks.Task<TApi>

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.

GetTestableHttpClient

Returns a properly configured HttpClient that can make reqests to the in-memory Restier context.

Syntax

public static System.Threading.Tasks.Task<System.Net.Http.HttpClient> GetTestableHttpClient<TApi>(string routeName = "api/tests", string routePrefix = "api/tests/", System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null, bool useEndpointRouting = false) where TApi : Microsoft.Restier.Core.ApiBase

Parameters

NameTypeDescription
routeNamestringThe name that will be assigned to the route in the route configuration dictionary.
routePrefixstringThe string that will be appendedin between the Host and the Resource when constructing a URL.
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-
useEndpointRoutingboolOn ASP.NET Core, determines whether or not to use EndpointRouting for the request. Not used on ASP.NET Classic.

Returns

Type: System.Threading.Tasks.Task<System.Net.Http.HttpClient> A properly configured HttpClient that can make reqests to the in-memory Restier context.

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.

GetTestableInjectedService

Retrieves class instance of type TService from the Dependency Injection container.

Syntax

public static System.Threading.Tasks.Task<TService> GetTestableInjectedService<TApi, TService>(string routeName = "api/tests", string routePrefix = "api/tests/", System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null, bool useEndpointRouting = false) where TApi : Microsoft.Restier.Core.ApiBase where TService : class

Parameters

NameTypeDescription
routeNamestringThe name that will be assigned to the route in the route configuration dictionary.
routePrefixstringThe string that will be appended in between the Host and the Resource when constructing a URL.
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-
useEndpointRoutingboolOn ASP.NET Core, determines whether or not to use EndpointRouting for the request. Not used on ASP.NET Classic.

Returns

Type: System.Threading.Tasks.Task<TService>

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.
  • TService - The type whose instance should be retrieved from the DI container.

GetTestableInjectionContainer

Retrieves the Dependency Injection container that was created as a part of the request pipeline.

Syntax

public static System.Threading.Tasks.Task<System.IServiceProvider> GetTestableInjectionContainer<TApi>(string routeName = "api/tests", string routePrefix = "api/tests/", System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null, bool useEndpointRouting = false) where TApi : Microsoft.Restier.Core.ApiBase

Parameters

NameTypeDescription
routeNamestringThe name that will be assigned to the route in the route configuration dictionary.
routePrefixstringThe string that will be appendedin between the Host and the Resource when constructing a URL.
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-
useEndpointRoutingboolOn ASP.NET Core, determines whether or not to use EndpointRouting for the request. Not used on ASP.NET Classic.

Returns

Type: System.Threading.Tasks.Task<System.IServiceProvider>

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.

GetTestableModelAsync

Retrieves the IEdmModel instance for a given API, whether it used a custom ModelBuilder or the RestierModelBuilder.

Syntax

public static System.Threading.Tasks.Task<Microsoft.OData.Edm.IEdmModel> GetTestableModelAsync<TApi>(string routeName = "api/tests", string routePrefix = "api/tests/", System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null, bool useEndpointRouting = false) where TApi : Microsoft.Restier.Core.ApiBase

Parameters

NameTypeDescription
routeNamestringThe name that will be assigned to the route in the route configuration dictionary.
routePrefixstringThe string that will be appended in between the Host and the Resource when constructing a URL.
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-
useEndpointRoutingboolOn ASP.NET Core, determines whether or not to use EndpointRouting for the request. Not used on ASP.NET Classic.

Returns

Type: System.Threading.Tasks.Task<Microsoft.OData.Edm.IEdmModel> An IEdmModel instance containing the model used to configure both OData and Restier processing.

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.

GetTestableRestierConfiguration

Retrieves an HttpConfiguration instance that has been configured to execute a given Restier API, along with settings suitable for easy troubleshooting.</see>

Syntax

public static System.Threading.Tasks.Task<System.Web.Http.HttpConfiguration> GetTestableRestierConfiguration<TApi>(string routeName = "api/tests", string routePrefix = "api/tests/", Microsoft.AspNet.OData.Query.DefaultQuerySettings defaultQuerySettings = null, System.TimeZoneInfo timeZoneInfo = null, System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null) where TApi : Microsoft.Restier.Core.ApiBase

Parameters

NameTypeDescription
routeNamestringThe name that will be assigned to the route in the route configuration dictionary.
routePrefixstringThe string that will be appendedin between the Host and the Resource when constructing a URL.
defaultQuerySettingsMicrosoft.AspNet.OData.Query.DefaultQuerySettingsA DefaultQuerySettings instabce that defines how OData operations should work. Defaults to everything enabled with a MaxTop of 10.
timeZoneInfoSystem.TimeZoneInfoA TimeZoneInfo instenace specifying what time zone should be used to translate time payloads into. Defaults to Utc.
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-

Returns

Type: System.Threading.Tasks.Task<System.Web.Http.HttpConfiguration> An HttpConfiguration instance

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.

WriteCurrentApiMetadata

Syntax

public static System.Threading.Tasks.Task WriteCurrentApiMetadata<TApi>(string sourceDirectory = "", string suffix = "ApiMetadata", System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> serviceCollection = null, bool useEndpointRouting = false) where TApi : Microsoft.Restier.Core.ApiBase

Parameters

NameTypeDescription
sourceDirectorystring-
suffixstring-
serviceCollectionSystem.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>-
useEndpointRoutingboolOn ASP.NET Core, determines whether or not to use EndpointRouting for the request. Not used on ASP.NET Classic.

Returns

Type: System.Threading.Tasks.Task

Type Parameters

  • TApi - The class inheriting from ApiBase that implements the Restier API to test.