Пример #1
0
        public async Task ShouldCallGetStudentMethod()
        {
            string url      = "http://example.org";
            var    mockHttp = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, url + "/api/students/1")
            .Respond("application/json", "{'firstName':'Bob', 'lastName':'Walker', 'age': 22}");

            var settings = new JsonRestSettings()
            {
                HttpMessageHandlerFactory = () => mockHttp
            };

            IApi05 restClient = RestClient.For <IApi05>(url, settings);

            Person student = await restClient.GetStudentAsync();

            student.FirstName
            .ShouldBeEquivalentTo("Bob");
            student.LastName
            .ShouldBeEquivalentTo("Walker");
            student.Age
            .ShouldBeEquivalentTo(22);

            mockHttp.VerifyNoOutstandingExpectation();
        }
Пример #2
0
        public async Task ShouldHaveUniqueContent02()
        {
            string url      = "http://example.org";
            var    mockHttp = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, url + "/api/posts")
            .Respond(HttpStatusCode.OK);

            var settings = new JsonRestSettings()
            {
                HttpMessageHandlerFactory = () => mockHttp
            };

            IApi07 restClient = RestClient.For <IApi07>(url, settings);

            var form = new Dictionary <string, string>
            {
                ["firstName"] = "John",
                ["lastName"]  = "Doe"
            };

            var response = await restClient.GetUniqueContent02Async(form);

            response.RequestMessage
            .Content
            .Should()
            .BeOfType <FormUrlEncodedContent>()
            .Which
            .ReadAsStringAsync()
            .Result
            .ShouldBeEquivalentTo("firstName=John&lastName=Doe");

            mockHttp.VerifyNoOutstandingExpectation();
        }
Пример #3
0
        public async Task ShouldHaveUniqueContent01()
        {
            string url      = "http://example.org";
            string content  = "myContent";
            var    mockHttp = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, url + "/api/posts")
            .Respond(HttpStatusCode.OK);

            var settings = new JsonRestSettings()
            {
                HttpMessageHandlerFactory = () => mockHttp
            };

            IApi07 restClient = RestClient.For <IApi07>(url, settings);

            var response = await restClient.GetUniqueContent01Async(content);

            response.RequestMessage
            .Content
            .Should()
            .BeOfType <StringContent>()
            .Which
            .ReadAsStringAsync()
            .Result
            .ShouldBeEquivalentTo(content);

            mockHttp.VerifyNoOutstandingExpectation();
        }
Пример #4
0
        public async Task ShouldObjectHaveHeaderValues()
        {
            string url      = "http://example.org/api/people";
            var    mockHttp = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, url)
            .Respond(x =>
            {
                var response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("[{'firstName':'A','lastName':'AA'},{'firstName':'B','lastName':'BB'}]", Encoding.UTF8, "application/json")
                };

                response.Headers.Add(PaginationPage, "1");
                response.Headers.Add(PaginationPageCount, "2");

                return(response);
            });


            var settings = new JsonRestSettings()
            {
                HttpMessageHandlerFactory = () => mockHttp,
                HeaderWriter = new HeaderWriter()
            };

            IApi09 restClient = RestClient.For <IApi09>(url, settings);

            var people = await restClient.GetPagedPeopleAsync();

            people.Should()
            .HaveCount(2);

            people.Page
            .ShouldBeEquivalentTo(1);

            people.PageCount
            .ShouldBeEquivalentTo(2);

            mockHttp.VerifyNoOutstandingExpectation();
        }
Пример #5
0
        public async Task ShouldHaveMultipartContent03()
        {
            string url = "http://example.org";

            string content  = @"--RestLessBoundary
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=content

doe
--RestLessBoundary
Content-Type: text/plain
Content-Disposition: form-data; name=firstName; filename=f; filename*=utf-8''f

john
--RestLessBoundary--
";
            var    mockHttp = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, url + "/api/posts")
            .Respond(HttpStatusCode.OK);

            var settings = new JsonRestSettings()
            {
                HttpMessageHandlerFactory = () => mockHttp
            };

            IApi07 restClient = RestClient.For <IApi07>(url, settings);

            var response = await restClient.GetMultipartContent03Async("doe", "john");

            response.RequestMessage
            .Content
            .Should()
            .BeOfType <MultipartFormDataContent>()
            .Which
            .ReadAsStringAsync()
            .Result
            .ShouldBeEquivalentTo(content);

            mockHttp.VerifyNoOutstandingExpectation();
        }
Пример #6
0
        public async Task ShouldHaveMultipartContent01()
        {
            string url      = "http://example.org";
            string content  = @"--RestLessBoundary
Content-Disposition: form-data; name=content; filename=Api07File.xml; filename*=utf-8''Api07File.xml

<?xml version=""1.0"" encoding=""utf-8"" ?>
<Project/>
--RestLessBoundary--
";
            var    mockHttp = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, url + "/api/posts")
            .Respond(HttpStatusCode.OK);

            var settings = new JsonRestSettings()
            {
                HttpMessageHandlerFactory = () => mockHttp
            };

            IApi07 restClient = RestClient.For <IApi07>(url, settings);

            var fileInfo = new FileInfo("Assets\\Api07File.xml");

            var response = await restClient.GetMultipartContent01Async(fileInfo);

            response.RequestMessage
            .Content
            .Should()
            .BeOfType <MultipartFormDataContent>()
            .Which
            .ReadAsStringAsync()
            .Result
            .ShouldBeEquivalentTo(content);

            mockHttp.VerifyNoOutstandingExpectation();
        }