public void Context_passed_to_constructor_is_passed_through_and_not_modified_by_requests() { var context = new Dictionary<string, object>() { { "Key", "Value" } }; var app = new StaticApp(null, null, null); IDictionary<string, object> appContext = null; app.OnRequest = () => { app.Env["OtherKey"] = "OtherValue"; appContext = app.Env; }; var requestDelegate = new GateRequestDelegate(app.Invoke, context); requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate); Assert.That(context.ContainsKey("Key"), Is.True); Assert.That(context["Key"], Is.EqualTo("Value")); Assert.That(context.ContainsKey("OtherKey"), Is.False); Assert.That(appContext.ContainsKey("Key"), Is.True); Assert.That(appContext["Key"], Is.EqualTo("Value")); Assert.That(appContext.ContainsKey("OtherKey"), Is.True); Assert.That(appContext["OtherKey"], Is.EqualTo("OtherValue")); }
public static IDisposable Create(AppDelegate app, int port, TextWriter output) { app = ExecutionContextPerRequest.Middleware(app); var endPoint = new IPEndPoint(IPAddress.Any, port); var schedulerDelegate = new NullSchedulerDelegate(output); var scheduler = KayakScheduler.Factory.Create(schedulerDelegate); var context = new Dictionary<string, object> { {"gate.Output", output}, }; var channel = new GateRequestDelegate(app, context); var server = KayakServer.Factory.CreateHttp(channel, scheduler); var listen = server.Listen(endPoint); var thread = new Thread(_ => scheduler.Start()); thread.Start(); return new Disposable(() => { scheduler.Stop(); thread.Join(5000); listen.Dispose(); server.Dispose(); }); }
public static IDisposable Create(AppFunc app, int port, TextWriter output) { app = ExecutionContextPerRequest.Middleware(app); var endPoint = new IPEndPoint(IPAddress.Any, port); var schedulerDelegate = new NullSchedulerDelegate(output); var scheduler = KayakScheduler.Factory.Create(schedulerDelegate); var context = new Dictionary <string, object> { { "gate.Output", output }, }; var channel = new GateRequestDelegate(app, context); var server = KayakServer.Factory.CreateHttp(channel, scheduler); var listen = server.Listen(endPoint); var thread = new Thread(_ => scheduler.Start()); thread.Start(); return(new Disposable(() => { scheduler.Stop(); thread.Join(5000); listen.Dispose(); server.Dispose(); })); }
public void Adds_connection_close_response_header_if_no_length_or_encoding() { var app = new StaticApp( "200 OK", new Dictionary<string, string[]>(), (write, end, cancel) => { write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("12345")), null); write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("67890")), null); end(null); }); var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary<string, object>()); requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate); Assert.That(new Environment(app.Env).BodyAction, Is.Null); Assert.That(mockResponseDelegate.Head.Headers.Keys, Contains.Item("Connection")); Assert.That(mockResponseDelegate.Head.Headers["Connection"], Is.EqualTo("close")); var body = mockResponseDelegate.Body.Consume(); Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890")); Assert.That(body.GotEnd, Is.True); }
public void Does_not_add_content_length_response_header_if_transfer_encoding_chunked() { var app = new StaticApp( "200 OK", new Dictionary<string, string[]>() { { "Transfer-Encoding", new[]{"chunked"} } }, (write, end, cancel) => { write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("12345")), null); write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("67890")), null); end(null); }); var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary<string, object>()); requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate); Assert.That(new Environment(app.Env).BodyAction, Is.Null); Assert.IsFalse(mockResponseDelegate.Head.Headers.Keys.Contains("Content-Length"), "should not contain Content-Length"); // chunks are not chunked-encoded at this level. eventually kayak will do this automatically. var body = mockResponseDelegate.Body.Consume(); Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890")); Assert.That(body.GotEnd, Is.True); }
public void Request_body_is_passed_through() { var app = new StaticApp(null, null, null); var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary<string, object>()); requestDelegate.OnRequest(new HttpRequestHead() { }, new MockDataProducer(c => { c.OnData(new ArraySegment<byte>(Encoding.ASCII.GetBytes("12345")), null); c.OnData(new ArraySegment<byte>(Encoding.ASCII.GetBytes("67890")), null); c.OnEnd(); return () => { }; }), mockResponseDelegate); var bodyDelegate = new Environment(app.Env).BodyDelegate; Assert.That(bodyDelegate, Is.Not.Null); var body = bodyDelegate.Consume(); Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890")); Assert.That(body.GotEnd, Is.True); }
public void Environment_items_conform_to_spec_by_default() { var app = new StaticApp(null, null, null); IDictionary<string, object> appContext = null; app.OnRequest = () => { appContext = app.Env; }; var requestDelegate = new GateRequestDelegate(app.Invoke, null); requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate); var env = new Environment(appContext); Assert.That(env.BodyAction, Is.Null); Assert.That(env.Headers, Is.Not.Null); Assert.That(env.Method, Is.Not.Null); Assert.That(env.Path, Is.Not.Null); Assert.That(env.PathBase, Is.Not.Null); Assert.That(env.QueryString, Is.Not.Null); Assert.That(env.Scheme, Is.EqualTo("http")); Assert.That(env.Version, Is.EqualTo("1.0")); }