Пример #1
0
        public void can_set_RootUrl_on_instance_or_globally()
        {
            var requestor = new Requestor();

            requestor.RootUrl.Should(Be.Null);

            // trying to do a GET to a relative path without a RootUrl will cause an exception
            var message  = "HttpRequestor.GetResponse failed for: GET /foo.  This url generated a System.Net.FileWebRequest instead of a HttpWebRequest.";
            var response = requestor.Get("/foo");             // <--- this doesn't raise an exception, but returns null

            response.Should(Be.Null);
            requestor.LastException.Should(Be.AssignableFrom(typeof(Exception)));
            requestor.LastException.Message.ShouldContain(message);

            // if we switch to our SimpleRequestor, tho, we're OK
            Requestor.Global.Implementation = new SimpleRequestor();
            requestor.Get("/foo").Body.ShouldEqual("You requested: GET /foo");             // <-- no RootUrl

            // if we pass the full path, it works
            requestor.Get("http://localhost:3000/").Body.ShouldEqual("You requested: GET http://localhost:3000/");

            // set RootUrl globally
            Requestor.Global.RootUrl = "http://google.com";
            requestor.RootUrl.ShouldEqual("http://google.com");
            requestor.Get("/foo").Body.ShouldEqual("You requested: GET http://google.com/foo");

            // override on instance
            requestor.RootUrl = "http://yahoo.com";
            requestor.RootUrl.ShouldEqual("http://yahoo.com");
            requestor.Get("/foo").Body.ShouldEqual("You requested: GET http://yahoo.com/foo");
            new Requestor().Get("/foo").Body.ShouldEqual("You requested: GET http://google.com/foo");             // new still gets global

            // if we pass the full path, it still works, even with a RootUrl
            requestor.Get("http://localhost:3000/").Body.ShouldEqual("You requested: GET http://localhost:3000/");
        }