Exemple #1
0
        public void BuildComplexUrl_Get()
        {
            string expected = String.Format("http://testprefix/foo/1/bar/List(foo,2,3)/baz/(deeper:List(found,it,{0}))/biz/123", complexStringEscapedPath);

            GetRequestBuilder <int, Greeting> requestBuilder = new GetRequestBuilder <int, Greeting>("foo/{one}/bar/{list}/baz/{complex}/biz");

            requestBuilder.SetID(123);
            requestBuilder.SetPathKey("one", 1);
            requestBuilder.SetPathKey("list", new List <object>()
            {
                "foo", 2, 3
            });
            requestBuilder.SetPathKey("complex", new Dictionary <string, object>()
            {
                { "deeper", new List <object>()
                  {
                      "found", "it", complexString
                  } }
            });
            GetRequest <int, Greeting> request = requestBuilder.Build();

            RequestUrlBuilder <EntityResponse <Greeting> > urlBuilder = new RequestUrlBuilder <EntityResponse <Greeting> >(request, "http://testprefix");
            Uri url = urlBuilder.Build();

            Assert.AreEqual(expected, url.AbsoluteUri);
        }
        public void GetGreeting_Sync_ServerError()
        {
            RestClient client = new RestClient(urlPrefix);

            GetRequestBuilder <int, Greeting> requestBuilder = new GetRequestBuilder <int, Greeting>("/basicCollection");

            requestBuilder.SetID(-1);
            GetRequest <int, Greeting> request = requestBuilder.Build();

            try
            {
                EntityResponse <Greeting> response = client.RestRequestSync(request);
            }
            catch (RestliException e)
            {
                Assert.IsNotNull(e.Message);
                Assert.IsNull(e.InnerException);

                ErrorResponse details = e.details;
                Assert.IsNotNull(details);
                Assert.AreEqual(400, details.status);
                Assert.AreEqual("Negative key.", details.message);
                Assert.AreEqual("com.linkedin.restli.server.RestLiServiceException", details.exceptionClass);
                Assert.IsTrue(details.hasStackTrace);
                return;
            }

            Assert.Fail("Rest request should throw RestliException");
        }
        public void GetGreeting_Async_ServerError()
        {
            RestClient client = new RestClient(urlPrefix);

            GetRequestBuilder <int, Greeting> requestBuilder = new GetRequestBuilder <int, Greeting>("/basicCollection");

            requestBuilder.SetID(-1);
            GetRequest <int, Greeting> request = requestBuilder.Build();

            AutoResetEvent blocker = new AutoResetEvent(false);

            Greeting            greeting      = null;
            ClientErrorResponse errorResponse = null;

            RestliCallback <EntityResponse <Greeting> > .SuccessHandler successHandler = delegate(EntityResponse <Greeting> response)
            {
                greeting = response.element;
                blocker.Set();
            };
            RestliCallback <EntityResponse <Greeting> > .ErrorHandler errorHandler = delegate(ClientErrorResponse response)
            {
                errorResponse = response;
                blocker.Set();
            };
            RestliCallback <EntityResponse <Greeting> > callback = new RestliCallback <EntityResponse <Greeting> >(successHandler, errorHandler);

            client.RestRequestAsync(request, callback);

            blocker.WaitOne();

            Assert.IsNull(greeting);
            Assert.IsNotNull(errorResponse);
            Assert.AreEqual(400, errorResponse.status);

            RestliException error = errorResponse.error;

            Assert.IsNotNull(error);
            Assert.IsNotNull(error.Message);
            Assert.IsNull(error.InnerException);

            ErrorResponse details = error.details;

            Assert.IsNotNull(details);
            Assert.AreEqual(400, details.status);
            Assert.AreEqual("Negative key.", details.message);
            Assert.AreEqual("com.linkedin.restli.server.RestLiServiceException", details.exceptionClass);
            Assert.IsTrue(details.hasStackTrace);
        }
        public void GetGreeting_Sync()
        {
            RestClient client = new RestClient(urlPrefix);

            GetRequestBuilder <int, Greeting> requestBuilder = new GetRequestBuilder <int, Greeting>("/basicCollection");

            requestBuilder.SetID(123);
            GetRequest <int, Greeting> request = requestBuilder.Build();

            EntityResponse <Greeting> response = client.RestRequestSync(request);

            Greeting greeting = response.element;

            Assert.IsNotNull(greeting);

            Assert.AreEqual(123, greeting.id);
            Assert.AreEqual(Tone.Symbol.SINCERE, greeting.tone.symbol);
            Assert.AreEqual("Hello World!", greeting.message);
        }
        public void GetGreeting_Async_HttpError()
        {
            RestClient client = new RestClient(badUrlPrefix);

            GetRequestBuilder <int, Greeting> requestBuilder = new GetRequestBuilder <int, Greeting>("/basicCollection");

            requestBuilder.SetID(123);
            GetRequest <int, Greeting> request = requestBuilder.Build();

            AutoResetEvent blocker = new AutoResetEvent(false);

            Greeting            greeting      = null;
            ClientErrorResponse errorResponse = null;

            RestliCallback <EntityResponse <Greeting> > .SuccessHandler successHandler = delegate(EntityResponse <Greeting> response)
            {
                greeting = response.element;
                blocker.Set();
            };
            RestliCallback <EntityResponse <Greeting> > .ErrorHandler errorHandler = delegate(ClientErrorResponse response)
            {
                errorResponse = response;
                blocker.Set();
            };
            RestliCallback <EntityResponse <Greeting> > callback = new RestliCallback <EntityResponse <Greeting> >(successHandler, errorHandler);

            client.RestRequestAsync(request, callback);

            blocker.WaitOne();

            Assert.IsNull(greeting);
            Assert.IsNotNull(errorResponse);
            Assert.AreEqual(500, errorResponse.status);

            Assert.IsNotNull(errorResponse.error);
            Assert.IsNull(errorResponse.error.details);
            Assert.IsNotNull(errorResponse.error.Message);
            Assert.IsNotNull(errorResponse.error.InnerException);
        }
        public void GetGreeting_Sync_HttpError()
        {
            RestClient client = new RestClient(badUrlPrefix);

            GetRequestBuilder <int, Greeting> requestBuilder = new GetRequestBuilder <int, Greeting>("/basicCollection");

            requestBuilder.SetID(123);
            GetRequest <int, Greeting> request = requestBuilder.Build();

            try
            {
                EntityResponse <Greeting> response = client.RestRequestSync(request);
            }
            catch (RestliException e)
            {
                Assert.IsNull(e.details);
                Assert.IsNotNull(e.Message);
                Assert.IsNotNull(e.InnerException);
                return;
            }

            Assert.Fail("Rest request should throw RestliException");
        }
        public void GetGreeting_Async()
        {
            RestClient client = new RestClient(urlPrefix);

            GetRequestBuilder <int, Greeting> requestBuilder = new GetRequestBuilder <int, Greeting>("/basicCollection");

            requestBuilder.SetID(123);
            GetRequest <int, Greeting> request = requestBuilder.Build();

            AutoResetEvent blocker = new AutoResetEvent(false);

            Greeting            greeting      = null;
            ClientErrorResponse errorResponse = null;

            RestliCallback <EntityResponse <Greeting> > .SuccessHandler successHandler = delegate(EntityResponse <Greeting> response)
            {
                greeting = response.element;
                blocker.Set();
            };
            RestliCallback <EntityResponse <Greeting> > .ErrorHandler errorHandler = delegate(ClientErrorResponse response)
            {
                errorResponse = response;
                blocker.Set();
            };
            RestliCallback <EntityResponse <Greeting> > callback = new RestliCallback <EntityResponse <Greeting> >(successHandler, errorHandler);

            client.RestRequestAsync(request, callback);

            blocker.WaitOne(asyncTimeoutMillis);

            Assert.IsNull(errorResponse);
            Assert.IsNotNull(greeting);

            Assert.AreEqual(123, greeting.id);
            Assert.AreEqual(Tone.Symbol.SINCERE, greeting.tone.symbol);
            Assert.AreEqual("Hello World!", greeting.message);
        }