If you can't read please download the document
Upload
micha-kops
View
47.529
Download
1
Embed Size (px)
Citation preview
Testing RESTful Webservices using the REST-assured Framework
Testing RESTful Webservices using the REST-assured Framework
Table of Contents
Prerequisites
REST-assured and Maven
Verify JSON via GET
JsonPath
Groovy Closures The JSON
Groovy Closures The Test
Verifying XML, Xpath, Schema
Request Parameters
Status Codes, Headers
Handling Basic Authentication
Setting HTTP Headers
Verifying HTTP Header
Setting Cookies
Verifying Cookies
File Uploads
Registering Custom Parsers
Specification Reuse
Tutorial Sources
Prerequisites
Were going to need a JDK and Maven .. nothing more
Java Development Kit >= 6
Maven 3
The full tutorial and the RESTful Webservice to test can be found at my blog www.hascode.com
REST-assured and Maven
Only one dependency needed:
com.jayway.restassured
rest-assured
1.4
test
Verify JSON via GET
{ "email":"[email protected]", "firstName":"Tim", "id":"1", "lastName":"Testerman"}
@Testpublic void testGetSingleUser() { expect(). statusCode(200). body( "email", equalTo("[email protected]"), "firstName", equalTo("Tim"), "lastName", equalTo("Testerman"), "id", equalTo("1")). when(). get("/service/single-user");}
JsonPath
{ "email":"[email protected]", "firstName":"Tim", "id":"1", "lastName":"Testerman"}
@Testpublic void testGetSingleUserProgrammatic() { Response res = get("/service/single-user"); assertEquals(200, res.getStatusCode()); String json = res.asString(); JsonPath jp = new JsonPath(json); assertEquals("[email protected]", jp.get("email")); assertEquals("Tim", jp.get("firstName")); assertEquals("Testerman", jp.get("lastName")); assertEquals("1", jp.get("id"));}
Groovy Closures The JSON
{"person":[{"@id":"1","email":"[email protected]","firstName":"Tim","lastName":"Testerman"},{"@id":"20","email":"[email protected]","firstName":"Sara","lastName":"Stevens"},{"@id":"11","email":"[email protected]","firstName":"Mark","lastName":"Mustache"}]}
Groovy Closures The Test
@Testpublic void testFindUsingGroovyClosure() { String json = get("/service/persons/json").asString(); JsonPath jp = new JsonPath(json); jp.setRoot("person"); Map person = jp.get("find {e -> e.email =~ /test@/}"); assertEquals("[email protected]", person.get("email")); assertEquals("Tim", person.get("firstName")); assertEquals("Testerman", person.get("lastName"));}
Verifying XML The XML
[email protected] Tim 1 Testerman
Verifying XML The Test
@Testpublic void testGetSingleUserAsXml() { expect(). statusCode(200). body( "user.email", equalTo("[email protected]"), "user.firstName", equalTo("Tim"), "user.lastName", equalTo("Testerman"), "user.id", equalTo("1")). when(). get("/service/single-user/xml");}
Verifying using Xpath The XML
[email protected] Tim Testerman [email protected] Sara Stevens [email protected] Mark Mustache
Verifying using XPath the Test
@Testpublic void testGetPersons() { expect(). statusCode(200). body( hasXPath("//person[@id='1']/email[.='[email protected]'] and firstName='Tim' and lastName='Testerman'"), hasXPath("//person[@id='20']/email[.='[email protected]'] and firstName='Sara' and lastName='Stevens'"), hasXPath("//person[@id='1']/email[.='[email protected]'] and firstName='Mark' and lastName='Mustache'")). when(). get("/service/persons/xml");}
Verify using Schema The XML
[email protected] Tim 1 Testerman
Verifying using Schema The XSD
Verifying using Schema The Test
@Testpublic void testGetSingleUserAgainstSchema() { InputStream xsd = getClass().getResourceAsStream("/user.xsd"); assertNotNull(xsd); expect(). statusCode(200). body( matchesXsd(xsd)). when(). get("/service/single-user/xml");}
Handling Request Parameters
@Testpublic void testCreateuser() { final String email = "[email protected]"; final String firstName = "Tim"; final String lastName = "Tester"; given(). parameters( "email", email, "firstName", firstName, "lastName", lastName). expect(). body("email", equalTo(email)). body("firstName", equalTo(firstName)). body("lastName", equalTo(lastName)). when(). get("/service/user/create");}
Verifying HTTP Status Codes
@Testpublic void testStatusNotFound() { expect(). statusCode(404). when(). get("/service/status/notfound");}
Handling Basic Authentication
@Testpublic void testAuthenticationWorking() { // we're not authenticated, service returns "401 Unauthorized" expect(). statusCode(401). when(). get("/service/secure/person"); // with authentication it is working expect(). statusCode(200). when(). with(). authentication().basic("admin", "admin"). get("/service/secure/person");}
Setting HTTP Headers
@Testpublic void testSetRequestHeaders() { expect(). body(equalTo("TEST")). when(). with(). header("myparam", "TEST"). get("/service/header/print"); expect(). body(equalTo("foo")). when(). with(). header("myparam", "foo"). get("/service/header/print");}
Verifying HTTP Header
@Testpublic void testReturnedHeaders() { expect(). headers("customHeader1", "foo", "anotherHeader", "bar"). when(). get("/service/header/multiple");}
Setting Cookies
@Testpublic void testAccessSecuredByCookie() { expect(). statusCode(403). when(). get("/service/access/cookie-token-secured"); given(). cookie("authtoken", "abcdef"). expect(). statusCode(200). when(). get("/service/access/cookie-token-secured");}
Verifying Cookies
@Testpublic void testModifyCookie() { expect(). cookie("userName", equalTo("Ted")). when(). with().param("name", "Ted"). get("/service/cookie/modify"); expect(). cookie("userName", equalTo("Bill")). when(). with().param("name", "Bill"). get("/service/cookie/modify");}
File Uploads
@Testpublic void testFileUpload() { final File file = new File(getClass().getClassLoader() .getResource("test.txt").getFile()); assertNotNull(file); assertTrue(file.canRead()); given(). multiPart(file). expect(). body(equalTo("This is an uploaded test file.")). when(). post("/service/file/upload");}
Registering Custom Parsers
@Testpublic void testRegisterParserForUnknownContentType() { RestAssured.registerParser("text/json", Parser.JSON); expect(). body("test", equalTo(true)). when(). get("/service/detail/json");}
Specification Reuse
@Testpublic void testSpecReuse() { ResponseSpecBuilder builder = new ResponseSpecBuilder(); builder.expectStatusCode(200); builder.expectBody("email", equalTo("[email protected]")); builder.expectBody("firstName", equalTo("Tim")); builder.expectBody("lastName", equalTo("Testerman")); builder.expectBody("id", equalTo("1")); ResponseSpecification responseSpec = builder.build(); // now we're able to use this specification for this test expect(). spec(responseSpec). when(). get("/service/single-user"); // now re-use for another test that returns similar data .. you may // extend the specification with further tests as you wish final String email = "[email protected]"; final String firstName = "Tim"; final String lastName = "Testerman"; expect(). spec(responseSpec). when(). with(). parameters( "email", email, "firstName", firstName, "lastName",lastName). get("/service/user/create");}
Tutorial Sources
On Bitbucket: http://bitbucket.org/hascode
Clone with Mercurial:
hg clone https://bitbucket.org/hascode/rest-assured-sample
The End
Thank you for your audience :)
Please feel free to read further tutorials of mine on www.hascode.com
I'm also on Twitter: http://twitter.com/hascode
Muokkaa otsikon tekstimuotoa napsauttamalla
Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso
Micha Kops / www.hascode.com