public IHttpRequest Create(string ip, int port) { var httpRequest = factory.Create(ip, port, false); httpRequests.Add(httpRequest); return(httpRequest); }
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); } } }
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); }
public void Intercept(IInvocation invocation) { var factory = new HttpRequestFactory(_settings); var httpRequest = factory.Create(invocation); _assert(httpRequest); }
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); }
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()); } }
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)); } }
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); }
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); }
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); }
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)); } } }
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()); }
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); }
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)); }
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)); }
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); }
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); }
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)); }
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)); }
/// <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()); }
/// <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()); }
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); }
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()); }
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); }
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)); }
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); }
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()); }
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); }
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); }
/// <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)); }