public void TestCreateWithStringThrowsArgumentExceptionWhenResourceNullOrEmpty()
        {
            var unit = new RestRequestFactory();

            try
            {
                string resource = null;
                var    result   = unit.Create(resource, Method.GET);
                Assert.Fail("Expected ArgumentException not thrown.");
            }
            catch (ArgumentException)
            {
                //expected
            }

            try
            {
                string resource = string.Empty;
                var    result   = unit.Create(resource, Method.GET);
                Assert.Fail("Expected ArgumentException not thrown.");
            }
            catch (ArgumentException)
            {
                //expected
            }

            var pass = unit.Create("pass", Method.GET);
        }
Beispiel #2
0
        public void RestRequestUpdateHeadersTest()
        {
            var context = new SessionContext();
            var uri     = new Uri("http://localhost/api");
            var factory = new RestRequestFactory();
            var target  = factory.Create(uri, context);
            var headers = new NameValueCollection
            {
                { "header1", "value1" },
                { "User-Agent", "UnitTest" },
                { "accept", "plain/text" },
                { "content-type", "application/xml" },
                { "Authorization", "my-hash" }
            };

            Assert.IsTrue(string.IsNullOrEmpty(target.HeaderValue("header1")), "header1 doesn't exist upfront");
            Assert.IsTrue(target.HeaderValue("Accept").Contains("application/json"),
                          "Accept contains application/json upfront");
            target.UpdateHeaders(headers);
            Assert.AreEqual("value1", target.HeaderValue("header1"), "header1 exists afterwards");
            Assert.AreEqual("UnitTest", target.HeaderValue("User-Agent"), "User-Agent changed");
            Assert.AreEqual("plain/text", target.HeaderValue("Accept"), "Accept changed");
            Assert.AreEqual("application/xml", target.HeaderValue("Content-Type"), "Content-Type changed");
            Assert.AreEqual("my-hash", target.HeaderValue("Authorization"), "Authorization changed");
        }
Beispiel #3
0
        public static int GetVersion()
        {
            var rc     = new RestClient(Definitions.OrigindServerUrl);
            var result = rc.Get(RestRequestFactory.Create(Definitions.Rest.LauncherVersion));

            return(int.Parse(result.Content));
        }
        public void TestCreateWithString()
        {
            var unit   = new RestRequestFactory();
            var result = unit.Create("/resource/", Method.GET);

            Assert.IsNotNull(result);
            Assert.AreEqual(DataFormat.Json, result.RequestFormat);
            Assert.IsInstanceOfType(result.JsonSerializer, typeof(NewtonsoftSerializer));
        }
Beispiel #5
0
        private IRestResponse getCEP(object CEP)
        {
            var client = restClientFactory.Create("http://viacep.com.br/ws/" + CEP + "/json/");
            //var client = new RestClient(baseUrl);
            var restRequest = restRequestFactory.Create(Method.GET, DataFormat.Json);

            //var restRequest = new RestRequest(method) { RequestFormat = format };

            return(client.Execute(restRequest));
        }
Beispiel #6
0
        public void RestRequestCreateTest()
        {
            var context = new SessionContext();
            var uri     = new Uri("http://localhost/api");
            var factory = new RestRequestFactory();
            var target  = factory.Create(uri, context);

            Assert.IsNotNull(target);
            Assert.AreEqual(uri, target.RequestUri);
            Assert.AreEqual("FitNesseRest", target.HeaderValue("User-Agent"));
        }
Beispiel #7
0
        private async Task <ResourceListResponse> GetResourceList(string resourceName)
        {
            var request = RestRequestFactory.Create(resourceName);
            //call twice to get full list
            var firstResponse = await Execute <ResourceListResponse>(request);

            var total = firstResponse.Count.ToString();

            Console.WriteLine($"** Getting {total} {resourceName} resources.");
            return(await Execute <ResourceListResponse>(request.AddQueryParameter("limit", total)));
        }
        public void TestCreateWithUriThrowsArgumentNullExceptionWhenUriNull()
        {
            var unit = new RestRequestFactory();

            try
            {
                Uri uri    = null;
                var result = unit.Create(uri, Method.GET);
                Assert.Fail("Expected ArgumentNullException not thrown.");
            }
            catch (ArgumentNullException)
            {
                //expected
            }
        }
Beispiel #9
0
        public T Execute()
        {
            var request = RestRequestFactory.Create((new T()).GenerateQueryResource(Id), Method.GET);

            if (Fields != null && Fields.Any() == true)
            {
                var fieldParamValue = FieldParameterValueFactory.Create(Fields);
                request.AddQueryParameter(QueryKeyFields, fieldParamValue);
            }
            var response = RestClient.Execute <T>(request);

            RestResponseValidator.Validate(request, response);
            var result = response.Data;

            return(result);
        }
Beispiel #10
0
        public void RestRequestSetBodyForGetTest()
        {
            var context     = new SessionContext();
            var uri         = new Uri("http://localhost/api");
            var factory     = new RestRequestFactory();
            var restRequest = factory.Create(uri, context);
            var method      = restRequest.GetType().GetMethod("SetBody", BindingFlags.Instance | BindingFlags.NonPublic);

            Assert.IsNotNull(method, "method nut null");
            method.Invoke(restRequest, new object[] { "hello", Encoding.GetEncoding("iso-8859-1"), "GET" });

            var fieldInfo = restRequest.GetType().GetField("_request", BindingFlags.Instance | BindingFlags.NonPublic);

            Assert.IsNotNull(fieldInfo, "FieldInfo not null");
            var req = fieldInfo.GetValue(restRequest) as HttpWebRequest;

            Assert.AreEqual(0, req?.ContentLength);
        }
Beispiel #11
0
        public IRestRequest CreateBaseRestRequest(int skip, int limit)
        {
            if (limit <= 0)
            {
                throw new ArgumentException("Limit must be greater than 0.", nameof(limit));
            }
            var request = RestRequestFactory.Create((new T().GenerateScanResource()), Method.GET);

            request.AddQueryParameter(QueryKeySkip, skip.ToString("F0"));
            request.AddQueryParameter(QueryKeyLimit, limit.ToString("F0"));
            if (Fields != null && Fields.Any() == true)
            {
                var fieldParamValue = FieldParameterValueFactory.Create(Fields);
                request.AddQueryParameter(QueryKeyFields, fieldParamValue);
            }
            if (string.IsNullOrWhiteSpace(SearchQuery) == false)
            {
                request.AddQueryParameter(QueryKeySearchQuery, SearchQuery);
            }

            return(request);
        }
Beispiel #12
0
        public void RestRequestUpdateWrongHeaderTest()
        {
            var context = new SessionContext();
            var uri     = new Uri("http://localhost/api");
            var factory = new RestRequestFactory();
            var target  = factory.Create(uri, context);

            // We force a Date entry in the Headers property of the request by setting the request's Date property
            // That will cause the case statement in UpdateHeaders to choose default (i.e. throw argumentexception).
            var fieldInfo = target.GetType().GetField("_request", BindingFlags.Instance | BindingFlags.NonPublic);

            Assert.IsNotNull(fieldInfo, "fieldInfo not null");
            var request = fieldInfo.GetValue(target) as HttpWebRequest;

            Assert.IsNotNull(request, "Request field captured");
            request.Date = new DateTime(2019, 02, 23);

            var headers = new NameValueCollection
            {
                { "Date", "1-Jan-2000" }
            };

            target.UpdateHeaders(headers);
        }
Beispiel #13
0
        public void Create_Given_URLAndMethod_Returns_RestSharpsRestRequestObject()
        {
            var result = factory.Create("test/1", Method.GET);

            result.Should().BeOfType <RestRequest>();
        }