public override void DoWithRequest(IClientHttpRequest request) { base.DoWithRequest(request); foreach (string str in this.requestEntity.Headers) { request.Headers[str] = this.requestEntity.Headers[str]; } if (this.requestEntity.HasBody) { object body = this.requestEntity.Body; MediaType contentType = this.requestEntity.Headers.ContentType; foreach (IHttpMessageConverter converter in base.messageConverters) { if (converter.CanWrite(body.GetType(), contentType)) { converter.Write(body, contentType, request); return; } } string str2 = string.Format("Could not write request: no suitable IHttpMessageConverter found for request type [{0}]", body.GetType().FullName); if (contentType != null) { str2 = string.Format("{0} and content type [{1}]", str2, contentType); } throw new RestClientException(str2); } if (request.Headers.ContentLength == -1L) { request.Headers.ContentLength = 0L; } }
public virtual void DoWithRequest(IClientHttpRequest request) { if (this.responseType != null) { List<MediaType> mediaTypes = new List<MediaType>(); foreach (IHttpMessageConverter converter in this.messageConverters) { if (converter.CanRead(this.responseType, null)) { foreach (MediaType type in converter.SupportedMediaTypes) { if (type.CharSet != null) { mediaTypes.Add(new MediaType(type.Type, type.Subtype)); } else { mediaTypes.Add(type); } } continue; } } if (mediaTypes.Count > 0) { MediaType.SortBySpecificity(mediaTypes); request.Headers.Accept = mediaTypes.ToArray(); } } }
/// <summary> /// Creates a new instance of the <see cref="InterceptingClientHttpRequest"/> with the given parameters. /// </summary> /// <param name="request">The request to wrap.</param> /// <param name="interceptors">The interceptors that are to be applied. Can be <c>null</c>.</param> public InterceptingClientHttpRequest( IClientHttpRequest request, IEnumerable<IClientHttpRequestInterceptor> interceptors) { ArgumentUtils.AssertNotNull(request, "request"); this.delegateRequest = request; this.interceptors = interceptors != null ? interceptors : new IClientHttpRequestInterceptor[0]; }
public void SetUp() { mocks = new MockRepository(); requestFactory = mocks.StrictMock<IClientHttpRequestFactory>(); request = mocks.StrictMock<IClientHttpRequest>(); response = mocks.StrictMock<IClientHttpResponse>(); errorHandler = mocks.StrictMock<IResponseErrorHandler>(); converter = mocks.StrictMock<IHttpMessageConverter>(); IList<IHttpMessageConverter> messageConverters = new List<IHttpMessageConverter>(1); messageConverters.Add(converter); template = new RestTemplate(); template.RequestFactory = requestFactory; template.MessageConverters = messageConverters; template.ErrorHandler = errorHandler; }
/// <summary> /// Gets called by <see cref="RestTemplate"/> with an <see cref="IClientHttpRequest"/> to write data. /// </summary> /// <remarks> /// Does not need to care about closing the request or about handling errors: /// this will all be handled by the <see cref="RestTemplate"/> class. /// </remarks> /// <param name="request">The active HTTP request.</param> public virtual void DoWithRequest(IClientHttpRequest request) { if (responseType != null) { List<MediaType> allSupportedMediaTypes = new List<MediaType>(); foreach (IHttpMessageConverter messageConverter in this.messageConverters) { if (messageConverter.CanRead(responseType, null)) { foreach (MediaType supportedMediaType in messageConverter.SupportedMediaTypes) { if (supportedMediaType.CharSet != null) { allSupportedMediaTypes.Add(new MediaType( supportedMediaType.Type, supportedMediaType.Subtype)); } else { allSupportedMediaTypes.Add(supportedMediaType); } } } } if (allSupportedMediaTypes.Count > 0) { MediaType.SortBySpecificity(allSupportedMediaTypes); #region Instrumentation #if !SILVERLIGHT && !CF_3_5 if (LOG.IsDebugEnabled) { LOG.Debug(String.Format( "Setting request Accept header to '{0}'", MediaType.ToString(allSupportedMediaTypes))); } #endif #endregion request.Headers.Accept = allSupportedMediaTypes.ToArray(); } } }
public void NoAsyncExecution() { ManualResetEvent manualEvent = new ManualResetEvent(false); Exception exception = null; NoOpExecutionInterceptor interceptor1 = new NoOpExecutionInterceptor(); NoOpRequestAsyncInterceptor interceptor2 = new NoOpRequestAsyncInterceptor(); requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, new IClientHttpRequestInterceptor[] { interceptor1, interceptor2 }); IClientHttpRequest request = requestFactory.CreateRequest(new Uri("http://example.com"), HttpMethod.GET); request.ExecuteAsync(null, delegate(ClientHttpRequestCompletedEventArgs args) { try { Assert.Fail("No Execution"); } catch (Exception ex) { exception = ex; } finally { manualEvent.Set(); } }); //manualEvent.WaitOne(); if (exception != null) { throw exception; } Assert.IsFalse(interceptor2.invoked); Assert.IsFalse(requestMock.executed); }
public void ChangeBody() { ChangeBodyInterceptor interceptor = new ChangeBodyInterceptor(); requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, new IClientHttpRequestInterceptor[] { interceptor }); IClientHttpRequest request = requestFactory.CreateRequest(new Uri("http://example.com"), HttpMethod.GET); request.Execute(); MemoryStream stream = new MemoryStream(); requestMock.Body(stream); stream.Position = 0; string result = null; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } Assert.AreEqual("New body", result); }
/// <summary> /// Gets called by <see cref="RestTemplate"/> with an <see cref="IClientHttpRequest"/> to write data. /// </summary> /// <remarks> /// Does not need to care about closing the request or about handling errors: /// this will all be handled by the <see cref="RestTemplate"/> class. /// </remarks> /// <param name="request">The active HTTP request.</param> public override void DoWithRequest(IClientHttpRequest request) { base.DoWithRequest(request); // headers foreach (string header in requestEntity.Headers) { request.Headers[header] = requestEntity.Headers[header]; } // body if (requestEntity.HasBody) { object requestBody = requestEntity.Body; MediaType requestContentType = requestEntity.Headers.ContentType; foreach (IHttpMessageConverter messageConverter in base.messageConverters) { if (messageConverter.CanWrite(requestBody.GetType(), requestContentType)) { #region Instrumentation #if !SILVERLIGHT && !CF_3_5 if (LOG.IsDebugEnabled) { if (requestContentType != null) { LOG.Debug(String.Format( "Writing [{0}] as '{1}' using [{2}]", requestBody, requestContentType, messageConverter)); } else { LOG.Debug(String.Format( "Writing [{0}] using [{1}]", requestBody, messageConverter)); } } #endif #endregion messageConverter.Write(requestBody, requestContentType, request); return; } } string message = String.Format( "Could not write request: no suitable IHttpMessageConverter found for request type [{0}]", requestBody.GetType().FullName); if (requestContentType != null) { message = String.Format("{0} and content type [{1}]", message, requestContentType); } throw new RestClientException(message); } #if !SILVERLIGHT else { if (request.Headers.ContentLength == -1) { request.Headers.ContentLength = 0; } } #endif }
protected AbstractRequestContext(IClientHttpRequest delegateRequest, Action<Stream> body, IEnumerable<IClientHttpRequestInterceptor> interceptors) { this.delegateRequest = delegateRequest; this.body = body; this.enumerator = interceptors.GetEnumerator(); }
public RequestSyncExecution(IClientHttpRequest delegateRequest, Action<Stream> body, IEnumerable<IClientHttpRequestInterceptor> interceptors) : base(delegateRequest, body, interceptors) { }
public InterceptingClientHttpRequest(IClientHttpRequest request, IEnumerable<IClientHttpRequestInterceptor> interceptors) { ArgumentUtils.AssertNotNull(request, "request"); this.targetRequest = request; this.interceptors = (interceptors != null) ? interceptors : ((IEnumerable<IClientHttpRequestInterceptor>) new IClientHttpRequestInterceptor[0]); }
public RequestAsyncExecution(IClientHttpRequest delegateRequest, Action<Stream> body, IEnumerable<IClientHttpRequestInterceptor> interceptors, object asyncState, Action<ClientHttpRequestCompletedEventArgs> executeCompleted) : base(delegateRequest, body, interceptors) { this.asyncState = asyncState; this.interceptedExecuteCompleted = executeCompleted; this.executeCompletedDelegates = new List<Action<IClientHttpResponseAsyncContext>>(); }
internal RestOperationCanceler(IClientHttpRequest request) { this.request = request; }