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 CreateAPostUsingTheWrongDataTypes() { string payload = @" { ""id"": ""test"", ""userId"": ""test"", ""title"": 1, ""body"": 1 }"; ResponseContext responseContext = new RestAssured() .Given() .Name(Name) .Header("Content-Type", "application/json") .Body(payload) .When() .Post("https://jsonplaceholder.typicode.com/posts") .Then(); ResponseContext testStatus = responseContext.TestStatus( "Verify a Post resource cannot be created with an empty Body with HTTP POST", x => { if (x == 201) { Assert.Pass("An expected failure occured due to a known issue with the API - wrong data types in Body"); } else { Assert.Fail(); } return(x != 201); }); }
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(); }
public void TestMethod1() { //Response retreived // { // "ip":"2605:6000:f705:ab00:78e3:1959:78d4:bd76", // "about":"/about", // "Pro!":"http://getjsonip.com" //} new RestAssured() .Given() //Optional, set the name of this suite .Name("JsonIP Test Suite") //Optional, set the header parameters. //Defaults will be set to application/json if none is given .Header("Content-Type", "application/json") .Header("Accept-Encoding", "gzip,deflate") .When() //url .Get("http://jsonip.com") .Then() //Give the name of the test and a lambda expression to test with //The lambda expression keys off of 'x' which represents the json blob as a dynamic. .TestBody("test a", x => x.about != null) //Throw an AssertException if the test case is false. .Assert("test a") ; var response = new RestAssured() .Given() //Optional, set the name of this suite .Name("JsonIP Test Suite") //Optional, set the header parameters. //Defaults will be set to application/json if none is given .Header("Content-Type", "application/json") .Header("Accept-Encoding", "gzip,deflate") .When() //url .Get("http://jsonip.com"); //.Then() // .Retrieve(x=>x.about); response.Then().TestBody("", x => x.about); }
public void VerifyFakeCountryNotExists() { var message = new RestAssured() .Given() .Name("Validate Fake Country does not exist") .Host(ApiService) .Uri("/country/get/iso2code/FAKE") .When() .Get() .Then() .TestStatus("Verify Responde Code is 200", code => code == 200) .Assert("Verify Responde Code is 200") .Retrieve(msg => msg.RestResponse.messages); Assert.IsTrue(message.ToString().Contains("No matching country found for requested code [FAKE].")); }
public void BadAddressShouldFail() { Assert.Throws <System.ArgumentException>(() => { var ec = new RestAssured() .Given() .Name("Bad Call") .When() .Get("http://www.fake-2-address.com"); try { ec.Then(); } catch (System.ArgumentException e) { Assert.IsTrue(e.ParamName.Contains("text/html")); throw e; } }); }
public void DeleteAnExistingPost() { var responseContext = new RestAssured() .Given() .Name(Name) .Header("Content-Type", "application/json") .When() .Delete("https://jsonplaceholder.typicode.com/posts/1") .Then(); responseContext.TestStatus( "Verify an existing Post resource can be deleted with HTTP DELETE by confirming the response HTTP status code", x => { Assert.AreEqual(200, x); return(x == 200); }) .Assert("Verify"); }
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 GetUser() { var endpoint = new RestAssured() .Given() .Header("Content-Type", "application/json; charset=utf-8") .Host("localhost") .Port(3000) .Uri("/users/1"); var resp = endpoint.When().Get() // .Debug() // --> Print Request Conditions .Then() .Debug() // --> Print Response Conditions .TestStatus("Should be equal 200", i => i == 200) .TestBody("id", i => i.Equals(1)); var id = resp.Retrieve(b => b.id); var user = (User)resp.Retrieve(u => JsonConvert.DeserializeObject <User>(u.ToString())); id.Should().Be(1); user.email.Should().Be("*****@*****.**"); resp.TestStatus("", s => s.Equals(200)); }
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"); }