/// <summary> /// Default configuration for asserting an Http Json end point. /// Sets the Accept and Content-Type to be application/json, uses the HttpClient and the JsonDeserialiser. /// </summary> /// <returns></returns> public static RestConfiguration JsonDefault() { var restDefaults = new RestConfiguration { RequestDefaults = { Headers = new Dictionary <string, IList <string> > { { "Accept", new List <string> { "application/json" } }, { "Content-Type", new List <string> { "application/json" } } }, TimeoutInSeconds = DefaultValues.TimeOutInSeconds } }; //TODO this may need to move out if we want to make the httpclient library an optional dependency to help with other .net implementations (i.e. core, phone, etc) restDefaults.UsingWebApiClient(); restDefaults.UseJsonResponseDeserialiser(); return(restDefaults); }
/// <summary> /// Prepare a POST request to the URL /// </summary> /// <param name="url">URL to send the request to</param> /// <param name="configuration">Configuration to apply to the request - null to use <see cref="Configuration"/></param> /// <returns></returns> public static RestRequest PostToUrl(Uri url, RestConfiguration configuration = null) { if (url == null) { throw new ArgumentNullException(nameof(url), ErrorMessages.NoUrl); } return(PrepareToUrl(HttpVerb.Post, url, configuration)); }
/// <summary> /// Validates the execution of the rules by outputting the results in the Console /// </summary> /// <param name="config"></param> /// <returns></returns> public static RestConfiguration WithConsoleAssertion(this RestConfiguration config) { if (config == null) { throw new ArgumentNullException(nameof(config), ErrorMessages.NoConfiguration); } config.Assertion = new ConsoleAssertion(); return(config); }
/// <summary> /// Sets up a custom authorisation header. You need to fill the value of the header. /// </summary> /// <param name="config"></param> /// <param name="authorisationHeaderValue">Value to be given as authorisation. This extension does not add any values to the one provided.</param> /// <returns></returns> public static RestConfiguration WithAuthorization(this RestConfiguration config, string authorisationHeaderValue) { if (config == null) { throw new ArgumentNullException(nameof(config)); } HeaderHelper.AddHeader(config.RequestDefaults.Headers, DefaultValues.AuthorisationHeaderKey, authorisationHeaderValue, true); return(config); }
/// <summary> /// Creates a new instance of RestRequest using the configuration provided. /// </summary> /// <param name="configuration"></param> public RestRequest(RestConfiguration configuration) { if (configuration.ClientFactory == null) { throw new InvalidOperationException(ErrorMessages.NoClientFactory); } Configuration = configuration; Request = configuration.ClientFactory.CreateRequest(); Assertion = configuration.Assertion; }
/// <summary> /// Prepare a DELETE request to the URL. You must have set the property <see cref="RestConfiguration.BaseUrl"/> of the configuration /// </summary> public static RestRequest Delete(this RestConfiguration config, string relativeUrl) { if (config == null) { throw new ArgumentNullException(nameof(config), ErrorMessages.NoConfiguration); } if (config.BaseUrl == null) { throw new ArgumentException(ErrorMessages.BaseUrlIsNotSet, nameof(relativeUrl)); } return(Rest.Delete(relativeUrl, config)); }
/// <summary> /// Prepare a request for assertion. /// </summary> /// <returns></returns> public static RestRequest Prepare(this RestConfiguration config, HttpVerb verb, string relativeUrl) { if (config == null) { throw new ArgumentNullException(nameof(config), ErrorMessages.NoConfiguration); } if (config.BaseUrl == null) { throw new ArgumentException(ErrorMessages.BaseUrlIsNotSet, nameof(relativeUrl)); } return(Rest.PrepareToUrl(verb, new Uri(config.BaseUrl, relativeUrl), config)); }
/// <summary> /// Prepare a GET request to the URL. You must have set the property <see cref="RestConfiguration.BaseUrl"/> of the configuration /// </summary> /// <param name="relative">Relative URL to append into the <see cref="RestConfiguration.BaseUrl"/> of the configuration</param> /// <param name="configuration">Configuration to apply to the request - null to use <see cref="Configuration"/></param> public static RestRequest Get(string relative, RestConfiguration configuration = null) { if (configuration == null) { configuration = Configuration; } if (configuration.BaseUrl == null) { throw new ArgumentException(ErrorMessages.BaseUrlIsNotSet, "relative"); } return(GetFromUrl(new Uri(configuration.BaseUrl, relative), configuration)); }
/// <summary> /// Prepare a DELETE request to the URL. You must have set the property <see cref="RestConfiguration.BaseUrl"/> of the configuration /// </summary> /// <param name="relative">Relative URL to append into the <see cref="RestConfiguration.BaseUrl"/> of the configuration</param> /// <param name="configuration">Configuration to apply to the request - null to use <see cref="Configuration"/></param> public static RestRequest Delete(string relative, RestConfiguration configuration = null) { if (configuration == null) { configuration = Configuration; } if (configuration.BaseUrl == null) { throw new ArgumentException(ErrorMessages.BaseUrlIsNotSet, "relative"); } return(PrepareToUrl(HttpVerb.Delete, new Uri(configuration.BaseUrl, relative), configuration)); }
/// <summary> /// Sets up a bearer token authorisation header. /// </summary> /// <param name="config"></param> /// <param name="bearerToken">Token to be appended to the header value.</param> /// <returns></returns> public static RestConfiguration WithBearerAuthorization(this RestConfiguration config, string bearerToken) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (bearerToken == null) { throw new ArgumentNullException(nameof(bearerToken)); } config.WithAuthorization($"Bearer {bearerToken}"); return(config); }
/// <summary> /// Adds a header into the request defaults. /// </summary> /// <param name="config">Request to be modified</param> /// <param name="headerKey">Header key to add</param> /// <param name="headerValue">Value of the key</param> /// <param name="overrideExistingValue">If <code>true</code> will dispose of any existing value on the header key</param> /// <returns></returns> public static RestConfiguration WithHeader(this RestConfiguration config, string headerKey, string headerValue, bool overrideExistingValue = true) { if (config == null) { throw new ArgumentNullException(nameof(config), ErrorMessages.NoConfiguration); } if (string.IsNullOrEmpty(headerKey)) { throw new ArgumentException(ErrorMessages.HeaderMustHaveKey); } HeaderHelper.AddHeader(config.RequestDefaults.Headers, headerKey, headerValue, overrideExistingValue); return(config); }
/// <summary> /// Sets up a bearer token authorisation header. /// </summary> /// <param name="config"></param> /// <param name="username">Username to generate the basic header authorization</param> /// <param name="password">Password to generate the basic header authorization</param> /// <returns></returns> public static RestConfiguration WithBasicAuthorization(this RestConfiguration config, string username, string password) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (username == null) { throw new ArgumentNullException(nameof(username)); } config.WithAuthorization($"Basic {HeaderHelper.BasicAuthorizationHeaderValue(username, password)}"); return(config); }
/// <summary> /// Prepare a specific request /// </summary> /// <param name="verb">Verb to use</param> /// <param name="url">URL to send the request to</param> /// <param name="configuration">Configuration to apply to the request - null to use <see cref="Configuration"/></param> public static RestRequest PrepareToUrl(HttpVerb verb, Uri url, RestConfiguration configuration = null) { if (url == null) { throw new ArgumentNullException(nameof(url), ErrorMessages.NoUrl); } if (configuration == null) { configuration = Configuration; } var request = new RestRequest(configuration) { Request = { Uri = url, Verb = verb } }; return(request); }
/// <summary> /// Adds an BeforeRequest Event against this request. Note that you can add more than one event. Events from the configuration are executed first. /// </summary> /// <param name="config"></param> /// <param name="afterRequest"></param> /// <returns></returns> public static RestConfiguration AfterRequest(this RestConfiguration config, AfterRequestDelegate afterRequest) { config.AfterRequestEvent += afterRequest; return(config); }
/// <summary> /// Adds an BeforeRequest Event against this request. Note that you can add more than one event. Events from the Configuration are executed first. /// </summary> /// <param name="config"></param> /// <param name="beforeRequest"></param> /// <returns></returns> public static RestConfiguration BeforeRequest(this RestConfiguration config, BeforeRequestDelegate beforeRequest) { config.BeforeRequestEvent += beforeRequest; return(config); }
/// <summary> /// Prepare a GET request to the URL /// </summary> /// <param name="url">URL to send the request to</param> /// <param name="configuration">Configuration to apply to the request - null to use <see cref="Configuration"/></param> public static RestRequest GetFromUrl(string url, RestConfiguration configuration = null) { return(GetFromUrl(new Uri(url), configuration)); }