コード例 #1
0
 /// <summary>
 /// Add a controller
 /// </summary>
 /// <param name="controller"></param>
 public void Push(RequestController controller)
 {
     Check.Require(controller, "controller");
     lock (_queue)
     {
         _queue.Enqueue(controller);
     }
 }
コード例 #2
0
 /// <summary>
 /// create a new request controller
 /// </summary>
 /// <param name="controller">prototype to copy information from</param>
 protected RequestController(RequestController controller)
 {
     _beforeFilters = controller._beforeFilters;
     _binaryMethods = controller._binaryMethods;
     _authMethods = controller._authMethods;
     _methods = controller._methods;
     _controllerName = controller.ControllerName;
     _defaultMethod = controller._defaultMethod;
     _defaultMethodStr = controller._defaultMethodStr;
     _authValidator = controller._authValidator;
 }
コード例 #3
0
// ReSharper disable SuggestBaseTypeForParameter
        private string Invoke(RequestController controller, string httpMetod, string uri, HttpForm form, out IHttpResponse response, IHttpSession session)
// ReSharper restore SuggestBaseTypeForParameter
        {
            HttpRequest request = new HttpRequest
            {
                HttpVersion = "HTTP/1.1",
                UriPath     = uri,
                Method      = httpMetod,
                Uri         = new Uri(HostName + uri)
            };

            request.AssignForm(form);

            response = request.CreateResponse(TestContext);
            if (!controller.Process(request, response, session))
            {
                throw new NotFoundException("404 could not find processor for: " + uri);
            }

            response.Body.Seek(0, SeekOrigin.Begin);
            StreamReader reader = new StreamReader(response.Body);

            return(reader.ReadToEnd());
        }
コード例 #4
0
        //private ILogWriter _logger;

        /// <summary>
        /// The controller module uses the prototype design pattern
        /// to be able to create new controller objects for requests
        /// if the stack is empty.
        /// </summary>
        /// <param name="prototype">A prototype which will be cloned for each request</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidProgramException">If a controller with that name have been added already.</exception>
        public void Add(RequestController prototype)
        {
            Check.Require(prototype, "prototype");

            lock (_controllers)
            {
                if (_controllers.ContainsKey(prototype.ControllerName))
                    throw new InvalidOperationException("Controller with name '" + prototype.ControllerName + "' already exists.");

                _controllers.Add(prototype.ControllerName, new ControllerContext(prototype));
            }
        }
コード例 #5
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ControllerContext"/> class.
            /// </summary>
            /// <param name="prototype">A controller used to handle certain URLs. Will be cloned for each parallel request.</param>
            public ControllerContext(RequestController prototype)
            {
                Check.Require(prototype, "prototype");

                _prototype = prototype;
            }
コード例 #6
0
// ReSharper disable SuggestBaseTypeForParameter
        private string Invoke(RequestController controller, string httpMetod, string uri, HttpForm form, out IHttpResponse response, IHttpSession session)
// ReSharper restore SuggestBaseTypeForParameter
        {
            HttpRequest request = new HttpRequest
                                  	{
                                  		HttpVersion = "HTTP/1.1",
                                  		UriPath = uri,
                                  		Method = httpMetod,
                                  		Uri = new Uri(HostName + uri)
                                  	};
        	request.AssignForm(form);

        	response = request.CreateResponse(TestContext);
            if(!controller.Process(request, response, session))
                throw new NotFoundException("404 could not find processor for: " + uri);
           
            response.Body.Seek(0, SeekOrigin.Begin);
            StreamReader reader = new StreamReader(response.Body);
            return reader.ReadToEnd();
        }
コード例 #7
0
 private string Invoke(RequestController controller, string httpMetod, string uri, out IHttpResponse response, IHttpSession session)
 {
     return Invoke(controller, httpMetod, uri, null, out response, session);
 }
コード例 #8
0
 /// <summary>
 /// Send a POST request to a controller.
 /// </summary>
 /// <param name="controller">Controller receiving the post request.</param>
 /// <param name="uri">Uri visited.</param>
 /// <param name="form">Form being processed by controller.</param>
 /// <param name="response">Response from the controller.</param>
 /// <param name="session">Session used during the test. null = <see cref="DefaultSession"/> is used.</param>
 /// <returns>body posted by the response object</returns>
 /// <example>
 /// <code>
 /// void MyTest()
 /// {
 ///     // Create a controller.
 ///     MyController controller = new MyController();
 ///
 ///     // build up a form that is used by the controller.
 ///     HttpForm form = new HttpForm();
 ///     form.Add("user[firstName]", "Jonas");
 /// 
 ///     // Invoke the request
 ///     ControllerTester tester = new ControllerTester();
 ///     IHttpResponse response;
 ///     string text = tester.Get(controller, "/user/create/", form, out response, null);
 /// 
 ///     // validate response back from controller.
 ///     Assert.Equal("User 'Jonas' has been created.", text);
 /// }
 /// </code>
 /// </example>
 public string Post(RequestController controller, string uri, HttpForm form, out IHttpResponse response, IHttpSession session)
 {
     return Invoke(controller, Method.Post, uri, form, out response, session);
 }
コード例 #9
0
 /// <summary>
 /// Send a GET request to a controller.
 /// </summary>
 /// <param name="controller">Controller receiving the post request.</param>
 /// <param name="uri">Uri visited.</param>
 /// <param name="response">Response from the controller.</param>
 /// <param name="session">Session used during the test. null = <see cref="DefaultSession"/> is used.</param>
 /// <returns>body posted by the response object</returns>
 /// <example>
 /// <code>
 /// void MyTest()
 /// {
 ///     ControllerTester tester = new ControllerTester();
 ///     
 ///     MyController controller = new MyController();
 ///     IHttpResponse response;
 ///     string text = Get(controller, "/my/hello/1?hello=world", out response, null);
 ///     Assert.Equal("world|1", text);
 /// }
 /// </code>
 /// </example>
 public string Get(RequestController controller, string uri, out IHttpResponse response, IHttpSession session)
 {
     return Invoke(controller, Method.Get, uri, out response, session);
 }
コード例 #10
0
 private string Invoke(RequestController controller, string httpMetod, string uri, out IHttpResponse response, IHttpSession session)
 {
     return(Invoke(controller, httpMetod, uri, null, out response, session));
 }
コード例 #11
0
 /// <summary>
 /// Send a POST request to a controller.
 /// </summary>
 /// <param name="controller">Controller receiving the post request.</param>
 /// <param name="uri">Uri visited.</param>
 /// <param name="form">Form being processed by controller.</param>
 /// <param name="response">Response from the controller.</param>
 /// <param name="session">Session used during the test. null = <see cref="DefaultSession"/> is used.</param>
 /// <returns>body posted by the response object</returns>
 /// <example>
 /// <code>
 /// void MyTest()
 /// {
 ///     // Create a controller.
 ///     MyController controller = new MyController();
 ///
 ///     // build up a form that is used by the controller.
 ///     HttpForm form = new HttpForm();
 ///     form.Add("user[firstName]", "Jonas");
 ///
 ///     // Invoke the request
 ///     ControllerTester tester = new ControllerTester();
 ///     IHttpResponse response;
 ///     string text = tester.Get(controller, "/user/create/", form, out response, null);
 ///
 ///     // validate response back from controller.
 ///     Assert.Equal("User 'Jonas' has been created.", text);
 /// }
 /// </code>
 /// </example>
 public string Post(RequestController controller, string uri, HttpForm form, out IHttpResponse response, IHttpSession session)
 {
     return(Invoke(controller, Method.Post, uri, form, out response, session));
 }
コード例 #12
0
 /// <summary>
 /// Send a GET request to a controller.
 /// </summary>
 /// <param name="controller">Controller receiving the post request.</param>
 /// <param name="uri">Uri visited.</param>
 /// <param name="response">Response from the controller.</param>
 /// <param name="session">Session used during the test. null = <see cref="DefaultSession"/> is used.</param>
 /// <returns>body posted by the response object</returns>
 /// <example>
 /// <code>
 /// void MyTest()
 /// {
 ///     ControllerTester tester = new ControllerTester();
 ///
 ///     MyController controller = new MyController();
 ///     IHttpResponse response;
 ///     string text = Get(controller, "/my/hello/1?hello=world", out response, null);
 ///     Assert.Equal("world|1", text);
 /// }
 /// </code>
 /// </example>
 public string Get(RequestController controller, string uri, out IHttpResponse response, IHttpSession session)
 {
     return(Invoke(controller, Method.Get, uri, out response, session));
 }