Skip to content

Instantly share code, notes, and snippets.

@dealproc
Created February 12, 2016 13:53
Show Gist options
  • Save dealproc/8525d83c47a2cbb46621 to your computer and use it in GitHub Desktop.
Save dealproc/8525d83c47a2cbb46621 to your computer and use it in GitHub Desktop.
base test class for nancy
using FakeItEasy;
using Nancy.Owin;
using Nancy.Testing;
using Nancy.TinyIoc;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading.Tasks;
namespace {project}.Tests {
public abstract class ApiBaseTest {
protected AppBuilder AppBuilder { get; private set; }
protected ConnectTestsStartup Startup { get; private set; }
private Func<IDictionary<string, object>, Task> _app;
protected IPrincipal Store {
get {
return new ClaimsPrincipal(new ClaimsIdentity(new Claim[] {
new Claim(ClaimTypes.NameIdentifier, "StoreId"),
new Claim(Core.Infrastructure.Constants.ClaimTypes.HardwareID, "HARDWAREID"),
new Claim(Core.Infrastructure.Constants.ClaimTypes.TenantId, "TENANTID"),
}, "bearer"));
}
}
protected IPrincipal NonStore {
get {
return new ClaimsPrincipal(new ClaimsIdentity(new Claim[] {
new Claim(ClaimTypes.NameIdentifier, "StoreId"),
new Claim(Core.Infrastructure.Constants.ClaimTypes.TenantId, "TENANTID"),
}, "bearer"));
}
}
public IPrincipal Anonymous {
get { return new GenericPrincipal(new GenericIdentity(string.Empty), new string[0]); }
}
public ApiBaseTest(params Type[] modulesToTest) {
AppBuilder = new AppBuilder();
Startup = new ConnectTestsStartup();
Startup.Configure(AppBuilder, modulesToTest);
_app = AppBuilder.Build();
}
protected async Task WithContext(Func<HttpClient, Task> test) {
using (var handler = new OwinHttpMessageHandler(_app))
using (var client = new HttpClient(handler) { BaseAddress = new Uri("https://local/") }) {
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
await test.Invoke(client);
}
}
protected class ConnectTestsStartup {
public Func<IPrincipal> ResolvePrincipal = () => new GenericPrincipal(new GenericIdentity(""), new string[] { });
public ConnectTestsBootstrapper Bootstrapper { get; private set; }
public void Configure(IAppBuilder app, IEnumerable<Type> modulesToTest) {
Bootstrapper = new ConnectTestsBootstrapper(modulesToTest);
app.Use(async (ctx, next) => {
if (ctx.Request.User == null) { ctx.Request.User = ResolvePrincipal.Invoke(); };
await next();
});
app.UseNancy(new NancyOptions() { Bootstrapper = Bootstrapper });
}
}
protected class ConnectTestsBootstrapper : ConfigurableBootstrapper {
public ConnectTestsBootstrapper(IEnumerable<Type> modulesToTest) : base(cfg => {
cfg.Modules(modulesToTest.ToArray());
}) {
}
protected override void ConfigureApplicationContainer(TinyIoCContainer container) {
base.ConfigureApplicationContainer(container);
}
}
}
}
@cottsak
Copy link

cottsak commented Feb 16, 2016

@dealproc does this [test] open a socket?

@dealproc
Copy link
Author

dealproc commented Mar 3, 2016

@cottsak - my apologies in the delay on this... to the best of my knowledge, it should not. This is using libs with an http message handler that are not dependent on networking. Take a look at https://github.com/damianh/OwinHttpMessageHandler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment