Skip to content

Instantly share code, notes, and snippets.

@damianh
Last active December 21, 2015 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save damianh/6332906 to your computer and use it in GitHub Desktop.
Save damianh/6332906 to your computer and use it in GitHub Desktop.
namespace TestinNancyWithOwinTesting
{
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Testing;
using Owin;
using Microsoft.Owin.Testing;
using Xunit;
public class Class1
{
[Fact]
public void NancyTest()
{
// Arrange
var browser = new Browser(with => with.Module<ConfigBootTestModule>());
// Act
BrowserResponse response = browser.Get("/config");
// Assert
Assert.Equal("Hello configured fellow", response.Body.AsString());
}
[Fact]
public async Task OwinTest()
{
// Arrange
var httpClient = CreateHttpClient(with => with.Module<ConfigBootTestModule>());
// Act
var response = await httpClient.GetAsync("http://localhost/config");
var body = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal("Hello configured fellow", body);
}
private static HttpClient CreateHttpClient(Action<ConfigurableBootstrapper.ConfigurableBootstrapperConfigurator> configureBootstrapperAction)
{
return TestServer.Create(builder =>
new Startup(new ConfigurableBootstrapper(configureBootstrapperAction)).Configuration(builder))
.CreateHttpClient();
}
private class Startup
{
private readonly INancyBootstrapper _nancyBootstrapper;
public Startup(INancyBootstrapper nancyBootstrapper)
{
_nancyBootstrapper = nancyBootstrapper;
}
public void Configuration(IAppBuilder builder)
{
builder.UseNancy(_nancyBootstrapper);
}
}
}
public class ConfigBootTestModule : NancyModule
{
public ConfigBootTestModule() : base("/config")
{
Get["/"] = p => "Hello configured fellow";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment