public void CurrentAbsoluteUrl_ProtocolDefined_OverridesTheOriginalProtocol()
        {
            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("SiteRoot", "");

            var httpContextDefinition = new TestHttpContextDefinition
            {
                Protocol = Protocol.Http
            };

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(MvcTestHelper.GetHttpContext(httpContextDefinition), new RouteData(), routeCollection);
            // ReSharper disable PossibleNullReferenceException
            Assert.That(urlHelper.RequestContext.HttpContext.Request.Url.Scheme, Is.EqualTo("http"));
            // ReSharper restore PossibleNullReferenceException

            Assert.That(urlHelper.CurrentAbsoluteUrl(Protocol.Https), Is.EqualTo("https://localhost/"));

            httpContextDefinition = new TestHttpContextDefinition
            {
                Protocol = Protocol.Https
            };

            urlHelper = MvcTestHelper.GetUrlHelper(MvcTestHelper.GetHttpContext(httpContextDefinition), new RouteData(), routeCollection);
            // ReSharper disable PossibleNullReferenceException
            Assert.That(urlHelper.RequestContext.HttpContext.Request.Url.Scheme, Is.EqualTo("https"));
            // ReSharper restore PossibleNullReferenceException

            Assert.That(urlHelper.CurrentAbsoluteUrl(Protocol.Http), Is.EqualTo("http://localhost/"));
        }
Beispiel #2
0
        internal static HttpContextBase GetHttpContext(TestHttpContextDefinition testHttpContextDefinition)
        {
            Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();

            if (testHttpContextDefinition.HasApplicationPath)
            {
                mockHttpContext.Setup(o => o.Request.ApplicationPath).Returns(testHttpContextDefinition.ApplicationPath);
                mockHttpContext.Setup(o => o.Request.RawUrl).Returns(testHttpContextDefinition.ApplicationPath);
            }

            if (testHttpContextDefinition.HasRequestPath)
            {
                mockHttpContext.Setup(o => o.Request.AppRelativeCurrentExecutionFilePath).Returns(testHttpContextDefinition.RequestPath);
            }

            mockHttpContext.Setup(o => o.Request.Url).Returns(testHttpContextDefinition.RequestUrl);

            mockHttpContext.Setup(o => o.Request.PathInfo).Returns(string.Empty);

            if (testHttpContextDefinition.HttpMethod.HasValue)
            {
                mockHttpContext.Setup(o => o.Request.HttpMethod).Returns(testHttpContextDefinition.HttpMethod.Value.ToString().ToUpperInvariant());
            }

            mockHttpContext.Setup(o => o.Request.QueryString).Returns(testHttpContextDefinition.QueryString);

            mockHttpContext.Setup(o => o.Session).Returns((HttpSessionStateBase)null);
            mockHttpContext.Setup(o => o.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(r => r);
            mockHttpContext.Setup(o => o.Items).Returns(new Hashtable());

            return mockHttpContext.Object;
        }
Beispiel #3
0
        public void GetProtocolFromHttpRequest_Http_ReturnsHttp()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                Protocol = Protocol.Http
            };

            Assert.That(ProtocolHelper.GetProtocolFromHttpRequest(MvcTestHelper.GetHttpContext(httpContextDefinition).Request), Is.EqualTo(Protocol.Http));
        }
Beispiel #4
0
        public void GetProtocolFromHttpRequest_HttpsCaseSensitive_ReturnsHttps()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                ArbitraryProtocol = "HtTpS"
            };

            Assert.That(ProtocolHelper.GetProtocolFromHttpRequest(MvcTestHelper.GetHttpContext(httpContextDefinition).Request), Is.EqualTo(Protocol.Https));
        }
Beispiel #5
0
        public void GetProtocolFromHttpRequest_RequestUrlIsNull_ThrowsException()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                SetRequestUrlToNull = true
            };

            var exception = Assert.Throws<InvalidOperationException>(() => ProtocolHelper.GetProtocolFromHttpRequest(MvcTestHelper.GetHttpContext(httpContextDefinition).Request));
            Assert.That(exception.Message.Contains("The HTTP request has no URL defined."));
            Console.WriteLine(exception.Message);
        }
Beispiel #6
0
        public void GetProtocolFromHttpRequest_ArbitraryProtocol_ThrowsException()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                ArbitraryProtocol = "ftp"
            };

            var exception = Assert.Throws<InvalidOperationException>(() => ProtocolHelper.GetProtocolFromHttpRequest(MvcTestHelper.GetHttpContext(httpContextDefinition).Request));
            Assert.That(exception.Message.Contains("The HTTP request has a URL scheme that do not correspond to any of the web protocols"));
            Console.WriteLine(exception.Message);
        }
        public void CurrentUrlAndCurrentAbsoluteUrl__RouteWithoutParameters_NewQueryStringParameterSetToUrlParameterOptional__ParameterWithoutValueAddedToQueryString()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection()
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", string.Empty);

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, new RouteData(), routeCollection);

            Assert.That(urlHelper.CurrentUrl(newParameter => UrlParameter.Optional), Is.EqualTo("/"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(newParameter => UrlParameter.Optional), Is.EqualTo("http://localhost/"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl__RouteWithParameters_NewQueryStringParameterSetToUrlParameterOptional__ParameterWithoutValueAddedToQueryString()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection()
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithParameters", "{language}/{year}");

            RouteData routeData = new RouteData();
            routeData.Values.Add("language", "en-US");
            routeData.Values.Add("year", "1999");

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, routeData, routeCollection);

            Assert.That(urlHelper.CurrentUrl(newParameter => UrlParameter.Optional), Is.EqualTo("/en-US/1999"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(newParameter => UrlParameter.Optional), Is.EqualTo("http://localhost/en-US/1999"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl_QueryStringWithoutDuplicateParameterEntries_ExistingQueryStringIsPreserved()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection { { "p1", "1" }, { "p2", "2.0" }, { "p3", "text" } },
                RequestPath = "/?p1=1&p2=2.0&p3=text"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", string.Empty);

            RouteData routeData = new RouteData();

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, routeData, routeCollection);

            Assert.That(urlHelper.CurrentUrl(), Is.EqualTo("/?p1=1&p2=2.0&p3=text"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(), Is.EqualTo("http://localhost/?p1=1&p2=2.0&p3=text"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl__RouteWithParameters_ParameterWithSameNameInQueryString__RouteParameterRemainTheSame()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection {{"language", "hr-HR"}},
                RequestPath = "/en-US/1999?language=hr-HR"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithParameters", "{language}/{year}");

            RouteData routeData = new RouteData();
            routeData.Values.Add("language", "en-US");
            routeData.Values.Add("year", "1999");

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, routeData, routeCollection);

            Assert.That(urlHelper.CurrentUrl(), Is.EqualTo("/en-US/1999?language=hr-HR"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(), Is.EqualTo("http://localhost/en-US/1999?language=hr-HR"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl_EnumerableParameterDoesNotExistInQueryString_ParameterWithSameNameExistsInRouteParameters_EnumerableParameterAddedToQueryString()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection(),
                RequestPath = "/routeEnumerableParameter"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithParameters", "{enumerableParameter}");

            RouteData routeData = new RouteData();
            routeData.Values.Add("enumerableParameter", "routeEnumerableParameter");

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, routeData, routeCollection);

            Assert.That(urlHelper.CurrentUrl(enumerableParameter => new[] { 1, 2 }), Is.EqualTo("/routeEnumerableParameter?enumerableParameter=1&enumerableParameter=2"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(enumerableParameter => new[] { 1, 2 }), Is.EqualTo("http://localhost/routeEnumerableParameter?enumerableParameter=1&enumerableParameter=2"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl_QueryStringParametersAreCaseInsensitive()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection { { "parameter", "value" } },
                RequestPath = "/en-US/1999/?parameter=value"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithParameters", "{language}/{year}");

            RouteData routeData = new RouteData();
            routeData.Values.Add("language", "en-US");
            routeData.Values.Add("year", "1999");

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, routeData, routeCollection);

            Assert.That(urlHelper.CurrentUrl(parameter => "newValue"), Is.EqualTo("/en-US/1999?parameter=newValue"));
            Assert.That(urlHelper.CurrentUrl(PARAMETER => "newValue"), Is.EqualTo("/en-US/1999?PARAMETER=newValue"));
            Assert.That(urlHelper.CurrentUrl(PARAMETER => "newValue"), Is.EqualTo("/en-US/1999?PARAMETER=newValue"));

            Assert.That(urlHelper.CurrentAbsoluteUrl(parameter => "newValue"), Is.EqualTo("http://localhost/en-US/1999?parameter=newValue"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(PARAMETER => "newValue"), Is.EqualTo("http://localhost/en-US/1999?PARAMETER=newValue"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(pArAmEtEr => "newValue"), Is.EqualTo("http://localhost/en-US/1999?pArAmEtEr=newValue"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl_NewQueryStringParameterConvertedToString()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection { {"p", "0" } },
                RequestPath = "/?p=0"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", string.Empty);

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, new RouteData(), routeCollection);

            Assert.That(urlHelper.CurrentUrl(p => new TestClassThatOverridesToString()), Is.EqualTo("/?p=" + TestClassThatOverridesToString.ToStringText));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p => new TestClassThatOverridesToString()), Is.EqualTo("http://localhost/?p=" + TestClassThatOverridesToString.ToStringText));
        }
Beispiel #14
0
 internal static UrlHelper GetUrlHelper(TestHttpContextDefinition testHttpContextDefinition, RouteData routeData, RouteCollection routeCollection)
 {
     return new UrlHelper(new RequestContext(GetHttpContext(testHttpContextDefinition), routeData), routeCollection);
 }
        public void CurrentUrlAndCurrentAbsoluteUrl_ExistingNonEnumerableQueryStringParameterSetToEnumerableParameter_ExistingParameterReplacedWithEnumerableParameter()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection { { "existing", "0" } },
                RequestPath = "/?existing=0"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", string.Empty);

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, new RouteData(), routeCollection);

            Assert.That(urlHelper.CurrentUrl(existing => new[] { 1, 2, 3 }), Is.EqualTo("/?existing=1&existing=2&existing=3"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(existing => new[] { 1, 2, 3 }), Is.EqualTo("http://localhost/?existing=1&existing=2&existing=3"));
        }
        public void ToAbsoluteUrl_HttpRequestHasNoUrlDefined_ThrowsException()
        {
            var httpRequestDefinition = new TestHttpContextDefinition
            {
                SetRequestUrlToNull = true
            };

            var urlHelper = MvcTestHelper.GetUrlHelper(httpRequestDefinition);

            var exception = Assert.Throws<InvalidOperationException>(() => urlHelper.ToAbsoluteUrl("/some/url"));
            Assert.That(exception.Message.Contains("The HTTP request has no URL defined."));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl_ExistingEnumerableQueryStringParameterValuesOverlapWithNewEnumerableParameterValues_OverlappedValuesRemainInQueryString()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection { { "existing", "0" }, { "existing", "1" }, { "existing", "2" } },
                RequestPath = "/?existing=0&existing=1&existing=2"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", string.Empty);

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, new RouteData(), routeCollection);

            Assert.That(urlHelper.CurrentUrl(existing => new[] { 1, 2, 3, 4 }), Is.EqualTo("/?existing=1&existing=2&existing=3&existing=4"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(existing => new[] { 1, 2, 3, 4 }), Is.EqualTo("http://localhost/?existing=1&existing=2&existing=3&existing=4"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl_EnumerableQueryStringParameterValuesContainUrlParameterOptional_OptionalParameterIsIgnored()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection()
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", string.Empty);

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, new RouteData(), routeCollection);

            Assert.That(urlHelper.CurrentUrl(p => new[] { UrlParameter.Optional }), Is.EqualTo("/"));
            Assert.That(urlHelper.CurrentUrl(p => new object[] { 1, UrlParameter.Optional }), Is.EqualTo("/?p=1"));
            Assert.That(urlHelper.CurrentUrl(p => new object[] { UrlParameter.Optional, 1 }), Is.EqualTo("/?p=1"));
            Assert.That(urlHelper.CurrentUrl(p => new object[] { 1, UrlParameter.Optional, 2 }), Is.EqualTo("/?p=1&p=2"));

            Assert.That(urlHelper.CurrentAbsoluteUrl(p => new[] { UrlParameter.Optional }), Is.EqualTo("http://localhost/"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p => new object[] { 1, UrlParameter.Optional }), Is.EqualTo("http://localhost/?p=1"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p => new object[] { UrlParameter.Optional, 1 }), Is.EqualTo("http://localhost/?p=1"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p => new object[] { 1, UrlParameter.Optional, 2 }), Is.EqualTo("http://localhost/?p=1&p=2"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl_EnumerableQueryStringParameterValuesContainNull_ThrowsException()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection()
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", string.Empty);

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, new RouteData(), routeCollection);

            var exception = Assert.Throws<ArgumentException>(() => urlHelper.CurrentUrl(enumerableParameter => new object[] { null }));
            Console.WriteLine(exception.Message);
            Assert.That(exception.ParamName, Is.EqualTo("newRouteAndQueryStringParameters"));
            Assert.That(exception.Message.Contains("The item on the zero-index"));
            Assert.That(exception.Message.Contains("An enumerable item cannot be null."));
            Assert.That(exception.Message.Contains("0"));
            Assert.That(exception.Message.Contains("'enumerableParameter'"));

            exception = Assert.Throws<ArgumentException>(() => urlHelper.CurrentUrl(enumerableParameter => new object[] { null, 1 }));
            Console.WriteLine(exception.Message);
            Assert.That(exception.ParamName, Is.EqualTo("newRouteAndQueryStringParameters"));
            Assert.That(exception.Message.Contains("The item on the zero-index"));
            Assert.That(exception.Message.Contains("An enumerable item cannot be null."));
            Assert.That(exception.Message.Contains("0"));
            Assert.That(exception.Message.Contains("'enumerableParameter'"));

            exception = Assert.Throws<ArgumentException>(() => urlHelper.CurrentUrl(enumerableParameter => new object[] { 1, null }));
            Console.WriteLine(exception.Message);
            Assert.That(exception.ParamName, Is.EqualTo("newRouteAndQueryStringParameters"));
            Assert.That(exception.Message.Contains("The item on the zero-index"));
            Assert.That(exception.Message.Contains("An enumerable item cannot be null."));
            Assert.That(exception.Message.Contains("1"));
            Assert.That(exception.Message.Contains("'enumerableParameter'"));

            exception = Assert.Throws<ArgumentException>(() => urlHelper.CurrentAbsoluteUrl(enumerableParameter => new object[] { null }));
            Console.WriteLine(exception.Message);
            Assert.That(exception.ParamName, Is.EqualTo("newRouteAndQueryStringParameters"));
            Assert.That(exception.Message.Contains("The item on the zero-index"));
            Assert.That(exception.Message.Contains("An enumerable item cannot be null."));
            Assert.That(exception.Message.Contains("0"));
            Assert.That(exception.Message.Contains("'enumerableParameter'"));

            exception = Assert.Throws<ArgumentException>(() => urlHelper.CurrentAbsoluteUrl(enumerableParameter => new object[] { null, 1 }));
            Console.WriteLine(exception.Message);
            Assert.That(exception.ParamName, Is.EqualTo("newRouteAndQueryStringParameters"));
            Assert.That(exception.Message.Contains("The item on the zero-index"));
            Assert.That(exception.Message.Contains("An enumerable item cannot be null."));
            Assert.That(exception.Message.Contains("0"));
            Assert.That(exception.Message.Contains("'enumerableParameter'"));

            exception = Assert.Throws<ArgumentException>(() => urlHelper.CurrentAbsoluteUrl(enumerableParameter => new object[] { 1, null }));
            Console.WriteLine(exception.Message);
            Assert.That(exception.ParamName, Is.EqualTo("newRouteAndQueryStringParameters"));
            Assert.That(exception.Message.Contains("The item on the zero-index"));
            Assert.That(exception.Message.Contains("An enumerable item cannot be null."));
            Assert.That(exception.Message.Contains("1"));
            Assert.That(exception.Message.Contains("'enumerableParameter'"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl__RouteWithParameters_ParameterWithSameNameInQueryStringAndInNewRouteParameters__ParametersOverriddenOnlyInTheQueryString()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection { { "language", "hr-HR" }, { "year", "2000" } },
                RequestPath = "/en-US/1999/?language=hr-HR&year=2000"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithParameters", "{language}/{year}");

            RouteData routeData = new RouteData();
            routeData.Values.Add("language", "en-US");
            routeData.Values.Add("year", "1999");

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, routeData, routeCollection);

            // The order of the parameters in the query string is not important.
            Assert.That(urlHelper.CurrentUrl(language => "fr-FR").StartsWith("/en-US/1999?"));
            Assert.That(urlHelper.CurrentUrl(language => "fr-FR").Contains("language=fr-FR"));
            Assert.That(urlHelper.CurrentUrl(language => "fr-FR").Contains("year=2000"));
            Assert.That(urlHelper.CurrentUrl(language => "fr-FR").Contains("&"));
            Assert.That(!urlHelper.CurrentUrl(language => "fr-FR").EndsWith("&"));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl_NewQueryStringParameterSetToNull_ThrowsException()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection()
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", string.Empty);

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, new RouteData(), routeCollection);

            var exception = Assert.Throws<ArgumentException>(() => urlHelper.CurrentUrl(newQueryStringParameter => null));
            Console.WriteLine(exception.Message);
            Assert.That(exception.ParamName, Is.EqualTo("newRouteAndQueryStringParameters"));
            Assert.That(exception.Message.Contains("A route or query string parameter cannot be set to null."));
            Assert.That(exception.Message.Contains("'newQueryStringParameter'"));

            exception = Assert.Throws<ArgumentException>(() => urlHelper.CurrentAbsoluteUrl(newQueryStringParameter => null));
            Console.WriteLine(exception.Message);
            Assert.That(exception.ParamName, Is.EqualTo("newRouteAndQueryStringParameters"));
            Assert.That(exception.Message.Contains("A route or query string parameter cannot be set to null."));
            Assert.That(exception.Message.Contains("'newQueryStringParameter'"));
        }
Beispiel #22
0
 /// <summary>
 /// Gets the URL helper for specified HTTP context definition and empty route data and route collection.
 /// </summary>
 internal static UrlHelper GetUrlHelper(TestHttpContextDefinition testHttpContextDefinition)
 {
     return GetUrlHelper(GetHttpContext(testHttpContextDefinition), new RouteData(), new RouteCollection());
 }
        public void CurrentUrlAndCurrentAbsoluteUrl_NewRouteParameterConvertedToString()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection(),
                RequestPath = "/parameterValue"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithParameters", "{parameter}");

            RouteData routeData = new RouteData();
            routeData.Values.Add("parameter", "parameterValue");

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, routeData, routeCollection);

            Assert.That(urlHelper.CurrentUrl(parameter => new TestClassThatOverridesToString()), Is.EqualTo("/" + TestClassThatOverridesToString.ToStringText));
            Assert.That(urlHelper.CurrentAbsoluteUrl(parameter => new TestClassThatOverridesToString()), Is.EqualTo("http://localhost/" + TestClassThatOverridesToString.ToStringText));
        }
        public void CurrentUrlAndCurrentAbsoluteUrl_ExistingQueryStringParameterSetToUrlParameterOptional_ParameterRemovedFromQueryString()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                QueryString = new NameValueCollection { { "p1", "1" }, { "p2", "2" }, { "p3", "3" } },
                RequestPath = "/?p1=1&p2=2&p3=3"
            };

            var httpContext = MvcTestHelper.GetHttpContext(httpContextDefinition);

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", string.Empty);

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContext, new RouteData(), routeCollection);

            Assert.That(urlHelper.CurrentUrl(p1 => UrlParameter.Optional), Is.EqualTo("/?p2=2&p3=3"));
            Assert.That(urlHelper.CurrentUrl(p2 => UrlParameter.Optional), Is.EqualTo("/?p1=1&p3=3"));
            Assert.That(urlHelper.CurrentUrl(p3 => UrlParameter.Optional), Is.EqualTo("/?p1=1&p2=2"));
            Assert.That(urlHelper.CurrentUrl(p1 => UrlParameter.Optional, p2 => UrlParameter.Optional), Is.EqualTo("/?p3=3"));
            Assert.That(urlHelper.CurrentUrl(p1 => UrlParameter.Optional, p3 => UrlParameter.Optional), Is.EqualTo("/?p2=2"));
            Assert.That(urlHelper.CurrentUrl(p2 => UrlParameter.Optional, p3 => UrlParameter.Optional), Is.EqualTo("/?p1=1"));
            Assert.That(urlHelper.CurrentUrl(p1 => UrlParameter.Optional, p2 => UrlParameter.Optional, p3 => UrlParameter.Optional), Is.EqualTo("/"));

            Assert.That(urlHelper.CurrentAbsoluteUrl(p1 => UrlParameter.Optional), Is.EqualTo("http://localhost/?p2=2&p3=3"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p2 => UrlParameter.Optional), Is.EqualTo("http://localhost/?p1=1&p3=3"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p3 => UrlParameter.Optional), Is.EqualTo("http://localhost/?p1=1&p2=2"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p1 => UrlParameter.Optional, p2 => UrlParameter.Optional), Is.EqualTo("http://localhost/?p3=3"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p1 => UrlParameter.Optional, p3 => UrlParameter.Optional), Is.EqualTo("http://localhost/?p2=2"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p2 => UrlParameter.Optional, p3 => UrlParameter.Optional), Is.EqualTo("http://localhost/?p1=1"));
            Assert.That(urlHelper.CurrentAbsoluteUrl(p1 => UrlParameter.Optional, p2 => UrlParameter.Optional, p3 => UrlParameter.Optional), Is.EqualTo("http://localhost/"));
        }
        public void ToAbsoluteUrl_RelativeUrlStartsWithSlash_ReturnsAbsoluteUrl()
        {
            var httpContextDefinition = new TestHttpContextDefinition
            {
                RequestPath = "/some/url/"
            };

            RouteCollection routeCollection = new RouteCollection();
            routeCollection.MapRoute("RouteWithoutParameters", "some/url");

            UrlHelper urlHelper = MvcTestHelper.GetUrlHelper(httpContextDefinition, new RouteData(), routeCollection);

            // ReSharper disable PossibleNullReferenceException
            Assert.That(urlHelper.RequestContext.HttpContext.Request.Url.ToString(), Is.EqualTo("http://localhost/some/url/"));
            // ReSharper restore PossibleNullReferenceException

            Assert.That(urlHelper.ToAbsoluteUrl("/relative/url"), Is.EqualTo("http://localhost/relative/url"));
            Assert.That(urlHelper.ToAbsoluteUrl("/relative\\url"), Is.EqualTo("http://localhost/relative/url"));
        }