protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) { Log.Debug("ApplicationStartup"); pipelines.EnableGzipCompression(new GzipCompressionSettings() { MinimumBytes = 1024 }); pipelines.AddHeadersAndLogging(); pipelines.AddRequestStashing(); // rate-limiting - uses remote address by default, but we can extend this to include user-agent and/or // authenticated user if necessary LeakyBucketRateLimiter.Enable(pipelines, new LeakyBucketRateLimiterConfiguration { // 100 requests per minute should be plenty MaxNumberOfRequests = 100, RefreshRate = TimeSpan.FromMinutes(1) }); // setup auth last so that rate-limited responses include authenticated user // (uses AddItemToStartOfPipeline() to ensure it happens before rate-limiter) var auth_method = Env.GetEnvironmentString("COMPUTE_AUTH_METHOD", ""); if (auth_method == "RHINO_ACCOUNT") { pipelines.AddAuthRhinoAccount(); } else if (auth_method == "API_KEY") { pipelines.AddAuthApiKey(); } base.ApplicationStartup(container, pipelines); }
public void Should_throw_with_null_configuration() { var pipelines = A.Fake <IPipelines>(); var module = A.Fake <INancyModule>(); Assert.Throws <ArgumentNullException>(() => LeakyBucketRateLimiter.Enable(pipelines, null)); Assert.Throws <ArgumentNullException>(() => LeakyBucketRateLimiter.Enable(module, null)); }
public void Should_add_before_hook_in_module_when_enabled() { var module = A.Fake <INancyModule>(); LeakyBucketRateLimiter.Enable(module, new LeakyBucketRateLimiterConfiguration()); A.CallTo(() => module.Before.AddItemToStartOfPipeline(A <Func <NancyContext, Response> > .Ignored)) .MustHaveHappened(Repeated.Exactly.Once); }
public void Should_add_before_request_hook_in_application_when_enabled() { var pipelines = A.Fake <IPipelines>(); LeakyBucketRateLimiter.Enable(pipelines, new LeakyBucketRateLimiterConfiguration()); A.CallTo(() => pipelines.BeforeRequest.AddItemToStartOfPipeline(A <Func <NancyContext, Response> > .Ignored)) .MustHaveHappened(Repeated.Exactly.Once); }
private static INancyBootstrapper CreateBootstrapper(LeakyBucketRateLimiterConfiguration configuration) { return(new ConfigurableBootstrapper(config => { config.ApplicationStartup((container, pipelines) => { LeakyBucketRateLimiter.Enable(pipelines, configuration); }); })); }
public void Should_throw_with_null_module_with_configuration() { var config = new LeakyBucketRateLimiterConfiguration(); Assert.Throws <ArgumentNullException>(() => LeakyBucketRateLimiter.Enable((INancyModule)null, config)); }