public void GetSinglePostById(int id, string title)
        {
            ResponseContext responseContext = new RestAssured()
                                              .Given()
                                              .Name(Name)
                                              .Header("Content-Type", "application/json")
                                              .Header("CharSet", "utf-8")
                                              .When()
                                              .Get($"https://jsonplaceholder.typicode.com/posts/{id}")
                                              .Then();

            var test = responseContext.Retrieve(x => x.ToString());

            Console.WriteLine(test);

            responseContext.TestBody(
                "Verify an existing single post is returned with an HTTP GET",
                x =>
            {
                int.TryParse(x.id.ToString(), out int actualId);
                Assert.AreEqual(id, actualId);
                Assert.AreEqual(title, x.title.ToString());
                return(x.id == id && x.title == title);
            })
            .Assert("Verified");
        }
        public void UpdateAnExistingPost()
        {
            int expectedId = 1;

            var postDto = new PostBuilder()
                          .WithTitle("Put")
                          .Build();

            string payload = JsonConvert.SerializeObject(postDto);

            ResponseContext responseContext = new RestAssured()
                                              .Given()
                                              .Name(Name)
                                              .Header("Content-Type", "application/json")
                                              .Body(payload)
                                              .When()
                                              .Put($"https://jsonplaceholder.typicode.com/posts/{expectedId}")
                                              .Then();

            responseContext.TestBody(
                "Verify an existing Post resource can be updated with HTTP PUT",
                x =>
            {
                int.TryParse(x.id.ToString(), out int actualId);
                Assert.AreEqual(expectedId, actualId);
                return(x.id == expectedId);
            });
        }
        public void CreateANewPost()
        {
            var    postDto = new PostBuilder().Build();
            string payload = JsonConvert.SerializeObject(postDto);

            ResponseContext responseContext = new RestAssured()
                                              .Given()
                                              .Name(Name)
                                              .Header("Content-Type", "application/json")
                                              .Body(payload)
                                              .When()
                                              .Post("https://jsonplaceholder.typicode.com/posts")
                                              .Then();

            responseContext.TestBody(
                "Verify a new Post resource can be created with HTTP POST by confirming the Id returned in the response",
                x =>
            {
                int.TryParse(x.id.ToString(), out int actualId);
                Assert.AreEqual(101, actualId);
                return(x.id == 101);
            });

            responseContext.TestStatus(
                "Verify a new Post resource can be created with HTTP POST by confirming the response HTTP status code",
                x =>
            {
                Assert.AreEqual(201, x);
                return(x == 201);
            });


            responseContext.AssertAll();
        }
Esempio n. 4
0
        public void GetAll(string resource, int resourceCount)
        {
            ResponseContext responseContext = new RestAssured()
                                              .Given()
                                              .Name(Name)
                                              .Header("Content-Type", "application/json")
                                              .Header("CharSet", "utf-8")
                                              .When()
                                              .Get($"https://jsonplaceholder.typicode.com/{resource}")
                                              .Then();

            responseContext.TestBody(
                $"Verify {resource} are returned when the {resource} endpoint is called with HTTP GET and no parameters",
                x =>
            {
                Assert.AreEqual(resourceCount, x.Count);
                return(x.count() == resourceCount);
            })
            .Assert("Verified");
        }
        public void GetAllPostsByUserWithId()
        {
            int expectedPostCount = 10;

            ResponseContext responseContext = new RestAssured()
                                              .Given()
                                              .Name(Name)
                                              .Header("Content-Type", "application/json")
                                              .Header("CharSet", "utf-8")
                                              .When()
                                              .Get("https://jsonplaceholder.typicode.com/posts?userId=1")
                                              .Then();

            responseContext.TestBody("Verify all posts for a user are returned by Id with an HTTP GET",
                                     x =>
            {
                Assert.AreEqual(expectedPostCount, x.Count);
                return(x.Count == expectedPostCount);
            })
            .Assert("Verified");
        }
        public void GetAllNestedCommentsForASpecificPost()
        {
            int expectedCommentCount = 5;

            ResponseContext responseContext = new RestAssured()
                                              .Given()
                                              .Name(Name)
                                              .Header("Content-Type", "application/json")
                                              .Header("CharSet", "utf-8")
                                              .When()
                                              .Get("https://jsonplaceholder.typicode.com/posts/1/comments")
                                              .Then();

            responseContext.TestBody(
                "Verify all comments are found for a specific post found with an HTTP GET",
                x =>
            {
                Assert.AreEqual(expectedCommentCount, x.Count);
                return(x.Count == expectedCommentCount);
            })
            .Assert("Verified");
        }