From 904eeb76f91c8629538689280155ea8bffeaefee Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 1 Sep 2014 11:56:11 -0700 Subject: [PATCH] Removing TestClient from TestServer --- src/Microsoft.AspNet.TestHost/TestClient.cs | 209 ------------------ src/Microsoft.AspNet.TestHost/TestServer.cs | 14 -- .../TestClientTests.cs | 116 ++-------- 3 files changed, 21 insertions(+), 318 deletions(-) delete mode 100644 src/Microsoft.AspNet.TestHost/TestClient.cs diff --git a/src/Microsoft.AspNet.TestHost/TestClient.cs b/src/Microsoft.AspNet.TestHost/TestClient.cs deleted file mode 100644 index 57886e56f9..0000000000 --- a/src/Microsoft.AspNet.TestHost/TestClient.cs +++ /dev/null @@ -1,209 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Text; -using System.Threading.Tasks; -using Microsoft.AspNet.Http; -using Microsoft.AspNet.FeatureModel; -using Microsoft.AspNet.HttpFeature; -using Microsoft.AspNet.PipelineCore; - -namespace Microsoft.AspNet.TestHost -{ - public class TestClient - { - private readonly Func _pipeline; - - public TestClient(Func pipeline) - { - _pipeline = pipeline; - } - - public async Task SendAsync(string method, - string url, - IDictionary headers = null, - Stream body = null, - Action onSendingRequest = null) - { - return await SendAsync(method, new Uri(url), headers, body, onSendingRequest); - } - - public async Task SendAsync(string method, - Uri uri, - IDictionary headers = null, - Stream body = null, - Action onSendingRequest = null) - { - var request = CreateRequest(method, uri, headers, body); - var response = new ResponseFeature(); - - var features = new FeatureCollection(); - features.Add(typeof(IHttpRequestFeature), request); - features.Add(typeof(IHttpResponseFeature), response); - var httpContext = new DefaultHttpContext(features); - - if (onSendingRequest != null) - { - onSendingRequest(httpContext.Request); - } - await _pipeline(features); - - response.Body.Seek(0, SeekOrigin.Begin); - return httpContext.Response; - } - - private static IHttpRequestFeature CreateRequest( - string method, - Uri uri, - IDictionary headers, - Stream body) - { - var request = new RequestFeature(); - request.Method = method; - request.Scheme = uri.Scheme; - request.Path = PathString.FromUriComponent(uri).Value; - request.QueryString = QueryString.FromUriComponent(uri).Value; - request.Headers = headers ?? request.Headers; - if (!request.Headers.ContainsKey("Host")) - { - var host = new string[1]; - if (uri.IsDefaultPort) - { - host[0] = uri.Host; - } - else - { - host[0] = uri.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped); - } - request.Headers["Host"] = host; - } - - if (body != null) - { - EnsureContentLength(request.Headers, body); - request.Body = body; - } - else - { - request.Body = Stream.Null; - } - return request; - } - - public async Task GetAsync(string url) - { - var uri = new Uri(url); - return await GetAsync(uri); - } - - public async Task GetAsync(Uri uri) - { - return await SendAsync("GET", uri); - } - - public async Task GetStringAsync(string url) - { - var uri = new Uri(url); - return await GetStringAsync(uri); - } - - public async Task GetStringAsync(Uri uri) - { - var response = await GetAsync(uri); - return await new StreamReader(response.Body).ReadToEndAsync(); - } - - public async Task GetStreamAsync(string url) - { - var uri = new Uri(url); - return await GetStreamAsync(uri); - } - - public async Task GetStreamAsync(Uri uri) - { - var response = await GetAsync(uri); - return response.Body; - } - - public async Task PostAsync( - string url, - string content, - string contentType, - Action onSendingRequest = null) - { - return await PostAsync(new Uri(url), content, contentType, onSendingRequest); - } - - public async Task PostAsync( - Uri url, - string content, - string contentType, - Action onSendingRequest = null) - { - var bytes = GetBytes(content); - var headers = CreateContentHeaders(contentType, bytes.Length); - var body = new MemoryStream(bytes); - - return await SendAsync("POST", url, headers, body, onSendingRequest); - } - - public async Task PutAsync( - string url, - string content, - string contentType, - Action onSendingRequest = null) - { - return await PutAsync(new Uri(url), content, contentType, onSendingRequest); - } - - public async Task PutAsync( - Uri url, - string content, - string contentType, - Action onSendingRequest = null) - { - var bytes = GetBytes(content); - var headers = CreateContentHeaders(contentType, bytes.Length); - var body = new MemoryStream(bytes); - - return await SendAsync("PUT", url, headers, body, onSendingRequest); - } - - public async Task DeleteAsync(string url) - { - return await DeleteAsync(new Uri(url)); - } - - public async Task DeleteAsync(Uri uri) - { - return await SendAsync("DELETE", uri); - } - - private static void EnsureContentLength(IDictionary dictionary, Stream body) - { - if (!dictionary.ContainsKey("Content-Length")) - { - dictionary["Content-Length"] = new[] { body.Length.ToString(CultureInfo.InvariantCulture) }; - } - } - - private static byte[] GetBytes(string content) - { - var bytes = Encoding.UTF8.GetBytes(content); - return bytes; - } - - private static Dictionary CreateContentHeaders(string contentType, int contentLength) - { - return new Dictionary(StringComparer.OrdinalIgnoreCase) - { - { "Content-Type", new [] { contentType } }, - { "Content-Length", new [] { contentLength.ToString(CultureInfo.InvariantCulture) } } - }; - } - } -} diff --git a/src/Microsoft.AspNet.TestHost/TestServer.cs b/src/Microsoft.AspNet.TestHost/TestServer.cs index 88f25d083f..b8df467d80 100644 --- a/src/Microsoft.AspNet.TestHost/TestServer.cs +++ b/src/Microsoft.AspNet.TestHost/TestServer.cs @@ -20,7 +20,6 @@ namespace Microsoft.AspNet.TestHost private static readonly string ServerName = typeof(TestServer).FullName; private static readonly ServerInformation ServerInfo = new ServerInformation(); private Func _appDelegate; - private TestClient _handler; private IDisposable _appInstance; private bool _disposed = false; @@ -45,19 +44,6 @@ namespace Microsoft.AspNet.TestHost _appInstance = engine.Start(hostContext); } - public TestClient Handler - { - get - { - if (_handler == null) - { - _handler = new TestClient(Invoke); - } - - return _handler; - } - } - public static TestServer Create(Action app) { return Create(provider: CallContextServiceLocator.Locator.ServiceProvider, app: app); diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs index 96fbfc04b0..d9843657dd 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs @@ -2,8 +2,8 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.IO; +using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; @@ -25,114 +25,41 @@ namespace Microsoft.AspNet.TestHost .AddSingleton() .BuildServiceProvider(); - _server = TestServer.Create(_services, app => app.Run(async ctx => { })); + _server = TestServer.Create(_services, app => app.Run(ctx => Task.FromResult(0))); } [Fact] - public async Task SendAsync_ConfiguresRequestProperly() + public async Task GetAsyncWorks() { // Arrange - var client = _server.Handler; + var expected = "GET Response"; + RequestDelegate appDelegate = ctx => + ctx.Response.WriteAsync(expected); + var server = TestServer.Create(_services, app => app.Run(appDelegate)); + var client = server.CreateClient(); // Act - var response = await client.SendAsync("GET", "http://localhost:12345/Home/Index?id=3&name=peter#fragment"); - var request = response.HttpContext.Request; + var actual = await client.GetStringAsync("http://localhost:12345"); // Assert - Assert.NotNull(request); - Assert.Equal("HTTP/1.1", request.Protocol); - Assert.Equal("GET", request.Method); - Assert.Equal("http", request.Scheme); - Assert.Equal("localhost:12345", request.Host.Value); - Assert.Equal("", request.PathBase.Value); - Assert.True(request.Path.HasValue); - Assert.Equal("/Home/Index", request.Path.Value); - Assert.Equal("?id=3&name=peter", request.QueryString.Value); - Assert.Null(request.ContentLength); - Assert.Equal(1, request.Headers.Count); - Assert.True(request.Headers.ContainsKey("Host")); - } - - [Fact] - public async Task SendAsync_InvokesCallbackWhenPassed() - { - // Arrange - var client = _server.Handler; - var invoked = false; - - // Act - var response = await client.SendAsync("GET", "http://localhost:12345/", null, null, _ => invoked = true); - - // Assert - Assert.True(invoked); - } - - [Fact] - public async Task SendAsync_RespectsExistingHost() - { - // Arrange - var client = _server.Handler; - var headers = new Dictionary { { "Host", new string[] { "server:12345" } } }; - - // Act - var response = await client.SendAsync("GET", "http://localhost:12345/Home/", headers); - var request = response.HttpContext.Request; - - // Assert - Assert.Equal("server:12345", request.Host.Value); - } - - [Fact] - public async Task SendAsync_RespectsArgumentBody() - { - // Arrange - var client = _server.Handler; - var headers = new Dictionary { { "Content-Type", new string[] { "text/plain" } } }; - var body = new MemoryStream(); - new StreamWriter(body).Write("Hello world"); - body.Position = 0; - - // Act - var response = await client.SendAsync("POST", "http://host/", headers, body); - var request = response.HttpContext.Request; - - // Assert - Assert.Same(body, request.Body); - Assert.Equal(0, request.Body.Position); - Assert.Equal(body.Length, request.ContentLength); - } - - [Fact] - public async Task SendAsync_RewindsTheResponseStream() - { - // Arrange - var server = TestServer.Create(_services, app => app.Run(ctx => ctx.Response.WriteAsync("Hello world"))); - var client = server.Handler; - - // Act - var response = await client.SendAsync("GET", "http://localhost"); - - // Assert - Assert.Equal(0, response.Body.Position); - Assert.Equal("Hello world", new StreamReader(response.Body).ReadToEnd()); + Assert.Equal(expected, actual); } [Fact] public async Task PutAsyncWorks() { // Arrange - RequestDelegate appDelegate = async ctx => - await ctx.Response.WriteAsync(new StreamReader(ctx.Request.Body).ReadToEnd()); + RequestDelegate appDelegate = ctx => + ctx.Response.WriteAsync(new StreamReader(ctx.Request.Body).ReadToEnd() + " PUT Response"); var server = TestServer.Create(_services, app => app.Run(appDelegate)); - var client = server.Handler; + var client = server.CreateClient(); // Act - var response = await client.PutAsync("http://localhost:12345", "Hello world", "text/plain"); - var request = response.HttpContext.Request; + var content = new StringContent("Hello world"); + var response = await client.PutAsync("http://localhost:12345", content); // Assert - Assert.Equal("PUT", request.Method); - Assert.Equal("Hello world", new StreamReader(response.Body).ReadToEnd()); + Assert.Equal("Hello world PUT Response", await response.Content.ReadAsStringAsync()); } [Fact] @@ -140,17 +67,16 @@ namespace Microsoft.AspNet.TestHost { // Arrange RequestDelegate appDelegate = async ctx => - await ctx.Response.WriteAsync(new StreamReader(ctx.Request.Body).ReadToEnd()); + await ctx.Response.WriteAsync(new StreamReader(ctx.Request.Body).ReadToEnd() + " POST Response"); var server = TestServer.Create(_services, app => app.Run(appDelegate)); - var client = server.Handler; + var client = server.CreateClient(); // Act - var response = await client.PostAsync("http://localhost:12345", "Hello world", "text/plain"); - var request = response.HttpContext.Request; + var content = new StringContent("Hello world"); + var response = await client.PostAsync("http://localhost:12345", content); // Assert - Assert.Equal("POST", request.Method); - Assert.Equal("Hello world", new StreamReader(response.Body).ReadToEnd()); + Assert.Equal("Hello world POST Response", await response.Content.ReadAsStringAsync()); } } }