public void SetUp() { const string verb = "POST"; const string applicationPath = "/"; var uri = new Uri("http://example.com/resources"); var routeParameters = new Dictionary <string, string>(); HttpContextBase.Request.HttpMethod.Returns(verb); HttpContextBase.Request.ApplicationPath.Returns(applicationPath); HttpContextBase.Request.Url.Returns(uri); HttpContextBase.Request.InputStream.Returns(new MemoryStream(Encoding.UTF8.GetBytes("\"request_payload\""))); RequestRouter.FindRequestHandler(verb, applicationPath, uri).Returns(new RouteHandlerLookupResult(typeof(CreateResource), routeParameters)); var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Created); httpResponseMessage.Headers.Location = new Uri("http://example.com/resources/1"); RequestHandlerInvoker.Execute( Arg.Any <CreateResource>(), verb, routeParameters, Arg.Is <IDictionary <string, string> >(x => x.Count == 0), Arg.Any <IDictionary <string, object> >(), Arg.Is <string>(x => x == "request_payload")).Returns(httpResponseMessage); _piccoloContext = new PiccoloContext(HttpContextBase); Engine.ProcessRequest(_piccoloContext); }
public void SetUp() { const string verb = "GET"; const string applicationPath = "/"; var uri = new Uri("http://example.com/resources/1?query=2"); var routeParameters = new Dictionary <string, string> { { "route", "1" } }; HttpContextBase.Request.HttpMethod.Returns(verb); HttpContextBase.Request.ApplicationPath.Returns(applicationPath); HttpContextBase.Request.Url.Returns(uri); var inputStream = Substitute.For <Stream>(); inputStream.CanRead.Returns(false); HttpContextBase.Request.InputStream.Returns(inputStream); RequestRouter.FindRequestHandler(verb, applicationPath, uri).Returns(new RouteHandlerLookupResult(typeof(GetResource), routeParameters)); RequestHandlerInvoker.Execute( Arg.Any <GetResource>(), verb, routeParameters, Arg.Is <IDictionary <string, string> >(x => x["query"] == "2"), Arg.Is <IDictionary <string, object> >(x => (string)x["context"] == "3"), Arg.Is <string>(x => x == null)).Returns(new HttpResponseMessage { Content = new ObjectContent("test") }); _piccoloContext = new PiccoloContext(HttpContextBase); _piccoloContext.Data.context = "3"; Engine.ProcessRequest(_piccoloContext); }
public void SetUp() { const string verb = "DELETE"; const string applicationPath = "/"; var uri = new Uri("http://example.com/resources/1"); var routeParameters = new Dictionary <string, string> { { "route", "1" } }; HttpContextBase.Request.HttpMethod.Returns(verb); HttpContextBase.Request.ApplicationPath.Returns(applicationPath); HttpContextBase.Request.Url.Returns(uri); var inputStream = Substitute.For <Stream>(); inputStream.CanRead.Returns(false); HttpContextBase.Request.InputStream.Returns(inputStream); RequestRouter.FindRequestHandler(verb, applicationPath, uri).Returns(new RouteHandlerLookupResult(typeof(DeleteResource), routeParameters)); RequestHandlerInvoker.Execute( Arg.Any <DeleteResource>(), verb, routeParameters, Arg.Is <IDictionary <string, string> >(x => x.Count == 0), Arg.Any <IDictionary <string, object> >(), Arg.Is <string>(x => x == string.Empty)).Returns(new HttpResponseMessage(HttpStatusCode.NoContent)); _piccoloContext = new PiccoloContext(HttpContextBase); Engine.ProcessRequest(_piccoloContext); }
public void ProcessRequest(PiccoloContext context) { try { _eventDispatcher.RaiseRequestProcessingEvent(context); var lookupResult = _requestRouter.FindRequestHandler(context.RequestVerb, context.RequestUri); if (lookupResult.IsSuccessful) { var requestHandler = _configuration.ObjectFactory.CreateInstance <IRequestHandler>(lookupResult.RequestHandlerType); var httpResponseMessage = _requestHandlerInvoker.Execute(requestHandler, context.RequestVerb, lookupResult.RouteParameters, context.RequestQueryParameters, context.RequestPayload); InjectResponse(context, httpResponseMessage); } else { InjectResponse(context, new HttpResponseMessage(HttpStatusCode.NotFound)); } } catch (Exception ex) { _eventDispatcher.RaiseRequestFaultedEvent(context, ex); InjectResponse(context, new HttpResponseMessage(HttpStatusCode.InternalServerError)); } finally { _eventDispatcher.RaiseRequestProcessedEvent(context); } }
public void it_should_throw_exception() { var handlerInvoker = new RequestHandlerInvoker(null, new Dictionary <Type, IParameterBinder>()); Should.Throw <InvalidOperationException>(() => { var routeParameters = new Dictionary <string, string>(); var queryParameters = new Dictionary <string, string> { { "Param", "value" } }; handlerInvoker.Execute(new Handler(), "GET", routeParameters, queryParameters, null); }); }
public void ProcessRequest(PiccoloContext context) { var lookupResult = _requestRouter.FindRequestHandler(context.RequestVerb, context.RequestUri); if (lookupResult.IsSuccessful) { var requestHandler = _configuration.RequestHandlerFactory.CreateInstance(lookupResult.RequestHandlerType); var httpResponseMessage = _requestHandlerInvoker.Execute(requestHandler, context.RequestVerb, lookupResult.RouteParameters, context.RequestQueryParameters, context.RequestPayload); InjectResponse(context, httpResponseMessage); } else { InjectResponse(context, new HttpResponseMessage(HttpStatusCode.NotFound)); } }