コード例 #1
0
        public void SendAsync_Direct_Returns_OK_For_Successful_ObjectContent_Write(string uri, TransferMode transferMode)
        {
            using (var port = new PortReserver())
            {
                // Arrange & Act
                server = CreateServer(port, transferMode);
                HttpResponseMessage response = new HttpClient().GetAsync(BaseUri(port, transferMode) + uri).Result;
                string responseString = response.Content.ReadAsStringAsync().Result;

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal("\"echoString\"", responseString);
            }
        }
コード例 #2
0
        public void SimpleGet_Works()
        {
            using (var port = new PortReserver())
            using (WebApp.Start<OwinHostIntegrationTest>(url: CreateBaseUrl(port)))
            {
                HttpClient client = new HttpClient();

                var response = client.GetAsync(CreateUrl(port, "HelloWorld")).Result;

                Assert.True(response.IsSuccessStatusCode);
                Assert.Equal("\"Hello from OWIN\"", response.Content.ReadAsStringAsync().Result);
                Assert.Null(response.Headers.TransferEncodingChunked);
            }
        }
コード例 #3
0
        public void SimplePost_Works()
        {
            using (var port = new PortReserver())
            using (WebApp.Start<OwinHostIntegrationTest>(url: CreateBaseUrl(port)))
            {
                HttpClient client = new HttpClient();
                var content = new StringContent("\"Echo this\"", Encoding.UTF8, "application/json");

                var response = client.PostAsync(CreateUrl(port, "Echo"), content).Result;

                Assert.True(response.IsSuccessStatusCode);
                Assert.Equal("\"Echo this\"", response.Content.ReadAsStringAsync().Result);
                Assert.Null(response.Headers.TransferEncodingChunked);
            }
        }
コード例 #4
0
        public void GetThatThrowsDuringSerializations_RespondsWith500()
        {
            using (var port = new PortReserver())
            using (WebApp.Start<OwinHostIntegrationTest>(url: CreateBaseUrl(port)))
            {
                HttpClient client = new HttpClient();

                var response = client.GetAsync(CreateUrl(port, "Error")).Result;

                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
                JObject json = Assert.IsType<JObject>(JToken.Parse(response.Content.ReadAsStringAsync().Result));
                JToken exceptionMessage;
                Assert.True(json.TryGetValue("ExceptionMessage", out exceptionMessage));
                Assert.Null(response.Headers.TransferEncodingChunked);
            }
        }
コード例 #5
0
        public void SendAsync_Direct_Returns_OK_For_Successful_Stream_Write(string uri, TransferMode transferMode)
        {
            using (var port = new PortReserver())
            {
                // Arrange & Act
                server = CreateServer(port, transferMode);
                HttpResponseMessage response = new HttpClient(server).GetAsync(BaseUri(port, transferMode) + uri).Result;
                string responseString = response.Content.ReadAsStringAsync().Result;
                IEnumerable<string> headerValues = null;
                bool isChunked = response.Headers.TryGetValues("Transfer-Encoding", out headerValues) && headerValues != null &&
                                 headerValues.Any((v) => String.Equals(v, "chunked", StringComparison.OrdinalIgnoreCase));

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal("echoStream", responseString);
                Assert.False(isChunked);    // stream never chunk, buffered or streamed
            }
        }
コード例 #6
0
        public void SendAsync_ServiceModel_Returns_OK_For_Successful_ObjectContent_Write(string uri, TransferMode transferMode)
        {
            using (var port = new PortReserver())
            {
                // Arrange
                server = CreateServer(port, transferMode);
                bool shouldChunk = transferMode == TransferMode.Streamed;

                // Act
                HttpResponseMessage response = new HttpClient().GetAsync(BaseUri(port, transferMode) + uri).Result;
                string responseString = response.Content.ReadAsStringAsync().Result;
                IEnumerable<string> headerValues = null;
                bool isChunked = response.Headers.TryGetValues("Transfer-Encoding", out headerValues) && headerValues != null &&
                                 headerValues.Any((v) => String.Equals(v, "chunked", StringComparison.OrdinalIgnoreCase));

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal("\"echoString\"", responseString);
                Assert.Equal(shouldChunk, isChunked);
            }
        }
コード例 #7
0
        private static void RunBasicAuthTest(string controllerName, string routeSuffix, NetworkCredential credential, Action<HttpResponseMessage> assert)
        {
            using (var port = new PortReserver())
            {
                // Arrange
                HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(port.BaseUri);
                config.HostNameComparisonMode = HostNameComparisonMode.Exact;
                config.Routes.MapHttpRoute("Default", "{controller}" + routeSuffix, new { controller = controllerName });
                config.UserNamePasswordValidator = new CustomUsernamePasswordValidator();
                config.MessageHandlers.Add(new CustomMessageHandler());
                HttpSelfHostServer server = new HttpSelfHostServer(config);

                server.OpenAsync().Wait();

                // Create a GET request with correct username and password
                HttpClientHandler handler = new HttpClientHandler();
                handler.Credentials = credential;
                HttpClient client = new HttpClient(handler);

                HttpResponseMessage response = null;
                try
                {
                    // Act
                    response = client.GetAsync(port.BaseUri).Result;

                    // Assert
                    assert(response);
                }
                finally
                {
                    if (response != null)
                    {
                        response.Dispose();
                    }
                    client.Dispose();
                }

                server.CloseAsync().Wait();
            }
        }
コード例 #8
0
        public void SendAsync_ServiceModel_AddsSelfHostHttpRequestContext()
        {
            // Arrange
            using (PortReserver port = new PortReserver())
            {
                string baseUri = port.BaseUri;

                HttpRequestContext context = null;
                Uri via = null;

                Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> sendAsync = (r, c) =>
                {
                    if (r != null)
                    {
                        context = r.GetRequestContext();
                    }

                    SelfHostHttpRequestContext typedContext = context as SelfHostHttpRequestContext;

                    if (typedContext != null)
                    {
                        via = typedContext.RequestContext.RequestMessage.Properties.Via;
                    }

                    return Task.FromResult(new HttpResponseMessage());
                };

                using (HttpSelfHostConfiguration expectedConfiguration = new HttpSelfHostConfiguration(baseUri))
                {
                    expectedConfiguration.HostNameComparisonMode = HostNameComparisonMode.Exact;

                    using (HttpMessageHandler dispatcher = new LambdaHttpMessageHandler(sendAsync))
                    using (HttpSelfHostServer server = new HttpSelfHostServer(expectedConfiguration, dispatcher))
                    using (HttpClient client = new HttpClient())
                    using (HttpRequestMessage expectedRequest = new HttpRequestMessage(HttpMethod.Get, baseUri))
                    {
                        server.OpenAsync().Wait();

                        // Act
                        using (HttpResponseMessage ignore = client.SendAsync(expectedRequest).Result)
                        {
                            // Assert
                            SelfHostHttpRequestContext typedContext = (SelfHostHttpRequestContext)context;
                            Assert.Equal(expectedRequest.RequestUri, via);
                            Assert.Same(expectedConfiguration, context.Configuration);
                            Assert.Equal(expectedRequest.RequestUri, typedContext.Request.RequestUri);

                            server.CloseAsync().Wait();
                        }
                    }
                }
            }
        }
コード例 #9
0
 private static string BaseUri(PortReserver port, TransferMode transferMode)
 {
     return transferMode == TransferMode.Streamed
         ? port.BaseUri + "stream"
         : port.BaseUri;
 }
コード例 #10
0
        private static HttpSelfHostServer CreateServer(PortReserver port, TransferMode transferMode, bool ignoreRoute = false)
        {
            HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(BaseUri(port, transferMode));
            config.HostNameComparisonMode = HostNameComparisonMode.Exact;
            if (ignoreRoute)
            {
                config.Routes.IgnoreRoute("Ignore", "{controller}/{action}");
                config.Routes.IgnoreRoute("IgnoreWithConstraints", "constraint/values/{id}", constraints: new { constraint = new CustomConstraint() });
            }
            config.Routes.MapHttpRoute("Default", "{controller}/{action}");
            config.Routes.MapHttpRoute("Other", "other/{controller}/{action}");
            config.TransferMode = transferMode;
            config.MapHttpAttributeRoutes();

            HttpSelfHostServer server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();
            return server;
        }
コード例 #11
0
 private static string CreateBaseUrl(PortReserver port)
 {
     return port.BaseUri + "vroot";
 }
コード例 #12
0
        public void Get_Returns_Success_If_OtherRouteMatched(string uri, TransferMode transferMode)
        {
            using (var port = new PortReserver())
            {
                // Arrange & Act
                server = CreateServer(port, transferMode, ignoreRoute: true);
                HttpResponseMessage response = new HttpClient().GetAsync(BaseUri(port, transferMode) + uri).Result;
                string responseString = response.Content.ReadAsStringAsync().Result;

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal("\"echoString\"", responseString);
            }
        }
コード例 #13
0
        public void Get_Returns_Value_If_IgnoreRoute_WithConstraints_ConstraintsNotMatched(string uri, TransferMode transferMode)
        {
            using (var port = new PortReserver())
            {
                // Arrange & Act
                server = CreateServer(port, transferMode, ignoreRoute: true);
                HttpResponseMessage response = new HttpClient().GetAsync(BaseUri(port, transferMode) + uri).Result;
                string responseString = response.Content.ReadAsStringAsync().Result;

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(String.Concat("/constraint/values/", responseString), uri);
            }
        }
コード例 #14
0
        public void Get_Returns_Hard404_If_IgnoreRoute_WithConstraints_ConstraintsMatched(string uri, TransferMode transferMode)
        {
            using (var port = new PortReserver())
            {
                // Arrange & Act
                server = CreateServer(port, transferMode, ignoreRoute: true);
                HttpResponseMessage response = new HttpClient().GetAsync(BaseUri(port, transferMode) + uri).Result;

                // Assert
                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
                Assert.False(response.RequestMessage.Properties.ContainsKey(HttpPropertyKeys.NoRouteMatched));
            }
        }
コード例 #15
0
 private static string CreateUrl(PortReserver port, string localPath)
 {
     return CreateBaseUrl(port) + "/" + localPath;
 }
コード例 #16
0
        public void SendAsync_ServiceModel_Throws_When_StreamContent_Throws(string uri, TransferMode transferMode)
        {
            using (var port = new PortReserver())
            {
                // Arrange
                server = CreateServer(port, transferMode);
                Task task = new HttpClient().GetAsync(BaseUri(port, transferMode) + uri);

                // Act & Assert
                Assert.Throws<HttpRequestException>(() => task.Wait());
            }
        }
コード例 #17
0
 public void SendAsync_Direct_Throws_When_StreamContent_Throws(string uri, TransferMode transferMode)
 {
     using (var port = new PortReserver())
     {
         // Arrange & Act & Assert
         server = CreateServer(port, transferMode);
         Assert.Throws<InvalidOperationException>(
             () => new HttpClient(server).GetAsync(BaseUri(port, transferMode) + uri).Wait());
     }
 }
コード例 #18
0
        public void SendAsync_ServiceModel_Closes_Connection_When_ObjectContent_CopyToAsync_Throws(string uri, TransferMode transferMode)
        {
            using (var port = new PortReserver())
            {
                // Arrange
                server = CreateServer(port, transferMode);
                Task<HttpResponseMessage> task = new HttpClient().GetAsync(BaseUri(port, transferMode) + uri);

                // Act & Assert
                Assert.Throws<HttpRequestException>(() => task.Wait());
            }
        }