예제 #1
0
        public static void Main()
        {
            string response;

            Debug.Print("* -------------------------------");
            Debug.Print("* IntegrationTest");
            Debug.Print("* -------------------------------");
            Debug.Print("");

            string getUrl = "mattbarton.org/netmf_utilities_http_get_test.php";
            HttpRequest getRequest = new HttpRequestBuilder()
                .SetUrl(getUrl)
                .Build();

            getRequest.AddArgument("arg1", "value1");
            getRequest.AddArgument("color", "yellow");

            Debug.Print("Getting " + getUrl);
            Debug.Print("");
            try
            {
                response = new HttpClient().Send(getRequest);
            }
            catch (Exception ex)
            {
                response = ex.Message;
            }
            Debug.Print(response);
            Debug.Print("");


            string postUrl = "mattbarton.org/netmf_utilities_http_post_test.php";
            HttpRequest postRequest = new HttpRequestBuilder()
                .SetUrl(postUrl)
                .SetMethod(HttpMethod.POST)
                .Build();

            postRequest.AddArgument("arg1", "value1");
            postRequest.AddArgument("color", "yellow");

            Debug.Print("Posting to " + postUrl);
            Debug.Print("");
            try
            {
                response = new HttpClient().Send(postRequest);
            }
            catch (Exception ex)
            {
                response = ex.Message;
            }
            Debug.Print(response);
            Debug.Print("");
            Debug.Print("* -------------------------------");
            Debug.Print("* End");
            Debug.Print("* -------------------------------");
        }
예제 #2
0
        public void Given_a_SocketException_is_thrown_When_requesting_Then_an_HttpSocketConnectionException_is_thrown()
        {
            // setup
            var target = new HttpSocketBuilder()
                .GivenAnHttpRequest()
                .GivenASocketExceptionIsThrown()
                .Build();

            var request = new HttpRequestBuilder()
                .SetUrl("")
                .Build();

            TestDelegate method = () => target.Request(request);

            // execution/assertion
            var exception = Assert.Throws(typeof(HttpSocketConnectionException), method);
            Assert.AreEqual("The socket threw a SocketException",
                exception.Message, "The exception message was not correct");
        }
예제 #3
0
        public void Given_a_host_is_specified_And_the_host_cannot_be_found_When_requesting_Then_an_HttpUnknownHostException_is_thrown()
        {
            // setup
            var host = "mock host";
            var request = new HttpRequestBuilder()
                .SetUrl(host)
                .Build();

            var target = new HttpSocketBuilder()
                .GivenAnHttpRequest()
                .GivenAHostIsSpecified(host)
                .GivenTheHostCannotBeFound()
                .Build();

            TestDelegate method = () => target.Request(request);

            // execution/assertion
            var exception = Assert.Throws(typeof(HttpUnknownHostException), method);
            Assert.AreEqual("The host mock host could not be found.", exception.Message,
                "The exception message was not correct");
        }
예제 #4
0
        public void Given_a_host_and_port_are_specified_When_requesting_Then_a_socket_is_connected_to_the_host()
        {
            // setup
            var host = "www.test.com";
            var port = 8080;
            var request = new HttpRequestBuilder()
                .SetUrl(host)
                .SetPort(port)
                .Build();

            var builder = new HttpSocketBuilder()
                .GivenAnHttpRequest();

            var target = builder.Build();

            // execution
            target.Request(request);

            // assertion
            builder.ThenSocketConnectedToHostOnPort(host, port);
        }
예제 #5
0
        public void Given_url_and_port_and_arguments_are_set_When_sending_Then_request_is_sent_via_socket()
        {
            // setup
            var mockHostname = "www.test.com";
            var mockPath = "/resource.html";
            var mockUrl = mockHostname + mockPath;
            var mockPort = 8080;
            var mockSocket = new Mock<IHttpSocket>();

            var mockRequest = new HttpRequestBuilder()
                .SetUrl(mockUrl)
                .SetPort(mockPort)
                .Build();

            var mockResponse = new HttpResponseBuilder()
                .SetStatusCode(200)
                .SetResponse("response")
                .Build();

            mockSocket
                .Setup(s => s.Request(It.IsAny<HttpRequest>()))
                .Returns(mockResponse);

            var target = new HttpClient(mockSocket.Object);

            // execution
            target.Send(mockRequest);

            // assertion
            mockSocket.Verify(
                s => s.Request(It.Is<HttpRequest>(
                    hr => hr.Hostname == mockHostname &&
                        hr.Path == mockPath &&
                        hr.Port == mockPort &&
                        hr.Method == HttpMethod.GET)),
                Times.Once(),
                "Request not sent via socket");
        }
예제 #6
0
        public void Given_an_HttpRequest_When_requesting_Then_HttpRequest_byte_array_is_sent_to_socket()
        {
            // setup
            var request = new HttpRequestBuilder()
                .SetUrl("www.test.com/test.html")
                .Build();

            var builder = new HttpSocketBuilder();
            var target = builder
                .GivenAnHttpRequest()
                .Build();

            // execution
            target.Request(request);

            // assertion
            builder.ThenHttpRequestByteArrayIsSentToSocket(request.ToByteArray());
        }