Skip to content

Instantly share code, notes, and snippets.

@damianh
Last active December 15, 2015 17:49
Show Gist options
  • Select an option

  • Save damianh/5299138 to your computer and use it in GitHub Desktop.

Select an option

Save damianh/5299138 to your computer and use it in GitHub Desktop.
namespace Microsoft.Owin.Testing.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Xunit;
public class TestServerTests
{
[Fact]
public async Task HangsIndefintely()
{
// because task is never completed
var testServer = TestServer.Create(builder => builder.Use(typeof(CustomOwinApp), false));
await testServer.HttpClient.GetAsync("http://example.com/custom");
}
[Fact]
public async Task ThrowsObjectDisposedException()
{
// because the (memory)stream is closed by the app, and the server attempts to
// seek, ObjectDisposedException occurs.
var testServer = TestServer.Create(builder => builder.Use(typeof(CustomOwinApp), true));
await testServer.HttpClient.GetAsync("http://example.com/custom");
}
public class CustomOwinApp
{
private readonly Func<IDictionary<string, object>, Task> _next;
private readonly bool _completeTheTask;
public CustomOwinApp(Func<IDictionary<string, object>, Task> next, bool completeTheTask)
{
_next = next;
_completeTheTask = completeTheTask;
}
public Task Invoke(IDictionary<string, object> environment)
{
var path = environment.Get<string>("owin.RequestPath");
if (path == "/custom")
{
var tcs = new TaskCompletionSource<int>();
var headers = environment.Get<IDictionary<string, string[]>>("owin.ResponseHeaders");
headers["Content-Type"] = new[] { "text/html" };
var body = environment.Get<Stream>("owin.ResponseBody");
using (var writer = new StreamWriter(body))
{
writer.Write("Response from the custom host2.");
}
if (_completeTheTask)
{
tcs.SetResult(0);
}
return tcs.Task;
}
return _next.Invoke(environment);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment