Beispiel #1
0
        /// <summary>
        /// Sends a POST request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <exception cref="System.ArgumentNullException">request</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="request" /> parameter is null.</exception>
        public void SendPostRequest(UrlServiceCallRequest request)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            SendPostRequestInternal(request.Url, request.GetQueryString());
        }
        public void SendGetRequestTest()
        {
            // Arrange.
            const string Url = "http://example.com/TestService";
            const string QueryString = "param1=Value1&param2=Value2";
            const string ParamName1 = "Param1";
            const string ParamValue1 = "Test Value";
            const string ParamName2 = "Param2";
            const string ParamValue2 = "!@#$%^&*()_[]{};:'\",<.>/?";

            var requestSender = new HttpRequestSender();
            var request = new UrlServiceCallRequest { Url = Url };
            request.ParameterValues.Add(new UrlServiceCallParameterValue { Name = ParamName1, Value = ParamValue1 });
            request.ParameterValues.Add(new UrlServiceCallParameterValue { Name = ParamName2, Value = ParamValue2 });

            var webRequest1 = Mock.Create<HttpWebRequest>();
            var webRequest2 = Mock.Create<HttpWebRequest>();
            Mock.Arrange(() => WebRequest.Create(Arg.IsAny<Uri>())).Returns(
                (Uri uri) =>
                    {
                        if (uri.OriginalString
                            == "http://example.com:80/TestService?Param1=Test+Value&Param2=!%40%23%24%25%5e%26*()_%5b%5d%7b%7d%3b%3a%27%22%2c%3c.%3e%2f%3f")
                            return webRequest1;

                        if (uri.OriginalString == "http://example.com:80/TestService?param1=Value1&param2=Value2")
                            return webRequest2;

                        return null;
                    });

            var webResponse1 = Mock.Create<HttpWebResponse>();
            Mock.Arrange(webRequest1, r => r.GetResponse()).Returns(webResponse1);

            var webResponse2 = Mock.Create<HttpWebResponse>();
            Mock.Arrange(webRequest2, r => r.GetResponse()).Returns(webResponse2);

            // Act.
            requestSender.SendGetRequest(request);
            requestSender.SendGetRequest(Url, QueryString);

            // Assert.
            Assert.AreEqual("GET", webRequest1.Method);
            Assert.AreEqual(HttpVersion.Version11, webRequest1.ProtocolVersion);
            Assert.IsTrue(webRequest1.UseDefaultCredentials);
            Assert.AreEqual("application/x-www-form-urlencoded", webRequest1.ContentType);
            Mock.Assert(() => webResponse1.Dispose(), Occurs.Once());

            Assert.AreEqual("GET", webRequest2.Method);
            Assert.AreEqual(HttpVersion.Version11, webRequest2.ProtocolVersion);
            Assert.IsTrue(webRequest2.UseDefaultCredentials);
            Assert.AreEqual("application/x-www-form-urlencoded", webRequest2.ContentType);
            Mock.Assert(() => webResponse2.Dispose(), Occurs.Once());

            // Excetions.
            TestsHelper.VerifyThrow<ArgumentNullException>(() => requestSender.SendGetRequest(null));
        }
        public void SendPostRequestTest()
        {
            // Arrange.
            const string Url1 = "http://example.com/TestService1";
            const string Url2 = "http://example.com/TestService2";
            const string Content = "param1=Value1&param2=Value2";
            const string ParamName1 = "Param1";
            const string ParamValue1 = "Test Value";
            const string ParamName2 = "Param2";
            const string ParamValue2 = "!@#$%^&*()_[]{};:'\",<.>/?";

            var requestSender = new HttpRequestSender();
            var request = new UrlServiceCallRequest { Url = Url1 };
            request.ParameterValues.Add(new UrlServiceCallParameterValue { Name = ParamName1, Value = ParamValue1 });
            request.ParameterValues.Add(new UrlServiceCallParameterValue { Name = ParamName2, Value = ParamValue2 });

            var webRequest1 = Mock.Create<HttpWebRequest>();
            var webRequest2 = Mock.Create<HttpWebRequest>();
            Mock.Arrange(() => WebRequest.Create(Arg.IsAny<Uri>())).Returns(
                (Uri uri) =>
                    {
                        if (uri.ToString() == Url1)
                            return webRequest1;

                        if (uri.ToString() == Url2)
                            return webRequest2;

                        return null;
                    });

            var stream1 = new MemoryStream();
            Mock.Arrange(webRequest1, r => r.GetRequestStream()).Returns(stream1);

            var stream2 = new MemoryStream();
            Mock.Arrange(webRequest2, r => r.GetRequestStream()).Returns(stream2);

            var webResponse1 = Mock.Create<HttpWebResponse>();
            Mock.Arrange(webRequest1, r => r.GetResponse()).Returns(webResponse1);

            var webResponse2 = Mock.Create<HttpWebResponse>();
            Mock.Arrange(webRequest2, r => r.GetResponse()).Returns(webResponse2);

            // Act.
            requestSender.SendPostRequest(request);
            requestSender.SendPostRequest(Url2, Content);

            var bytes1 = stream1.ToArray();
            var bytes2 = stream2.ToArray();

            // Assert.
            Assert.AreEqual("POST", webRequest1.Method);
            Assert.AreEqual(HttpVersion.Version11, webRequest1.ProtocolVersion);
            Assert.IsTrue(webRequest1.UseDefaultCredentials);
            Assert.AreEqual("application/x-www-form-urlencoded", webRequest1.ContentType);
            Assert.AreEqual(bytes1.Length, webRequest1.ContentLength);
            Mock.Assert(() => webResponse1.Dispose(), Occurs.Once());
            Assert.AreEqual(
                "Param1=Test+Value&Param2=!%40%23%24%25%5e%26*()_%5b%5d%7b%7d%3b%3a%27%22%2c%3c.%3e%2f%3f", Encoding.UTF8.GetString(bytes1));

            Assert.AreEqual("POST", webRequest2.Method);
            Assert.AreEqual(HttpVersion.Version11, webRequest2.ProtocolVersion);
            Assert.IsTrue(webRequest2.UseDefaultCredentials);
            Assert.AreEqual("application/x-www-form-urlencoded", webRequest2.ContentType);
            Assert.AreEqual(bytes2.Length, webRequest2.ContentLength);
            Mock.Assert(() => webResponse2.Dispose(), Occurs.Once());
            Assert.AreEqual("param1=Value1&param2=Value2", Encoding.UTF8.GetString(bytes2));

            // Exceptions.
            TestsHelper.VerifyThrow<ArgumentNullException>(() => requestSender.SendPostRequest(null));
        }
Beispiel #4
0
        private UrlServiceCallRequest GetRequestInternal(IEditableRoot item)
        {
            var request = new UrlServiceCallRequest { Url = _addressExpression != null ? _addressExpression(item) : null };

            request.ParameterValues.AddRange(
                _parameterMappings.Select(
                    parameterMapping => new UrlServiceCallParameterValue { Name = parameterMapping.Name, Value = parameterMapping.GetValue(item) ?? string.Empty }));

            return request;
        }