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 void AsynchCallback(HttpRemoteResponse response, object state)
        {
            // bubble it up
            var callback = _callback;

            if (callback != null)
                callback(response, state);
        }
        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 HttpRemoteResponse GetRemoteResponse(HttpWebResponse webResponse)
        {
            HttpRemoteResponse response = new HttpRemoteResponse(webResponse.StatusCode,
                GetHeadersDictionaryFrom(webResponse.Headers),
                GetContentFromStream(webResponse.GetResponseStream()));

            webResponse.Close();
            return response;
        }
        public static HttpRemoteResponse GetRemoteResponse(HttpWebResponse webResponse)
        {
            HttpRemoteResponse response = new HttpRemoteResponse(webResponse.StatusCode,
                                                                 GetHeadersDictionaryFrom(webResponse.Headers),
                                                                 GetContentFromStream(webResponse.GetResponseStream()));

            webResponse.Close();
            return(response);
        }
        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;
        }
        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 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;
        }
        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;
        }
 private void UpdateWebResponse(HttpRemoteResponse response)
 {
     this.WebResponse = response;
 }
 public DynamicXmlResource(HttpRemoteResponse response, IRemoteResourceService remoteService)
     : this(response)
 {
     this.RemoteResourceService = remoteService;
 }
 protected virtual bool ShouldRetry(HttpRemoteResponse response)
 {
     return response.StatusCode == HttpStatusCode.ServiceUnavailable;
 }
 public HttpRemoteResponse Next(HttpRemoteResponse response)
 {
     return _current.MoveNext() ? _current.Current.Process(this, response) : response;
 }
        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 DynamicXmlResource GetDynamicResourceWithServiceFake(string fileName)
        {
            XElement element;
            IRemoteResourceService remoteService;
            string xml = new LoadDocument().GetDocumentContent(fileName);
            element = XElement.Parse(xml);
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("XRUNTIME", "29");
            headers.Add("CONNECTION", "keep-alive");
            headers.Add("CONTENTTYPE", "application/xml");
            headers.Add("CACHECONTROL", "private, max-age=0, must-revalidate");
            headers.Add("DATE", "Mon, 11 Jan 2010 22:39:24 GMT");
            headers.Add("ETAG", "40edb82345bbb4d257708270c4cd8f76");
            headers.Add("LASTMODIFIED", "Tue, 05 Jan 2010 02:44:25 GMT");
            headers.Add("SERVER", "nginx/0.6.39");
            headers.Add("VIA", "1.1 varnish");
            HttpRemoteResponse response = new HttpRemoteResponse(HttpStatusCode.OK, headers, xml);

            remoteService = this.GetRemoteServiceFake(fileName);
            return new DynamicXmlResource(response, remoteService);
        }
        private void AsynchCallback(HttpRemoteResponse response, object state)
        {
            var callback = _clientCallback;

            if (callback != null)
                callback(ParseResponse(response), _clientCallbackState);
        }
 private static HttpRemoteResponse CreateRemoteResponse(string content)
 {
     HttpRemoteResponse response = new HttpRemoteResponse(HttpStatusCode.OK,
         new Dictionary<string, string>(), content);
     return response;
 }
 public DynamicXmlResource(HttpRemoteResponse response)
 {
     this.WebResponse = response;
     this.NumberFormatInfo = System.Globalization.NumberFormatInfo.CurrentInfo;
 }
        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;
        }
 protected bool ShouldRedirect(HttpRemoteResponse response)
 {
     return (int)response.StatusCode / 100 == 3 || (
            response.StatusCode == HttpStatusCode.Created && response.HasNoContent());
 }
        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"]);
        }