コード例 #1
0
        public void IsLocal_should_return_false_if_urlString_is_empty()
        {
            // Given when
            var request = new FakeRequest("GET", string.Empty, string.Empty, string.Empty);

            // Then
            Assert.False(request.IsLocal());
        }
コード例 #2
0
        public void IsAjaxRequest_should_return_false_if_request_is_not_ajax()
        {
            // Given when
            var request = new FakeRequest("POST", "/");

            // Then
            Assert.False(request.IsAjaxRequest());
        }
コード例 #3
0
        public void IsLocal_should_return_false_if_userHostAddr_is_not_localhost()
        {
            // Given when
            var request = new FakeRequest("GET", "/", string.Empty, "86.13.73.12");
            request.Url.HostName = "anotherhost";

            // Then
            Assert.False(request.IsLocal());
        }
コード例 #4
0
 public void IsLocal_should_return_true_if_userHostAddr_is_localhost_IPV4()
 {
     // Given when
     var request = new FakeRequest("POST", "/", string.Empty, "127.0.0.1");
     request.Url.HostName = "localhost";
     
     // Then
     Assert.True(request.IsLocal());
 }
コード例 #5
0
        public void IsAjaxRequest_should_return_true_if_request_is_ajax()
        {
            // Given
            var headers =
                new Dictionary<string, IEnumerable<string>>
                    {
                        { "X-Requested-With", new[] { "XMLHttpRequest" } }
                    };

            // When
            var request = new FakeRequest("POST", "/", headers);

            // Then
            Assert.True(request.IsAjaxRequest());
        }
コード例 #6
0
        public void WhenTheBasicCredentialsHaveInvalidValues_ThenTheResultIsHttpBasicUnauthorizedResult()
        {
            userService.Setup(u => u.GetRegisteredUser()).Returns(new User { Salt = "saltsalt", Name = "name", Password = "password" });
            configurationManager.Setup(c => c.AppSetting("keyphrase")).Returns("key");
            var filterContext = new AuthorizationContext();
            var attr = new BasicAuthorizeAttribute();
            attr.UserService = userService.Object;
            attr.ConfigurationManager = configurationManager.Object;
            attr.RequireSsl = false;
            var request = new FakeRequest { SecureConnection = false, Local = false };
            httpContext.Setup(h => h.Request).Returns(request);
            httpContext.Setup(h => h.Response).Returns(new FakeResponse());
            httpContext.SetupProperty(h => h.User);

            request.Values["Authorization"] = "Basic: ";
            filterContext.HttpContext = httpContext.Object;
            attr.OnAuthorization(filterContext);
            filterContext.Result.Should().BeOfType<HttpBasicUnauthorizedResult>();
        }
コード例 #7
0
        public void Should_invoke_route_resolver_with_passed_in_accept_headers_when_path_does_not_contain_file_extensions()
        {
            // Given
            var expectedAcceptHeaders = new List<Tuple<string, decimal>>
            {
                { new Tuple<string, decimal>("application/json", 0.8m) },
                { new Tuple<string, decimal>("application/xml", 0.4m) }
            };

            var requestedAcceptHeaders =
                new List<Tuple<string, decimal>>();

            var request = new FakeRequest("GET", "/")
            {
                Headers = { Accept = expectedAcceptHeaders }
            };

            var context =
                new NancyContext { Request = request };

            var resolvedRoute = new ResolveResult(
               new FakeRoute(),
               DynamicDictionary.Empty,
               null,
               null,
               null);

            A.CallTo(() => this.routeResolver.Resolve(context))
                .Invokes(x => requestedAcceptHeaders = ((NancyContext)x.Arguments[0]).Request.Headers.Accept.ToList())
                .Returns(resolvedRoute);

            // When
            this.requestDispatcher.Dispatch(context);

            // Then
            requestedAcceptHeaders.ShouldHaveCount(2);
            requestedAcceptHeaders[0].Item1.ShouldEqual("application/json");
            requestedAcceptHeaders[0].Item2.ShouldEqual(0.8m);
            requestedAcceptHeaders[1].Item1.ShouldEqual("application/xml");
            requestedAcceptHeaders[1].Item2.ShouldEqual(0.4m);
        }
コード例 #8
0
        public void WhenThereIsNoAuthorizationHeader_ThenTheResultIsHttpBasicUnauthorizedResult()
        {
            var filterContext = new AuthorizationContext();
            var attr = new BasicAuthorizeAttribute();
            attr.RequireSsl = false;
            var request = new FakeRequest { SecureConnection = false, Local = false };
            httpContext.Setup(h => h.Request).Returns(request);

            filterContext.HttpContext = httpContext.Object;
            attr.OnAuthorization(filterContext);
            filterContext.Result.Should().BeOfType<HttpBasicUnauthorizedResult>();
        }
コード例 #9
0
        public void WhenThereIsAPrinciple_ThenTheUserIsStoredInTheContext()
        {
            userService.Setup(u => u.GetRegisteredUser()).Returns(new User { Salt = "saltsalt", Name = "name", Password = "password" });
            configurationManager.Setup(c => c.AppSetting("keyphrase")).Returns("key");
            var filterContext = new AuthorizationContext();
            var attr = new BasicAuthorizeAttribute();
            attr.UserService = userService.Object;
            attr.ConfigurationManager = configurationManager.Object;
            attr.RequireSsl = false;
            var request = new FakeRequest { SecureConnection = false, Local = false };
            httpContext.Setup(h => h.Request).Returns(request);
            httpContext.Setup(h => h.Response).Returns(new FakeResponse());
            httpContext.SetupProperty(h => h.User);

            request.Values["Authorization"] = "Basic: " + ToBase64("name", "password");
            filterContext.HttpContext = httpContext.Object;
            attr.OnAuthorization(filterContext);
            filterContext.HttpContext.User.Should().BeOfType<GenericPrincipal>();
        }