/// <summary> /// Creates a new instance of <see cref="HttpEntity"/> with the given body and headers. /// </summary> /// <param name="body">The entity body.</param> /// <param name="headers">The entity headers.</param> public HttpEntity(object body, HttpHeaders headers) { ArgumentUtils.AssertNotNull(headers, "headers"); this.body = body; this.headers = headers; }
/// <summary> /// Creates a new instance of <see cref="HttpMethod"/> with the given HTTP method value. /// </summary> /// <param name="method">The HTTP method as a string value.</param> public HttpMethod(string method) { ArgumentUtils.AssertNotNull(method, "method"); // TODO: check method (http://tools.ietf.org/html/rfc2616#section-2.2) this.method = method; }
/// <summary> /// Creates a new instance of <see cref="WebClientHttpRequest"/> /// with the given <see cref="HttpWebRequest"/> instance. /// </summary> /// <param name="request">The <see cref="HttpWebRequest"/> instance to use.</param> public WebClientHttpRequest(HttpWebRequest request) { ArgumentUtils.AssertNotNull(request, "request"); this.httpWebRequest = request; this.headers = new HttpHeaders(); }
/// <summary> /// Creates an OAuth2Template for a given set of client credentials. /// </summary> /// <param name="clientId">The client identifier.</param> /// <param name="clientSecret">The client password.</param> /// <param name="authorizeUrl"> /// The base URL to redirect to when doing authorization code or implicit grant authorization. /// </param> /// <param name="authenticateUrl"> /// The URL to redirect to when doing authentication via authorization code grant. /// </param> /// <param name="accessTokenUrl"> /// The URL at which an authorization code, refresh token, or user credentials may be exchanged for an access token. /// </param> /// <param name="useParametersForClientAuthentication"> /// A value indicating whether to pass client credentials to the provider as parameters /// instead of using HTTP Basic authentication. /// </param> public OAuth2Template(string clientId, string clientSecret, string authorizeUrl, string authenticateUrl, string accessTokenUrl, bool useParametersForClientAuthentication) { ArgumentUtils.AssertNotNull(clientId, "clientId"); ArgumentUtils.AssertNotNull(clientSecret, "clientSecret"); ArgumentUtils.AssertNotNull(authorizeUrl, "authorizeUrl"); ArgumentUtils.AssertNotNull(accessTokenUrl, "accessTokenUrl"); this.clientId = clientId; this.clientSecret = clientSecret; string clientInfo = "?client_id=" + HttpUtils.UrlEncode(clientId); this.authorizeUrl = authorizeUrl + clientInfo; if (authenticateUrl != null) { this.authenticateUrl = authenticateUrl + clientInfo; } else { this.authenticateUrl = null; } this.accessTokenUrl = accessTokenUrl; this.restTemplate = this.CreateRestTemplate(); this.useParametersForClientAuthentication = useParametersForClientAuthentication; if (!this.useParametersForClientAuthentication) { restTemplate.RequestInterceptors.Add(new BasicSigningRequestInterceptor(clientId, clientSecret)); } }
/// <summary> /// Creates a new instance of the <see cref="Spring.IO.AssemblyResource"/> class. /// <para/> /// Uses the specified <paramref name="type"/> to obtain the assembly and namespace for the resource. /// </summary> /// <param name="fileName">The name of the file in the assembly.</param> /// <param name="type">The type to determine the assembly and the namespace.</param> public AssemblyResource(string fileName, Type type) { ArgumentUtils.AssertHasText(fileName, "resourceName"); ArgumentUtils.AssertNotNull(type, "type"); this.Initialize(fileName, type.Namespace, type.Assembly); }
public JsonValue(string value) { ArgumentUtils.AssertNotNull(value, "value"); this.type = JsonValueType.String; this.value = value; }
/// <summary> /// Creates a new instance of <see cref="HttpResponseMessage"/> with the given headers, status code and status description. /// </summary> /// <param name="headers">The response headers.</param> /// <param name="statusCode">The HTTP status code.</param> /// <param name="statusDescription">The HTTP status description.</param> public HttpResponseMessage(HttpHeaders headers, HttpStatusCode statusCode, string statusDescription) { ArgumentUtils.AssertNotNull(headers, "headers"); this.headers = headers; this.statusCode = statusCode; this.statusDescription = statusDescription; }
/// <summary> /// Creates a new instance of the <see cref="InterceptingClientHttpRequestFactory"/> with the given parameters. /// </summary> /// <param name="requestFactory">The request factory to wrap.</param> /// <param name="interceptors">The interceptors that are to be applied. Can be <c>null</c>.</param> public InterceptingClientHttpRequestFactory( IClientHttpRequestFactory requestFactory, IEnumerable <IClientHttpRequestInterceptor> interceptors) { ArgumentUtils.AssertNotNull(requestFactory, "requestFactory"); this.targetRequestFactory = requestFactory; this.interceptors = interceptors != null ? interceptors : new IClientHttpRequestInterceptor[0]; }
/// <summary> /// Creates a new instance of <see cref="WebClientHttpResponse"/> /// with the given <see cref="HttpWebResponse"/> instance. /// </summary> /// <param name="response">The <see cref="HttpWebResponse"/> instance to use.</param> public WebClientHttpResponse(HttpWebResponse response) { ArgumentUtils.AssertNotNull(response, "response"); this.httpWebResponse = response; this.headers = new HttpHeaders(); this.Initialize(); }
/// <summary> /// Creates a <see cref="ResponseCreator"/> that respond with /// the given response body, headers, status code, and status description. /// </summary> /// <param name="body">The body of the response.</param> /// <param name="headers">The response headers.</param> /// <param name="statusCode">The response status code.</param> /// <param name="statusDescription">The response status description.</param> /// <returns>A <see cref="ResponseCreator"/>.</returns> public static ResponseCreator CreateWith( string body, HttpHeaders headers, HttpStatusCode statusCode, string statusDescription) { ArgumentUtils.AssertNotNull(body, "body"); return(delegate(IClientHttpRequest request) { return new MockClientHttpResponse(new MemoryStream(Encoding.UTF8.GetBytes(body)), headers, statusCode, statusDescription); }); }
/// <summary> /// Creates a <see cref="ResponseCreator"/> that respond with /// the given response body (from a <see cref="IResource"/>), headers, status code, and status description. /// </summary> /// <param name="body">The <see cref="IResource"/> containing the body of the response.</param> /// <param name="headers">The response headers.</param> /// <param name="statusCode">The response status code.</param> /// <param name="statusDescription">The response status description.</param> /// <returns>A <see cref="ResponseCreator"/>.</returns> public static ResponseCreator CreateWith( IResource body, HttpHeaders headers, HttpStatusCode statusCode, string statusDescription) { ArgumentUtils.AssertNotNull(body, "body"); return(delegate(IClientHttpRequest request) { return new MockClientHttpResponse(body.GetStream(), headers, statusCode, statusDescription); }); }
/// <summary> /// Creates a <see cref="MockRestServiceServer"/> instance based on the given <see cref="RestTemplate"/>. /// </summary> /// <param name="restTemplate">The RestTemplate.</param> /// <returns>The created server.</returns> public static MockRestServiceServer CreateServer(RestTemplate restTemplate) { ArgumentUtils.AssertNotNull(restTemplate, "restTemplate"); MockClientHttpRequestFactory mockRequestFactory = new MockClientHttpRequestFactory(); restTemplate.RequestFactory = mockRequestFactory; return(new MockRestServiceServer(mockRequestFactory)); }
/// <summary> /// Returns the <see cref="JsonValue"/> associated with the specified entry name /// if the value held by this instance is a JSON object structure. /// </summary> /// <param name="name">The name of the entry that contains the value to get.</param> /// <returns> /// The <see cref="JsonValue"/> associated with the specified name /// or <see langword="null"/> if the entry does not exists. /// </returns> /// <exception cref="JsonException">If the value held by this instance is not a JSON object structure.</exception> public override JsonValue GetValue(string name) { ArgumentUtils.AssertNotNull(name, "name"); IDictionary <string, JsonValue> jsonObject = (IDictionary <string, JsonValue>) this.value; JsonValue result; if (jsonObject.TryGetValue(name, out result)) { return(result); } return(null); }
/// <summary> /// Sorts the given list of <see cref="MediaType"/> objects by specificity. /// <a href="http://tools.ietf.org/html/rfc2616#section-14.1">HTTP 1.1, section 14.1</a> /// </summary> /// <remarks> /// <para> /// Given two media types: /// <ol> /// <li>if either media type has a wildcard type, then the media type without the /// wildcard is ordered before the other.</li> /// <li>if the two media types have different types, then they are considered equal and /// remain their current order.</li> /// <li>if either media type has a wildcard subtype, then the media type without /// the wildcard is sorted before the other.</li> /// <li>if the two media types have different subtypes, then they are considered equal /// and remain their current order.</li> /// <li>if the two media types have different quality value, then the media type /// with the highest quality value is ordered before the other.</li> /// <li>if the two media types have a different amount of parameters, then the /// media type with the most parameters is ordered before the other.</li> /// </ol> /// </para> /// <para> /// For example: /// <blockquote>audio/basic < audio/* < */*</blockquote> /// <blockquote>audio/* < audio/*;q=0.7; audio/*;q=0.3</blockquote> /// <blockquote>audio/basic;level=1 < audio/basic</blockquote> /// <blockquote>audio/basic == text/html</blockquote> /// <blockquote>audio/basic == audio/wave</blockquote> /// </para> /// </remarks> /// <param name="mediaTypes">The list of media types to be sorted.</param> public static void SortBySpecificity(List <MediaType> mediaTypes) { ArgumentUtils.AssertNotNull(mediaTypes, "mediaTypes"); if (mediaTypes.Count > 1) { // Stable sort for (int i = 0; i < mediaTypes.Count; i++) { mediaTypes[i].SortIndex = i; } mediaTypes.Sort(SPECIFICITY_COMPARER); } }
/// <summary> /// Sorts the given list of <see cref="MediaType"/> objects by quality value. /// </summary> /// <remarks> /// <para> /// Given two media types: /// <ol> /// <li>if the two media types have different quality value, then the media type /// with the highest quality value is ordered before the other.</li> /// <li>if either media type has a wildcard type, then the media type without the /// wildcard is ordered before the other.</li> /// <li>if the two media types have different types, then they are considered equal and /// remain their current order.</li> /// <li>if either media type has a wildcard subtype, then the media type without /// the wildcard is sorted before the other.</li> /// <li>if the two media types have different subtypes, then they are considered equal /// and remain their current order.</li> /// <li>if the two media types have a different amount of parameters, then the /// media type with the most parameters is ordered before the other.</li> /// </ol> /// </para> /// </remarks> /// <param name="mediaTypes">The list of media types to be sorted</param> public static void SortByQualityValue(List <MediaType> mediaTypes) { ArgumentUtils.AssertNotNull(mediaTypes, "mediaTypes"); if (mediaTypes.Count > 1) { // Stable sort for (int i = 0; i < mediaTypes.Count; i++) { mediaTypes[i].SortIndex = i; } mediaTypes.Sort(QUALITY_VALUE_COMPARER); } }
/// <summary> /// Adds the specified name/<see cref="JsonValue"/> pair /// to the JSON object structure held by this instance. /// </summary> /// <param name="name">The name of the entry to add.</param> /// <param name="value">The <see cref="JsonValue"/> of the entry to add.</param> /// <exception cref="JsonException"> /// If the value held by this instance is not a JSON object structure or /// if an entry with the same name already exists. /// </exception> public void AddValue(string name, JsonValue value) { ArgumentUtils.AssertNotNull(name, "name"); ArgumentUtils.AssertNotNull(value, "value"); IDictionary <string, JsonValue> jsonObject = (IDictionary <string, JsonValue>) this.value; if (jsonObject.ContainsKey(name)) { throw new JsonException(String.Format( "An entry with the name '{0}' already exists in the JSON object structure.", name)); } jsonObject.Add(name, value); }
/// <summary> /// Creates an OAuth1Template. /// </summary> /// <param name="consumerKey">The application's consumer key.</param> /// <param name="consumerSecret">The application's consumer secret.</param> /// <param name="requestTokenUrl">The request token URL.</param> /// <param name="authorizeUrl">The authorize URL.</param> /// <param name="authenticateUrl">The authenticate URL.</param> /// <param name="accessTokenUrl">The access token URL.</param> /// <param name="version">The version of OAuth 1, either 10 or 10a.</param> public OAuth1Template(string consumerKey, string consumerSecret, string requestTokenUrl, string authorizeUrl, string authenticateUrl, string accessTokenUrl, OAuth1Version version) { ArgumentUtils.AssertNotNull(consumerKey, "consumerKey"); ArgumentUtils.AssertNotNull(consumerSecret, "consumerSecret"); ArgumentUtils.AssertNotNull(requestTokenUrl, "requestTokenUrl"); ArgumentUtils.AssertNotNull(authorizeUrl, "authorizeUrl"); ArgumentUtils.AssertNotNull(accessTokenUrl, "accessTokenUrl"); this.consumerKey = consumerKey; this.consumerSecret = consumerSecret; this.requestTokenUrl = new Uri(requestTokenUrl); this.authorizeUrl = authorizeUrl; this.authenticateUrl = authenticateUrl; this.accessTokenUrl = new Uri(accessTokenUrl); this.version = version; this.restTemplate = this.CreateRestTemplate(); this.signingSupport = new SigningSupport(); }
/// <summary> /// Create a new <see cref="IClientHttpRequest"/> for the specified URI and HTTP method. /// </summary> /// <param name="uri">The URI to create a request for.</param> /// <param name="method">The HTTP method to execute.</param> /// <returns>The created request.</returns> public IClientHttpRequest CreateRequest(Uri uri, HttpMethod method) { ArgumentUtils.AssertNotNull(uri, "uri"); ArgumentUtils.AssertNotNull(method, "method"); if (this.requestEnumerator == null) { requestEnumerator = expectedRequests.GetEnumerator(); } if (!this.requestEnumerator.MoveNext()) { throw new AssertionException("No further requests expected"); } MockClientHttpRequest currentRequest = this.requestEnumerator.Current; currentRequest.Uri = uri; currentRequest.Method = method; return(currentRequest); }
/// <summary> /// Allows for reponse creation on the request. /// </summary> /// <param name="responseCreator">The response creator.</param> public void AndRespond(ResponseCreator responseCreator) { ArgumentUtils.AssertNotNull(responseCreator, "responseCreator"); this.responseCreator = responseCreator; }
/// <summary> /// Create a new <see cref="IClientHttpRequest"/> for the specified URI and HTTP method. /// </summary> /// <param name="uri">The URI to create a request for.</param> /// <param name="method">The HTTP method to execute.</param> /// <returns>The created request</returns> public virtual IClientHttpRequest CreateRequest(Uri uri, HttpMethod method) { ArgumentUtils.AssertNotNull(uri, "uri"); ArgumentUtils.AssertNotNull(method, "method"); HttpWebRequest httpWebRequest; #if SILVERLIGHT && !WINDOWS_PHONE switch (this._webRequestCreator) { case WebRequestCreatorType.ClientHttp: httpWebRequest = (HttpWebRequest)System.Net.Browser.WebRequestCreator.ClientHttp.Create(uri); break; case WebRequestCreatorType.BrowserHttp: httpWebRequest = (HttpWebRequest)System.Net.Browser.WebRequestCreator.BrowserHttp.Create(uri); break; case WebRequestCreatorType.Unknown: default: if (method == HttpMethod.GET || method == HttpMethod.POST) { httpWebRequest = WebRequest.Create(uri) as HttpWebRequest; } else { // Force Client HTTP stack httpWebRequest = (HttpWebRequest)System.Net.Browser.WebRequestCreator.ClientHttp.Create(uri); } break; } #else httpWebRequest = WebRequest.Create(uri) as HttpWebRequest; #endif httpWebRequest.Method = method.ToString(); #if !SILVERLIGHT_3 if (this._allowAutoRedirect != null) { httpWebRequest.AllowAutoRedirect = this._allowAutoRedirect.Value; } #if !CF_3_5 if (this._useDefaultCredentials.HasValue) { httpWebRequest.UseDefaultCredentials = this._useDefaultCredentials.Value; } #endif #endif #if !CF_3_5 if (this._cookieContainer != null) { httpWebRequest.CookieContainer = this._cookieContainer; } #endif if (this._credentials != null) { httpWebRequest.Credentials = this._credentials; } #if !SILVERLIGHT if (this._clientCertificates != null) { foreach (X509Certificate2 certificate in this._clientCertificates) { httpWebRequest.ClientCertificates.Add(certificate); } } if (this._proxy != null) { httpWebRequest.Proxy = this._proxy; } if (this._timeout != null) { httpWebRequest.Timeout = this._timeout.Value; } if (this._expect100Continue != null) { httpWebRequest.ServicePoint.Expect100Continue = this._expect100Continue.Value; } if (this._automaticDecompression.HasValue) { httpWebRequest.AutomaticDecompression = this._automaticDecompression.Value; } #endif return(new WebClientHttpRequest(httpWebRequest)); }
/// <summary> /// Creates a new instance of the <see cref="Spring.IO.StreamResource"/> class. /// </summary> /// <param name="stream">The <see cref="System.IO.Stream"/> to use.</param> public StreamResource(Stream stream) { ArgumentUtils.AssertNotNull(stream, "stream"); this.stream = stream; }
/// <summary> /// Allows for further expectations to be set on the request. /// </summary> /// <param name="requestMatcher">The request expectations.</param> /// <returns>The request expectations.</returns> public IRequestActions AndExpect(RequestMatcher requestMatcher) { ArgumentUtils.AssertNotNull(requestMatcher, "requestMatcher"); this.requestMatchers.Add(requestMatcher); return(this); }
/// <summary> /// Adds a <see cref="JsonValue"/> to the end of the JSON array structure. /// </summary> /// <param name="value">The <see cref="JsonValue"/> to add.</param> public void AddValue(JsonValue value) { ArgumentUtils.AssertNotNull(value, "value"); ((IList <JsonValue>) this.value).Add(value); }
private MockRestServiceServer(MockClientHttpRequestFactory mockRequestFactory) { ArgumentUtils.AssertNotNull(mockRequestFactory, "mockRequestFactory"); this.mockRequestFactory = mockRequestFactory; }