public void Async_Exception_in_response_body_stream_should_be_formatted_as_it_passes_through() { var stack = Build(b => b .UseShowExceptions() .UseFunc<AppDelegate>(_=>appCall => { Response appResult = new Response(200); appResult.Headers.SetHeader("Content-Type", "text/html"); appResult.StartAsync().Then( resp1 => { resp1.Write("<p>so far so good</p>"); resp1.Error(new ApplicationException("failed sending body async")); }); return appResult.ResultTask; })); ResultParameters result = stack(new Request().Call).Result; Assert.That(result.Status, Is.EqualTo(200)); Assert.That(result.Headers.GetHeader("Content-Type"), Is.EqualTo("text/html")); String bodyText = ReadBody(result.Body); Assert.That(bodyText, Is.StringContaining("<p>so far so good</p>")); Assert.That(bodyText, Is.StringContaining("failed sending body async")); }
public Task<ResultParameters> Invoke(CallParameters call) { var request = new Request(call); var response = new Response() { ContentType = "text/html", }; var wilson = "left - right\r\n123456789012\r\nhello world!\r\n"; response.StartAsync().Then(resp1 => { var href = "?flip=left"; if (request.Query["flip"] == "left") { wilson = wilson.Split(new[] {System.Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).Select(line => new string(line.Reverse().ToArray())).Aggregate("", (agg, line) => agg + line + System.Environment.NewLine); href = "?flip=right"; } return TimerLoop(350, () => resp1.Write("<title>Hutchtastic</title>"), () => resp1.Write("<pre>"), () => resp1.Write(wilson), () => resp1.Write("</pre>"), () => { if (request.Query["flip"] == "crash") { throw new ApplicationException("Wilson crashed!"); } }, () => resp1.Write("<p><a href='" + href + "'>flip!</a></p>"), () => resp1.Write("<p><a href='?flip=crash'>crash!</a></p>"), () => resp1.End()); }).Catch(errorInfo => { response.Error(errorInfo.Exception); return errorInfo.Handled(); }); return response.ResultTask; }
public static AppDelegate AsyncApp() { return call => { var request = new Request(call); var response = new Response { ContentType = "text/html", }; var wilson = "left - right\r\n123456789012\r\nhello world!\r\n"; response.StartAsync() .Then(()=> { Delay }); return response.GetResultAsync(); ThreadPool.QueueUserWorkItem(_ => { try { var href = "?flip=left"; if (request.Query["flip"] == "left") { wilson = wilson.Split(new[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) .Select(line => new string(line.Reverse().ToArray())) .Aggregate("", (agg, line) => agg + line + System.Environment.NewLine); href = "?flip=right"; } response.Start(() => TimerLoop(350, response.Error, () => response.Write("<title>Hutchtastic</title>"), () => response.Write("<pre>"), () => response.Write(wilson), () => response.Write("</pre>"), () => { if (request.Query["flip"] == "crash") { throw new ApplicationException("Wilson crashed!"); } }, () => response.Write("<p><a href='" + href + "'>flip!</a></p>"), () => response.Write("<p><a href='?flip=crash'>crash!</a></p>"), response.End)); } catch (Exception ex) { callback(default(ResultParameters), ex); } }); }; }