A class implementing the T:Stumps.Http.IHttpHandler interface that provides a fallback response to an incomming HTTP request.
Inheritance: IHttpHandler
示例#1
0
        public void ProcessRequest_WithNullContext_ThrowsException()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http404NotFound);

            Assert.That(
                async() => await handler.ProcessRequest(null),
                Throws.Exception.TypeOf <ArgumentNullException>().With.Property("ParamName").EqualTo("context"));
        }
        public void ProcessRequest_WithNullContext_ThrowsException()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http404NotFound);

            Assert.That(
                async () => await handler.ProcessRequest(null),
                Throws.Exception.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("context"));
        }
示例#3
0
        public async Task ProcessRequest_With503Fallback_Returns503()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http503ServiceUnavailable);
            var context = new MockHttpContext();
            await handler.ProcessRequest(context);

            Assert.AreEqual(503, context.Response.StatusCode);
            Assert.AreEqual(HttpStatusCodes.GetStatusDescription(503), context.Response.StatusDescription);
        }
示例#4
0
        public async Task ProcessRequest_With404Fallback_Returns404()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http404NotFound);
            var context = new MockHttpContext();
            await handler.ProcessRequest(context);

            Assert.AreEqual(404, context.Response.StatusCode);
            Assert.AreEqual(HttpStatusCodes.GetStatusDescription(404), context.Response.StatusDescription);
        }
        public void ProcessRequest_With503Fallback_Returns503()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http503ServiceUnavailable);
            var context = new MockHttpContext();
            handler.ProcessRequest(context);

            Assert.AreEqual(503, context.Response.StatusCode);
            Assert.AreEqual(HttpStatusCodes.GetStatusDescription(503), context.Response.StatusDescription);
        }
        public void ProcessRequest_With404Fallback_Returns404()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http404NotFound);
            var context = new MockHttpContext();
            handler.ProcessRequest(context);

            Assert.AreEqual(404, context.Response.StatusCode);
            Assert.AreEqual(HttpStatusCodes.GetStatusDescription(404), context.Response.StatusDescription);
        }
示例#7
0
        public async Task ProcessRequest_WithStumpsHttpResponse_PopulatesOrigin()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http503ServiceUnavailable);

            var response = new StumpsHttpResponse();
            var context  = new MockHttpContext(null, response);

            await handler.ProcessRequest(context);

            Assert.AreEqual(response.Origin, HttpResponseOrigin.ServiceUnavailable);
        }
        public void ProcessRequest_WithPopulatedResponse_ClearsHeadersAndBody()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http503ServiceUnavailable);
            var context = new MockHttpContext();
            context.Response.AppendToBody(new byte[100]);
            context.Response.Headers["ABCD"] = "123";
            handler.ProcessRequest(context);

            Assert.AreEqual(0, context.Response.Headers.Count);
            Assert.AreEqual(0, context.Response.BodyLength);
        }
示例#9
0
        public async Task ProcessRequest_WithPopulatedResponse_ClearsHeadersAndBody()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http503ServiceUnavailable);
            var context = new MockHttpContext();

            context.Response.AppendToBody(new byte[100]);
            context.Response.Headers["ABCD"] = "123";
            await handler.ProcessRequest(context);

            Assert.AreEqual(0, context.Response.Headers.Count);
            Assert.AreEqual(0, context.Response.BodyLength);
        }
        public void ProcessRequest_WithStumpsHttpResponse_PopulatesOrigin()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http503ServiceUnavailable);

            var hitCount = 0;
            handler.ContextProcessed += (o, e) => hitCount++;

            var response = new StumpsHttpResponse();
            var context = new MockHttpContext(null, response);

            handler.ProcessRequest(context);
            Assert.AreEqual(response.Origin, HttpResponseOrigin.ServiceUnavailable);
        }
示例#11
0
        public async Task ProcessRequest_WithValidContext_RaisesContextProcessedEvent()
        {
            var resetEvent = new AutoResetEvent(false);

            var handler = new FallbackResponseHandler(FallbackResponse.Http503ServiceUnavailable);

            handler.ContextProcessed += (o, e) => resetEvent.Set();

            var context = new MockHttpContext();

            await handler.ProcessRequest(context);

            var eventRaised = resetEvent.WaitOne(1000);

            Assert.IsTrue(eventRaised);
        }
示例#12
0
        /// <summary>
        ///     Starts this instance of the Stumps server.
        /// </summary>
        public void Start()
        {
            lock (_syncRoot)
            {

                if (_started)
                {
                    return;
                }

                _started = true;

                // Setup the pipeline HTTP handler
                var pipeline = new HttpPipelineHandler();

                // Setup the Stump HTTP handler
                _stumpsHandler = new StumpsHandler(_stumpsManager);
                _stumpsHandler.Enabled = this.StumpsEnabled;

                pipeline.Add(_stumpsHandler);

                // Setup the Proxy HTTP handler
                if (_remoteHttpServer != null)
                {
                    var proxyHandler = new ProxyHandler(_remoteHttpServer);
                    pipeline.Add(proxyHandler);
                }
                else
                {
                    // Setup the Service Unavailable HTTP handler
                    var stumpNotFoundHandler = new FallbackResponseHandler(_defaultResponse);
                    pipeline.Add(stumpNotFoundHandler);
                }

                var scheme = _useHttpsForIncommingConnections ? ServerScheme.Https : ServerScheme.Http;
                _server = new HttpServer(scheme, _port, pipeline);

                _server.RequestFinished += ServerRequestFinished;
                _server.RequestProcessed += ServerRequestProcessed;
                _server.RequestReceived += ServerRequestStarted;

                _server.StartListening();

            }
        }
        public void ProcessRequest_WithValidContext_RaisesContextProcessedEvent()
        {
            var handler = new FallbackResponseHandler(FallbackResponse.Http503ServiceUnavailable);

            var hitCount = 0;
            handler.ContextProcessed += (o, e) => hitCount++;

            var context = new MockHttpContext();

            handler.ProcessRequest(context);
            Assert.AreEqual(1, hitCount);
        }