Esempio n. 1
0
        public async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpResponseMessage response = null;
            IHttpMessageHandler handler  = null;

            // First check for a perfect match, or if there's a key that's a base url of the request
            // url, match it if client code chooses to accept that.
            if (_urlsToHandlers.TryGetValue(request.RequestUri, out handler) ||
                (MatchBaseUrls && null != (handler = _urlsToHandlers.FirstOrDefault(
                                               entry => entry.Key.IsBaseOf(request.RequestUri)).Value)))
            {
                response =
                    await handler.SendAsync(request, cancellationToken).ConfigureAwait(false);
            }
            else if (DeferToDefault)
            {
                response = await DefaultAsync(request, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                Console.WriteLine(string.Format("WARNING: No validator found for absolute URI: {0}",
                                                request.RequestUri.AbsoluteUri));

                // No handler found, so return 404
                response = await HttpTests.CreateJsonStringResponseAsync(
                    HttpStatusCode.NotFound, "Resource not found.", "ResourceNotFoundError")
                           .ConfigureAwait(false);
            }

            return(response);
        }
Esempio n. 2
0
 public Downloader(
     IHttpMessageHandler messageHandler,
     //IResource resource,
     int requestTimeout,
     string userAgent,
     Action <IResource> onStart,
     Action <IResource> onComplete,
     Action <IResource> onError,
     Action <IResource> onTimeout)
 //: this(messageHandler, resource)
     : this(messageHandler)
 {
     //this.Resource = resource;
     this.RequestTimeout = new TimeSpan(0, 0, 0, 0, requestTimeout);
     this.UserAgent      = userAgent;
     this.OnStart        = onStart;
     this.OnComplete     = onComplete;
     this.OnError        = onError;
     this.OnTimeout      = onTimeout;
 }
Esempio n. 3
0
        public CrawlerEngineTests()
        {
            this.crawlerRepository  = Substitute.For <ICrawlerRepository>();
            this.httpMessageHandler = Substitute.For <IHttpMessageHandler>();

            var httpClient = new HttpClient(new FakeHttpMessageHandler(this.httpMessageHandler));

            this.crawlerEngine = new CrawlerEngine(this.crawlerRepository, httpClient);

            this.uri = new Uri("http://localhost.crawl.com");
            this.crawlerRepository.GetNext().Returns(new CrawlItem()
            {
                Url = uri.ToString()
            });
            this.httpMessageHandler.SendAsync(Arg.Any <HttpRequestMessage>(), Arg.Any <CancellationToken>())
            .Returns(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("<a href=\"http://localhost/\"></a>")
            });
        }
 /// <summary>Initializes an instance of a <see cref="HttpMessageInvokerAdapter" /> class with a specific <see cref="T:System.Net.Http.HttpMessageHandler" />.</summary>
 /// <param name="handler">The <see cref="T:System.Net.Http.HttpMessageHandler" /> responsible for processing the HTTP response messages.</param>
 /// <param name="disposeHandler">true if the inner handler should be disposed of by Dispose(),false if you intend to reuse the inner handler.</param>
 public HttpMessageInvokerAdapter([NotNull] IHttpMessageHandler handler, bool disposeHandler)
     : this(handler.ToImplementation(), disposeHandler)
 {
 }
Esempio n. 5
0
 public ForwardingMessageHandler(IHttpMessageHandler messageHandler)
 {
     MessageHandler = messageHandler ?? throw new ArgumentNullException(nameof(messageHandler));
 }
Esempio n. 6
0
 internal DelegatingHandlerMock(IHttpMessageHandler handler)
 {
     _handler = handler;
     _handler.DefaultAsync = (_handler.DefaultAsync ?? base.SendAsync);
 }
Esempio n. 7
0
 //public Downloader(IHttpMessageHandler messageHandler, IResource resource)
 public Downloader(IHttpMessageHandler messageHandler)
 {
     this.MessageHandler = messageHandler;
     //this.Resource = resource;
 }
Esempio n. 8
0
 /// <summary>Initializes a new instance of the <see cref="HttpClientAdapter" /> class with a specific handler.</summary>
 /// <param name="handler">The <see cref="T:System.Net.Http.HttpMessageHandler" /> responsible for processing the HTTP response messages.</param>
 /// <param name="disposeHandler">true if the inner handler should be disposed of by Dispose(),false if you intend to reuse the inner handler.</param>
 public HttpClientAdapter(IHttpMessageHandler handler, bool disposeHandler)
     : this(new HttpClient(handler.ToImplementation(), disposeHandler))
 {
 }
Esempio n. 9
0
 public FakeHttpMessageHandler(IHttpMessageHandler subtitute)
 {
     this.subtitute = subtitute;
 }
 internal HttpClientHandlerMock(IHttpMessageHandler handler)
 {
     _handler = handler;
     _handler.DefaultAsync = (_handler.DefaultAsync ?? base.SendAsync);
 }