private HttpRemoteResponse CreateRemoteResponse(string content, string verb) { var statusCode = HttpStatusCode.OK; var hasContent = !String.IsNullOrWhiteSpace(content); switch (verb.ToUpper()) { case "POST": statusCode = hasContent ? HttpStatusCode.Created : HttpStatusCode.BadRequest; break; case "PUT": statusCode = hasContent ? HttpStatusCode.OK : HttpStatusCode.Conflict; break; case "DELETE": statusCode = HttpStatusCode.NoContent; break; } var response = new HttpRemoteResponse(statusCode, new Dictionary <string, string>(), content); if (hasContent) { response.Headers.Add("Content-Type", _contentType); response.Headers.Add("Content-Length", content.Length.ToString()); } return(response); }
private static HttpRemoteResponse CreateRemoteResponse(string content) { HttpRemoteResponse response = new HttpRemoteResponse(HttpStatusCode.OK, new Dictionary <string, string>(), content); return(response); }
public static DynamicXmlResource GetDynamicXmlResourceWithServiceFake(string rawUri) { IRemoteResourceService service; HttpRemoteResponse response = GetDynamicResourceWithServiceFake(rawUri, "application/xml", out service); return(new DynamicXmlResource(response, service)); }
private DynamicJsonResource(HttpRemoteResponse webResponse, IRemoteResourceService service, JContainer root) { WebResponse = webResponse; _service = service; _root = root; IndexRoot(); }
private void AsynchCallback(HttpRemoteResponse response, object state) { var callback = _clientCallback; if (callback != null) { callback(ParseResponse(response), _clientCallbackState); } }
public DynamicJsonResource(HttpRemoteResponse webResponse) { WebResponse = webResponse; _root = String.IsNullOrWhiteSpace(webResponse.Content) ? new JObject() : JObject.Parse(webResponse.Content); IndexRoot(); }
private void AsynchWorker(object rawState) { dynamic state = rawState; HttpRemoteResponse response = _request.Process( state.Uri, state.Verb, state.Payload); AsynchCallback(response, rawState); }
public static Mock <IRequestDispatcher> CreateDispatcher(HttpRemoteResponse response) { var dispatcher = new Mock <IRequestDispatcher>(); dispatcher.Setup(d => d.Process(It.IsAny <IRemoteResourceService>(), It.IsAny <string>(), It.IsAny <Uri>(), It.IsAny <string>())) .Returns(response); return(dispatcher); }
public static Mock <IRequestFeature> CreateRequestFeature(HttpRemoteResponse response) { var requestFeature = new Mock <IRequestFeature>(); requestFeature.Setup(f => f.Process(It.IsAny <RequestChain>(), It.IsAny <Request>(), It.IsAny <string>(), It.IsAny <Uri>(), It.IsAny <string>())) .Callback <RequestChain, Request, string, Uri, string>((c, r, v, u, p) => c.Next(r, v, u, p)) .Returns(response); return(requestFeature); }
public static Mock <IResponseFeature> CreateResponseFeature(HttpRemoteResponse response) { var responseFeature = new Mock <IResponseFeature>(); responseFeature.Setup(f => f.Process(It.IsAny <ResponseChain>(), response)) .Callback <ResponseChain, HttpRemoteResponse>((c, r) => c.Next(r)) .Returns(response); return(responseFeature); }
private void AsynchCallback(HttpRemoteResponse response, object state) { // bubble it up var callback = _callback; if (callback != null) { callback(response, state); } }
private IRemoteResourceService CreateService(HttpRemoteResponse response = null, IRequestDispatcher dispatcher = null) { if (response == null) { response = TestHelper.CreateResponse(); } if (dispatcher == null) { dispatcher = TestHelper.CreateDispatcher(response).Object; } return(new EntryPointService("file://NotImportant", dispatcher)); }
private IResource CreateResource(string contentType, HttpRemoteResponse response) { if (contentType.IndexOf("application/xml", StringComparison.OrdinalIgnoreCase) > -1 || contentType.IndexOf("text/xml", StringComparison.OrdinalIgnoreCase) > -1) { return(new DynamicXmlResource(response, this)); } if (contentType.IndexOf("application/json", StringComparison.OrdinalIgnoreCase) > -1 || contentType.IndexOf("text/json", StringComparison.OrdinalIgnoreCase) > -1) { return(new DynamicJsonResource(response, this)); } return(null); }
public HttpRemoteResponse Process(ResponseChain chain, HttpRemoteResponse response) { response = chain.Next(response); if (!ShouldRedirect(response)) return response; string uri; if (response.Headers.TryGetValue("Location", out uri) && !String.IsNullOrWhiteSpace(uri)) { chain.Reset(); response = chain.Service.Dispatcher.Process(chain.Service, "GET", new Uri(uri), null); response = chain.Next(response); } return response; }
private IResource ParseResponse(HttpRemoteResponse response) { if (response.StatusCode >= HttpStatusCode.BadRequest || response.HasNoContent() || !response.Headers.ContainsKey("Content-Type")) { return(new EmptyResource(response)); } string contentType = response.Headers["Content-Type"]; IResource resource = CreateResource(contentType, response); if (resource != null) { return(resource); } throw new InvalidOperationException("unsupported media type: " + response.Headers["Content-Type"]); }
public HttpRemoteResponse Process(ResponseChain chain, HttpRemoteResponse response) { if (!chain.Service.IsAsynch) { throw new NotSupportedException("AutoRefresh is not supported on synchronous requests"); } response = chain.Next(response); Refresh refresh; if (!ShouldRefresh(response, out refresh) || refresh == null) { return(response); } Thread.Sleep(TimeSpan.FromSeconds(refresh.Seconds)); // TODO: use a better alternative response = chain.Service.Dispatcher.Process(chain.Service, "GET", new Uri(refresh.Uri), null); response = chain.Next(response); return(response); }
protected bool ShouldRefresh(HttpRemoteResponse response, out Refresh refresh) { var should = (int)response.StatusCode / 100 == 2 && response.Headers.ContainsKey("Refresh"); refresh = null; if (should) { var match = RefreshPattern.Match(response.Headers["Refresh"]); if (match.Success && match.Groups.Count == 3) { refresh = new Refresh { Seconds = int.Parse(match.Groups[1].Value), Uri = match.Groups[2].Value } } ; } return(should); } }
public HttpRemoteResponse Next(HttpRemoteResponse response) { return(_current.MoveNext() ? _current.Current.Process(this, response) : response); }
public DynamicJsonResource(HttpRemoteResponse webResponse, IRemoteResourceService service) : this(webResponse) { _service = service; }
protected bool ShouldRedirect(HttpRemoteResponse response) { return (int)response.StatusCode / 100 == 3 || ( response.StatusCode == HttpStatusCode.Created && response.HasNoContent()); }
public DynamicXmlResource(HttpRemoteResponse response, IRemoteResourceService remoteService) : this(response) { RemoteResourceService = remoteService; }
public DynamicXmlResource(HttpRemoteResponse response) { WebResponse = response; NumberFormatInfo = NumberFormatInfo.CurrentInfo; }
public EmptyResource(HttpRemoteResponse webResponse) { WebResponse = webResponse; }
protected virtual bool ShouldRetry(HttpRemoteResponse response) { return(response.StatusCode == HttpStatusCode.ServiceUnavailable); }