Created
February 12, 2016 13:53
-
-
Save dealproc/8525d83c47a2cbb46621 to your computer and use it in GitHub Desktop.
base test class for nancy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 - 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
@dealproc does this [test] open a socket?