public void StumpsHttpRequest_PopulatesCorrectly()
        {
            var mockHandler = new MockHandler();
            mockHandler.StatusCode = 202;
            mockHandler.StatusDescription = "Bob";
            mockHandler.UpdateBody(TestData.SampleTextResponse);
            mockHandler.ContentType = "bobs.type";

            var startingEventCount = 0;

            using (var server = HttpHelper.CreateServer(mockHandler))
            {

                server.RequestReceived += (o, i) =>
                {
                    var request = i.Context.Request;

                    startingEventCount++;
                    Assert.Greater(request.Headers.Count, 0);
                    Assert.AreEqual("POST", request.HttpMethod);
                    Assert.AreEqual(server.Port, request.LocalEndPoint.Port);
                    Assert.AreEqual("1.1", request.ProtocolVersion);
                    Assert.AreEqual("/", request.RawUrl);
                    Assert.AreEqual(3, request.BodyLength);
                    CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, request.GetBody());

                };

                server.StartListening();

                var uri = new Uri("http://localhost:" + server.Port.ToString(CultureInfo.InvariantCulture) + "/");

                var webRequest = WebRequest.Create(uri) as System.Net.HttpWebRequest;
                webRequest.ContentType = "Bobs.Content";
                webRequest.Referer = "http://stumps-project.com/";
                webRequest.UserAgent = "StumpsTestAgent";
                webRequest.Method = "POST";
                webRequest.ContentLength = 3;
                var stream = webRequest.GetRequestStream();
                stream.Write(new byte[] { 1, 2, 3 }, 0, 3);

                stream.Close();
                using (var response = (HttpWebResponse)webRequest.GetResponse())
                {
                    Assert.IsNotNull(response);
                }

            }

            Assert.AreEqual(startingEventCount, 1);
        }
Example #2
0
        public void ProcessAsyncRequest_WithValidRequest_RaisesServerEvents()
        {
            var mockHandler = new MockHandler();

            mockHandler.StatusCode        = 202;
            mockHandler.StatusDescription = "Bob";
            mockHandler.UpdateBody(TestData.SampleTextResponse);

            var requestReceivedEvent = new AutoResetEvent(false);
            var requestFinishedEvent = new AutoResetEvent(false);

            using (var server = HttpHelper.CreateServer(mockHandler))
            {
                server.RequestReceived += (o, i) =>
                {
                    Assert.IsNotNull(o);
                    Assert.IsNotNull(i);
                    Assert.IsNotNull(i.Context);
                    Assert.IsNotNull(i.Context.Request);
                    Assert.IsNotNull(i.Context.Response);
                    requestReceivedEvent.Set();
                };

                server.RequestFinished += (o, i) =>
                {
                    Assert.IsNotNull(o);
                    Assert.IsNotNull(i);
                    Assert.IsNotNull(i.Context);
                    Assert.IsNotNull(i.Context.Request);
                    Assert.IsNotNull(i.Context.Response);
                    requestFinishedEvent.Set();
                };

                server.StartListening();

                var uri = new Uri("http://localhost:" + server.Port.ToString(CultureInfo.InvariantCulture) + "/");

                var request = WebRequest.Create(uri);

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    Assert.IsNotNull(response);
                }
            }

            var requestReceived = requestReceivedEvent.WaitOne(1000);
            var requestFinished = requestFinishedEvent.WaitOne(1000);

            Assert.IsTrue(requestReceived, "Request received.");
            Assert.IsTrue(requestFinished, "Request finished.");
        }
Example #3
0
        public void ProcessAsyncRequest_WithValidRequest_RaisesServerEvents()
        {
            var mockHandler = new MockHandler();
            mockHandler.StatusCode = 202;
            mockHandler.StatusDescription = "Bob";
            mockHandler.UpdateBody(TestData.SampleTextResponse);

            var startingEventCount = 0;
            var finishingEventCount = 0;

            using (var server = HttpHelper.CreateServer(mockHandler))
            {

                server.RequestReceived += (o, i) =>
                {
                    startingEventCount++;
                    Assert.IsNotNull(o);
                    Assert.IsNotNull(i);
                    Assert.IsNotNull(i.Context);
                    Assert.IsNotNull(i.Context.Request);
                    Assert.IsNotNull(i.Context.Response);
                };

                server.RequestFinished += (o, i) =>
                {
                    finishingEventCount++;
                    Assert.IsNotNull(o);
                    Assert.IsNotNull(i);
                    Assert.IsNotNull(i.Context);
                    Assert.IsNotNull(i.Context.Request);
                    Assert.IsNotNull(i.Context.Response);
                };

                server.StartListening();

                var uri = new Uri("http://localhost:" + server.Port.ToString(CultureInfo.InvariantCulture) + "/");

                var request = WebRequest.Create(uri);

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    Assert.IsNotNull(response);
                }

            }

            Assert.AreEqual(startingEventCount, 1);
            Assert.AreEqual(finishingEventCount, 1);
        }
        public void StumpsHttpResponse_PopulatesCorrectly()
        {
            var mockHandler = new MockHandler();
            mockHandler.StatusCode = 202;
            mockHandler.StatusDescription = "Bob";
            mockHandler.UpdateBody(TestData.SampleTextResponse);
            mockHandler.AddHeader("X-Stumps", "V1");

            var finishingEventCount = 0;

            using (var server = HttpHelper.CreateServer(mockHandler))
            {

                server.RequestFinished += (o, i) =>
                {
                    var response = i.Context.Response;

                    finishingEventCount++;
                    Assert.IsNotNull(response.Headers);
                    Assert.Greater(response.Headers.Count, 0);
                    Assert.AreEqual(response.Headers["X-Stumps"], "V1");

                };

                server.StartListening();

                var uri = new Uri("http://localhost:" + server.Port.ToString(CultureInfo.InvariantCulture) + "/");

                var request = WebRequest.Create(uri);

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    Assert.IsNotNull(response);
                }

            }

            Assert.AreEqual(finishingEventCount, 1);
        }
        public void StumpsHttpResponse_PopulatesCorrectly()
        {
            var mockHandler = new MockHandler();

            mockHandler.StatusCode        = 202;
            mockHandler.StatusDescription = "Bob";
            mockHandler.UpdateBody(TestData.SampleTextResponse);
            mockHandler.AddHeader("X-Stumps", "V1");

            var requestFinishedEvent = new AutoResetEvent(false);

            using (var server = HttpHelper.CreateServer(mockHandler))
            {
                server.RequestFinished += (o, i) =>
                {
                    var response = i.Context.Response;

                    Assert.IsNotNull(response.Headers);
                    Assert.Greater(response.Headers.Count, 0);
                    Assert.AreEqual(response.Headers["X-Stumps"], "V1");
                    requestFinishedEvent.Set();
                };

                server.StartListening();

                var uri = new Uri("http://localhost:" + server.Port.ToString(CultureInfo.InvariantCulture) + "/");

                var request = WebRequest.Create(uri);

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    Assert.IsNotNull(response);
                }
            }

            var requestFinished = requestFinishedEvent.WaitOne(1000);

            Assert.IsTrue(requestFinished);
        }
Example #6
0
        public void ProcessAsyncRequest_WithValidRequest_ReturnsResponseFromHandler()
        {
            var mockHandler = new MockHandler();
            mockHandler.StatusCode = 202;
            mockHandler.StatusDescription = "Bob";
            mockHandler.UpdateBody(TestData.SampleTextResponse);

            using (var server = HttpHelper.CreateServer(mockHandler))
            {

                server.StartListening();

                var uri = new Uri("http://localhost:" + server.Port.ToString(CultureInfo.InvariantCulture) + "/");

                var request = WebRequest.Create(uri);

                using (var response = (HttpWebResponse)request.GetResponse())
                {

                    Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode);
                    Assert.AreEqual(mockHandler.StatusDescription, response.StatusDescription);

                    string body;

                    using (var sr = new StreamReader(response.GetResponseStream()))
                    {
                        body = sr.ReadToEnd();
                    }

                    Assert.AreEqual(TestData.SampleTextResponse, body);

                }

            }
        }