コード例 #1
0
        public IHttpRequest Create(string ip, int port)
        {
            var httpRequest = factory.Create(ip, port, false);

            httpRequests.Add(httpRequest);
            return(httpRequest);
        }
コード例 #2
0
        public virtual async Task <ContinuousChangesResponse> GetAsync(GetChangesRequest request, Action <string> onRead, CancellationToken cancellationToken)
        {
            Ensure.That(request, "request").IsNotNull();
            Ensure.That(onRead, "onRead").IsNotNull();
            if (!request.Feed.HasValue || request.Feed != ChangesFeed.Continuous)
            {
                throw new ArgumentException(ExceptionStrings.GetContinuousChangesInvalidFeed, "request");
            }

            using (var httpRequest = HttpRequestFactory.Create(request))
            {
                using (var httpResponse = await SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ForAwait())
                {
                    var response = ContinuousChangesResponseFactory.Create(httpResponse);
                    if (response.IsSuccess)
                    {
                        using (var content = await httpResponse.Content.ReadAsStreamAsync().ForAwait())
                        {
                            using (var reader = new StreamReader(content, MyCouchRuntime.DefaultEncoding))
                            {
                                while (!cancellationToken.IsCancellationRequested && !reader.EndOfStream)
                                {
                                    cancellationToken.ThrowIfCancellationRequested();
                                    onRead(reader.ReadLine());
                                }
                            }
                        }
                    }
                    return(response);
                }
            }
        }
コード例 #3
0
        public void EvaluatesOptionsShouldLogInputStream()
        {
            KissLog.Tests.Common.CommonTestHelpers.ResetContext();

            KissLogConfiguration.Options.ShouldLogInputStream((KissLog.Http.HttpRequest args) => false);

            string body = $"Input stream {Guid.NewGuid()}";
            var    ms   = new MemoryStream(Encoding.UTF8.GetBytes(body));

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.Method).Returns("GET");
            httpRequest.Setup(p => p.Body).Returns(ms);
            httpRequest.Setup(p => p.Headers).Returns(new CustomHeaderCollection(new Dictionary <string, StringValues>
            {
                { HeaderNames.ContentType, "text/plain" }
            }));

            ModuleInitializer.ReadInputStreamProvider = new EnableBufferingReadInputStreamProvider();

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.IsNull(result.Properties.InputStream);
        }
コード例 #4
0
        public void Intercept(IInvocation invocation)
        {
            var factory     = new HttpRequestFactory(_settings);
            var httpRequest = factory.Create(invocation);

            _assert(httpRequest);
        }
コード例 #5
0
        public void Intercept(IInvocation invocation)
        {
            _configure(_settings);
            _settings.Request.ParameterListTransformers.Add(new TestTransformer(_pipelineAction));
            var factory     = new HttpRequestFactory(_settings);
            var httpRequest = factory.Create(invocation);

            _assert(httpRequest);
        }
コード例 #6
0
ファイル: Changes.cs プロジェクト: ygalbel/mycouch
        public virtual async Task <ChangesResponse> GetAsync(GetChangesRequest request, CancellationToken cancellationToken = default)
        {
            var httpRequest = HttpRequestFactory.Create(request);

            using (var httpResponse = await SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ForAwait())
            {
                return(await ChangesResponseFactory.CreateAsync(httpResponse).ForAwait());
            }
        }
コード例 #7
0
ファイル: Changes.cs プロジェクト: hambroz/mycouch
        public virtual async Task <ChangesResponse <TIncludedDoc> > GetAsync <TIncludedDoc>(GetChangesRequest request)
        {
            var httpRequest = HttpRequestFactory.Create(request);

            using (var httpResponse = await SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).ForAwait())
            {
                return(ChangesResponseFactory.Create <TIncludedDoc>(httpResponse));
            }
        }
コード例 #8
0
        public void Intercept(IInvocation invocation)
        {
            var listTransformers = new ApiSettings().CreateParameterListTransformers();
            var factory          = new HttpRequestFactory(new ContentSerializer(new JsonSerializer()), listTransformers);

            _configure(factory);
            var httpRequest = factory.Create(invocation);

            _assert(httpRequest);
        }
コード例 #9
0
        public void EmptyHttpRequestDoesNotThrowException()
        {
            Uri url = UrlParser.GenerateUri($"/Home/Index/{Guid.NewGuid()}");

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(url);
            httpRequest.Setup(p => p.Method).Returns("GET");

            var result = HttpRequestFactory.Create(httpRequest.Object);
        }
コード例 #10
0
        public void HttpMethodIsCopied(string httpMethod)
        {
            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.Method).Returns(httpMethod);

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(httpMethod, result.HttpMethod);
        }
コード例 #11
0
        public virtual async Task <ChangesResponse <TIncludedDoc> > GetAsync <TIncludedDoc>(GetChangesRequest request)
        {
            Ensure.That(request, "request").IsNotNull();
            EnsureNonContinuousFeed(request);

            using (var httpRequest = HttpRequestFactory.Create(request))
            {
                using (var httpResponse = await SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).ForAwait())
                {
                    return(ChangesResponseFactory.Create <TIncludedDoc>(httpResponse));
                }
            }
        }
コード例 #12
0
        public void UrlIsCopied()
        {
            Uri url = UrlParser.GenerateUri($"/Home/Index/{Guid.NewGuid()}?param1={Guid.NewGuid()}");

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(url);
            httpRequest.Setup(p => p.Method).Returns("GET");

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(url.ToString(), result.Url.ToString());
        }
コード例 #13
0
        public void MachineNameIsCopied()
        {
            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.Method).Returns("GET");

            var result = HttpRequestFactory.Create(httpRequest.Object);

            string machineName = InternalHelpers.GetMachineName();

            Assert.AreEqual(machineName, result.MachineName);
        }
コード例 #14
0
        public void HeadersAreCopied()
        {
            var value = CommonTestHelpers.GenerateList(5);

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.Method).Returns("GET");
            httpRequest.Setup(p => p.Headers).Returns(new CustomHeaderCollection(value.ToStringValuesDictionary()));

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(JsonSerializer.Serialize(value), JsonSerializer.Serialize(result.Properties.Headers));
        }
コード例 #15
0
        public void ServerVariablesAreCopied()
        {
            var value = KissLog.Tests.Common.CommonTestHelpers.GenerateList(5);

            var httpRequest = new Mock <HttpRequestBase>();

            httpRequest.Setup(p => p.Url).Returns(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.HttpMethod).Returns("GET");
            httpRequest.Setup(p => p.ServerVariables).Returns(Helpers.GenerateNameValueCollection(value));

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(JsonSerializer.Serialize(value), JsonSerializer.Serialize(result.Properties.ServerVariables));
        }
コード例 #16
0
        public void UserHostAddressIsCopied()
        {
            string userHostAddress = $"UserHostAddress {Guid.NewGuid()}";

            var httpRequest = new Mock <HttpRequestBase>();

            httpRequest.Setup(p => p.Url).Returns(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.HttpMethod).Returns("GET");
            httpRequest.Setup(p => p.UserHostAddress).Returns(userHostAddress);

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(userHostAddress, result.RemoteAddress);
        }
コード例 #17
0
        public void UrlReferrerIsCopied()
        {
            Uri urlReferrer = new Uri("https://google.com");

            var httpRequest = new Mock <HttpRequestBase>();

            httpRequest.Setup(p => p.Url).Returns(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.HttpMethod).Returns("GET");
            httpRequest.Setup(p => p.UrlReferrer).Returns(urlReferrer);

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(urlReferrer.ToString(), result.HttpReferer);
        }
コード例 #18
0
        public void QueryStringIsCopied()
        {
            var value = CommonTestHelpers.GenerateList(5);
            var qs    = new FormUrlEncodedContent(value).ReadAsStringAsync().Result;

            var dictionary = CommonTestHelpers.GenerateDictionary(value);

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(UrlParser.GenerateUri("/Home/Index?" + qs));
            httpRequest.Setup(p => p.Method).Returns("GET");

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(JsonSerializer.Serialize(value), JsonSerializer.Serialize(result.Properties.QueryString));
        }
コード例 #19
0
        public void QueryStringIsCopiedFromUnvalidatedRequestValues()
        {
            var value            = KissLog.Tests.Common.CommonTestHelpers.GenerateList(5);
            var unvalidatedValue = KissLog.Tests.Common.CommonTestHelpers.GenerateList(5);

            var httpRequest = new Mock <HttpRequestBase>();

            httpRequest.Setup(p => p.Url).Returns(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.HttpMethod).Returns("GET");
            httpRequest.Setup(p => p.QueryString).Returns(Helpers.GenerateNameValueCollection(value));
            httpRequest.Setup(p => p.Unvalidated.QueryString).Returns(Helpers.GenerateNameValueCollection(unvalidatedValue));

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(JsonSerializer.Serialize(unvalidatedValue), JsonSerializer.Serialize(result.Properties.QueryString));
        }
コード例 #20
0
        /// <summary>
        ///		Create a new HTTP request that expects and uses XML as its primary format.
        /// </summary>
        /// <typeparam name="TContext">
        ///		The type of object used as a context for resolving deferred parameters.
        /// </typeparam>
        /// <param name="requestFactory">
        ///		The HTTP request factory.
        /// </param>
        /// <param name="requestUri">
        ///		The request URI (can be relative or absolute).
        /// </param>
        /// <returns>
        ///		The new <see cref="HttpRequest"/>.
        /// </returns>
        public static HttpRequest <TContext> CreateXml <TContext>(this HttpRequestFactory <TContext> requestFactory, string requestUri)
        {
            if (requestFactory == null)
            {
                throw new ArgumentNullException(nameof(requestFactory));
            }

            if (String.IsNullOrWhiteSpace(requestUri))
            {
                throw new ArgumentException("Argument cannot be null, empty, or composed entirely of whitespace: 'requestUri'.", nameof(requestUri));
            }

            return
                (requestFactory.Create(requestUri)
                 .ExpectXml()
                 .UseXml());
        }
コード例 #21
0
        /// <summary>
        ///		Create a new HTTP request that expects and uses XML as its primary format.
        /// </summary>
        /// <typeparam name="TContext">
        ///		The type of object used as a context for resolving deferred parameters.
        /// </typeparam>
        /// <param name="requestFactory">
        ///		The HTTP request factory.
        /// </param>
        /// <param name="requestUri">
        ///		The request URI (can be relative or absolute).
        /// </param>
        /// <returns>
        ///		The new <see cref="HttpRequest"/>.
        /// </returns>
        public static HttpRequest <TContext> CreateXml <TContext>(this HttpRequestFactory <TContext> requestFactory, Uri requestUri)
        {
            if (requestFactory == null)
            {
                throw new ArgumentNullException(nameof(requestFactory));
            }

            if (requestUri == null)
            {
                throw new ArgumentNullException(nameof(requestUri));
            }

            return
                (requestFactory.Create(requestUri)
                 .ExpectXml()
                 .UseXml());
        }
コード例 #22
0
        public void ReferrerIsCopied()
        {
            string referrer = $"Referer {Guid.NewGuid()}";

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.Method).Returns("GET");
            httpRequest.Setup(p => p.Headers).Returns(new CustomHeaderCollection(new Dictionary <string, StringValues>
            {
                { HeaderNames.Referer, referrer }
            }));

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(referrer, result.HttpReferer);
        }
コード例 #23
0
        public void EvaluatesOptionsShouldLogFormData()
        {
            KissLog.Tests.Common.CommonTestHelpers.ResetContext();

            KissLogConfiguration.Options.ShouldLogFormData((KissLog.Http.HttpRequest args) => false);

            var value = KissLog.Tests.Common.CommonTestHelpers.GenerateList(5);

            var httpRequest = new Mock <HttpRequestBase>();

            httpRequest.Setup(p => p.Url).Returns(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.HttpMethod).Returns("GET");
            httpRequest.Setup(p => p.Unvalidated.Form).Returns(Helpers.GenerateNameValueCollection(value));

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(0, result.Properties.FormData.Count());
        }
コード例 #24
0
        public void MachineNameIsCopied()
        {
            NameValueCollection serverVariables = new NameValueCollection();

            serverVariables.Add("SERVER_NAME", Guid.NewGuid().ToString());

            var httpRequest = new Mock <HttpRequestBase>();

            httpRequest.Setup(p => p.Url).Returns(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.HttpMethod).Returns("GET");
            httpRequest.Setup(p => p.ServerVariables).Returns(serverVariables);

            var result = HttpRequestFactory.Create(httpRequest.Object);

            string machineName = InternalHelpers.GetMachineName(httpRequest.Object);

            Assert.AreEqual(machineName, result.MachineName);
        }
コード例 #25
0
        public void FormDataIsCopied()
        {
            KissLog.Tests.Common.CommonTestHelpers.ResetContext();

            var value = CommonTestHelpers.GenerateList(5);

            var dictionary = CommonTestHelpers.GenerateDictionary(value);

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.Method).Returns("GET");
            httpRequest.Setup(p => p.HasFormContentType).Returns(true);
            httpRequest.Setup(p => p.Form).Returns(new CustomFormCollection(value.ToStringValuesDictionary()));

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(JsonSerializer.Serialize(value), JsonSerializer.Serialize(result.Properties.FormData));
        }
コード例 #26
0
        public void InputStreamIsCopied()
        {
            string body = $"Input stream {Guid.NewGuid()}";

            var ms = new MemoryStream(Encoding.UTF8.GetBytes(body));

            var httpRequest = new Mock <HttpRequestBase>();

            httpRequest.Setup(p => p.Url).Returns(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.HttpMethod).Returns("POST");
            httpRequest.Setup(p => p.InputStream).Returns(ms);
            httpRequest.Setup(p => p.Unvalidated.Headers).Returns(Helpers.GenerateNameValueCollection(new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("Content-Type", "text/plain")
            }));

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(body, result.Properties.InputStream);
        }
コード例 #27
0
        public void EvaluatesOptionsShouldLogFormData()
        {
            KissLog.Tests.Common.CommonTestHelpers.ResetContext();

            KissLogConfiguration.Options.ShouldLogFormData((KissLog.Http.HttpRequest args) => false);

            var value = CommonTestHelpers.GenerateList(5);

            var dictionary = CommonTestHelpers.GenerateDictionary(value);

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.Method).Returns("GET");
            httpRequest.Setup(p => p.HasFormContentType).Returns(true);
            httpRequest.Setup(p => p.Form).Returns(new CustomFormCollection(value.ToStringValuesDictionary()));

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(0, result.Properties.FormData.Count());
        }
コード例 #28
0
        public void RemoteIpAddressIsCopied()
        {
            string ipAddress = "82.116.36.117";

            var connectionInfo = new Mock <ConnectionInfo>();

            connectionInfo.Setup(p => p.RemoteIpAddress).Returns(IPAddress.Parse(ipAddress));

            var httpContext = new Mock <HttpContext>();

            httpContext.Setup(p => p.Connection).Returns(connectionInfo.Object);

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.SetUrl(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.Method).Returns("GET");
            httpRequest.Setup(p => p.HttpContext).Returns(httpContext.Object);

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.AreEqual(ipAddress, result.RemoteAddress);
        }
コード例 #29
0
        public void EvaluatesOptionsShouldLogInputStream()
        {
            KissLog.Tests.Common.CommonTestHelpers.ResetContext();

            KissLogConfiguration.Options.ShouldLogInputStream((KissLog.Http.HttpRequest args) => false);

            string body = $"Input stream {Guid.NewGuid()}";

            var ms = new MemoryStream(Encoding.UTF8.GetBytes(body));

            var httpRequest = new Mock <HttpRequestBase>();

            httpRequest.Setup(p => p.Url).Returns(UrlParser.GenerateUri("/Home/Index"));
            httpRequest.Setup(p => p.HttpMethod).Returns("POST");
            httpRequest.Setup(p => p.InputStream).Returns(ms);
            httpRequest.Setup(p => p.Unvalidated.Headers).Returns(Helpers.GenerateNameValueCollection(new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("Content-Type", "text/plain")
            }));

            var result = HttpRequestFactory.Create(httpRequest.Object);

            Assert.IsNull(result.Properties.InputStream);
        }
コード例 #30
0
        /// <summary>
        /// Construct an HttpWebRequest using the <see cref="HttpRequestFactory"/> and
        /// the uri constructed by <see cref="BuildUri"/>.
        /// </summary>
        /// <returns>
        /// An <see cref="HttpWebRequest"/> instance, with the uri and method configured by
        /// this builder.
        /// </returns>
        public HttpWebRequest GetWebRequest()
        {
            Uri uri = BuildUri();

            return(HttpRequestFactory.Create(uri, this.Method));
        }