public void ControllerGetsPopulated()
 {
     var router=new Router();
     var home=router.Controller(c => new HomeController(c));
     home.Handles("/home").With(x=>x.GetHome());
     var context=new FakeServerContext();
     context.RequestUrl=new Uri("http://foo.bar/home");
     context.HttpMethod="get";
     router.Execute(context);
     Assert.AreEqual("mehfoobar", context.WrittenText());
 }
示例#2
0
 public void RequiresLogin_should_pass_through_when_authorized()
 {
     var mock=new FakeServerContext();
     var auth=new FSCAuth(mock, new FSCAuthConfig(), new SimpleUserStore());
     Prep(auth);
     auth.Login("user", "pass");
     var cookie=mock.ResponseCookies.Single();
     mock.ResponseCookies.Clear();
     mock.RequestCookies.Add(cookie);
     auth.RequiresAuthentication(); //ensure this doesn't cause FakeServerContext to throw FakeServerKilled
 }
示例#3
0
 public void Login_should_work_when_given_proper_credentials()
 {
     var mock=new FakeServerContext();
     var auth=new FSCAuth(mock, new FSCAuthConfig(), new SimpleUserStore());
     Prep (auth);
     auth.Login("user", "pass");
     var cookie=mock.ResponseCookies.Single();
     Assert.AreEqual(auth.Config.SiteName+"_login", cookie.Name);
     Assert.IsTrue(cookie["secret"].Length>0);
     Assert.IsNotNull(auth.CurrentUser);
 }
示例#4
0
 public void RequiresInGroup_should_redirect()
 {
     var mock=new FakeServerContext();
     var auth=new FSCAuth(mock, new FSCAuthConfig(), new SimpleUserStore());
     Prep(auth);
     auth.Login("user", "pass");
     try
     {
         auth.RequiresInGroup("foo");
     }catch(HttpException e)
     {
         Assert.AreEqual(403, e.GetHttpCode());
         return;
     }
     Assert.Fail("Should not reach here. No exception thrown");
 }
示例#5
0
 public void Execute_DefaultsToOnlyGetHttpMethod()
 {
     var router=new Router();
     var r=new Route
     {
         Responder=(RequestContext c, ref bool skip) => new WrapperView("foo"),
         AllowedMethods=null,
         Pattern=new FakePatternMatcher("/foo")
     };
     router.AddRoute(r);
     var context=new FakeServerContext();
     context.RequestUrl=new Uri("http://meh.com/foo");
     context.HttpMethod="post";
     Assert.IsFalse(router.Execute(context));
     context.HttpMethod="get";
     Assert.IsTrue(router.Execute(context));
 }
示例#6
0
 public void Login_Should_Send_401_When_Basic()
 {
     var mock=new FakeServerContext();
     var auth=new FSCAuth(mock,new FSCAuthConfig(), new SimpleUserStore(), true);
     auth.Config.AllowBasicAuth=true;
     auth.Config.SiteName="Foo bar";
     Prep(auth);
     bool threw=false;
     try
     {
         auth.RequiresAuthentication();
     }
     catch(HttpException e)
     {
         Assert.AreEqual(401, e.GetHttpCode());
         return;
     }
     Assert.Fail("Should not reach here");
 }
示例#7
0
 public void Execute_ChecksValidatorsForBadParameters()
 {
     var router=new Router();
     var parameters = new ParameterDictionary();
     parameters.Add("id", "1234");
     var badvalidators = new List<RouteParamsMustMatch>();
     badvalidators.Add(x => x["id"] == "xxx");
     var bad=new Route
     {
         Responder=(RequestContext c, ref bool skip) => new WrapperView("bad"),
         Pattern=new FakePatternMatcher("/foo/1234", parameters),
         ParameterValidators=badvalidators
     };
     router.AddRoute(bad);
     var context=new FakeServerContext();
     context.RequestUrl=new Uri("http://meh.com/foo/1234");
     context.HttpMethod="get";
     Assert.IsFalse(router.Execute(context));
 }
示例#8
0
 public void RequiresLogin_should_redirect_when_not_logged_in()
 {
     var mock=new FakeServerContext();
     var auth=new FSCAuth(mock, new FSCAuthConfig(), new SimpleUserStore());
     Prep(auth);
     bool threw=false;
     try
     {
         auth.RequiresAuthentication();
     }
     catch(FakeServerKilledException)
     {
         threw=true;
     }
     Assert.IsTrue(threw);
     Assert.AreEqual("/login", mock.RedirectedTo);
 }
示例#9
0
        public void Execute_FindsProperRoute()
        {
            var router=new Router();
            router.AddRoute(new Route());
            var rt=new Route();
            router.AddRoute(rt);
            rt.Pattern=new FakePatternMatcher("/foo");
            var context=new FakeServerContext();
            context.HttpMethod="GET";
            context.RequestUrl=new Uri("http://meh.com/foo");
            rt.Responder=(RequestContext c,ref bool skip ) => {
                var view=new WrapperView("foo");
                Assert.AreEqual(c.Context, context);
                return view;
            };

            var res=router.Execute(context);
            Assert.IsTrue(res);

            Assert.AreEqual("foo", context.WrittenText());
        }
示例#10
0
 public void Execute_ReturnsFalseOnNotFound()
 {
     var router=new Router();
     router.AddRoute(new Route());
     router.AddRoute(new Route{Pattern=new FakePatternMatcher("/meh")});
     var context=new FakeServerContext();
     context.RequestUrl=new Uri("http://meh.com/foo");
     Assert.IsFalse(router.Execute(context));
 }
示例#11
0
 public void Execute_RespectsMultipleAllowedHttpMethods()
 {
     var router=new Router();
     router.AddRoute(new Route
     {
         Responder=(RequestContext c, ref bool skip) => new WrapperView("foo"),
         AllowedMethods=new string[]{"POST", "meh"},
         Pattern=new FakePatternMatcher("/foo")
     });
     var context=new FakeServerContext();
     context.RequestUrl=new Uri("http://meh.com/foo");
     context.HttpMethod="meh";
     Assert.IsTrue(router.Execute(context));
 }
示例#12
0
 public void FromXXX_Should_Populate_Model_In_An_Additive_Manner()
 {
     var r = new Router();
     var ctrl = r.Controller(c => new TestController(c));
     var foo = ctrl.Handles("/foo").
         UsingModel(c => new TestModel()).
         FromForm().
         FromRoute().
         With((c, m) => c.TestWithModel(m));
     bool skip = false;
     var formparams=new ParameterDictionary();
     formparams.Add("Foo", new string[]{"bar"});
     var context = new FakeServerContext();
     context.Form = formparams;
     var routeparams = new ParameterDictionary();
     routeparams.Add("Bar", new string[]{"baz"});
     var view=foo.Current.Responder(
         new RequestContext(context, null, null, routeparams), ref skip);
     Assert.AreEqual("foobarbaz", view.ToString());
 }