83 lines
2.8 KiB
C#
83 lines
2.8 KiB
C#
// 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.IO;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNet.Builder;
|
|
using Microsoft.AspNet.Http;
|
|
using Microsoft.Framework.DependencyInjection;
|
|
using Microsoft.Framework.DependencyInjection.Fallback;
|
|
using Microsoft.Framework.Runtime;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.AspNet.TestHost
|
|
{
|
|
public class TestClientTests
|
|
{
|
|
private readonly IServiceProvider _services;
|
|
private readonly TestServer _server;
|
|
|
|
public TestClientTests()
|
|
{
|
|
_services = new ServiceCollection()
|
|
.AddSingleton<IApplicationEnvironment, TestApplicationEnvironment>()
|
|
.BuildServiceProvider();
|
|
|
|
_server = TestServer.Create(_services, app => app.Run(ctx => Task.FromResult(0)));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAsyncWorks()
|
|
{
|
|
// Arrange
|
|
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 actual = await client.GetStringAsync("http://localhost:12345");
|
|
|
|
// Assert
|
|
Assert.Equal(expected, actual);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PutAsyncWorks()
|
|
{
|
|
// Arrange
|
|
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.CreateClient();
|
|
|
|
// Act
|
|
var content = new StringContent("Hello world");
|
|
var response = await client.PutAsync("http://localhost:12345", content);
|
|
|
|
// Assert
|
|
Assert.Equal("Hello world PUT Response", await response.Content.ReadAsStringAsync());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PostAsyncWorks()
|
|
{
|
|
// Arrange
|
|
RequestDelegate appDelegate = async ctx =>
|
|
await ctx.Response.WriteAsync(new StreamReader(ctx.Request.Body).ReadToEnd() + " POST Response");
|
|
var server = TestServer.Create(_services, app => app.Run(appDelegate));
|
|
var client = server.CreateClient();
|
|
|
|
// Act
|
|
var content = new StringContent("Hello world");
|
|
var response = await client.PostAsync("http://localhost:12345", content);
|
|
|
|
// Assert
|
|
Assert.Equal("Hello world POST Response", await response.Content.ReadAsStringAsync());
|
|
}
|
|
}
|
|
}
|