Your SlideShare is downloading. ×
0
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Testing RESTful Webservices using the REST-assured framework

39,393

Published on

The REST-assured framework and its features explained by example. …

The REST-assured framework and its features explained by example.

For detailed information please take a look at my full tutorial including the sources at http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/

Published in: Technology
1 Comment
5 Likes
Statistics
Notes
  • Excellent intro to REST-Assured!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
39,393
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
169
Comments
1
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Testing RESTful Webservices using the REST-assured Framework
  • 2. Table of Contents <ul><li>Prerequisites
  • 3. REST-assured and Maven
  • 4. Verify JSON via GET
  • 5. JsonPath
  • 6. Groovy Closures – The JSON
  • 7. Groovy Closures – The Test
  • 8. Verifying XML, Xpath, Schema
  • 9. Request Parameters
  • 10. Status Codes, Headers </li></ul><ul><li>Handling Basic Authentication
  • 11. Setting HTTP Headers
  • 12. Verifying HTTP Header
  • 13. Setting Cookies
  • 14. Verifying Cookies
  • 15. File Uploads
  • 16. Registering Custom Parsers
  • 17. Specification Reuse
  • 18. Tutorial Sources </li></ul>
  • 19. Prerequisites <ul><li>We’re going to need a JDK and Maven .. nothing more …
  • 20. Java Development Kit &gt;= 6
  • 21. Maven 3
  • 22. The full tutorial and the RESTful Webservice to test can be found at my blog www.hascode.com </li></ul>
  • 23. REST-assured and Maven <ul><li>Only one dependency needed:
  • 24. &lt;dependency&gt;
  • 25. &lt;groupId&gt;com.jayway.restassured&lt;/groupId&gt;
  • 26. &lt;artifactId&gt;rest-assured&lt;/artifactId&gt;
  • 27. &lt;version&gt;1.4&lt;/version&gt;
  • 28. &lt;scope&gt;test&lt;/scope&gt;
  • 29. &lt;/dependency&gt; </li></ul>
  • 30. Verify JSON via GET { &amp;quot;email&amp;quot;:&amp;quot;test@hascode.com&amp;quot;, &amp;quot;firstName&amp;quot;:&amp;quot;Tim&amp;quot;, &amp;quot;id&amp;quot;:&amp;quot;1&amp;quot;, &amp;quot;lastName&amp;quot;:&amp;quot;Testerman&amp;quot; } @Test public void testGetSingleUser() { expect(). statusCode(200). body( &amp;quot;email&amp;quot;, equalTo(&amp;quot;test@hascode.com&amp;quot;), &amp;quot;firstName&amp;quot;, equalTo(&amp;quot;Tim&amp;quot;), &amp;quot;lastName&amp;quot;, equalTo(&amp;quot;Testerman&amp;quot;), &amp;quot;id&amp;quot;, equalTo(&amp;quot;1&amp;quot;)). when(). get(&amp;quot;/service/single-user&amp;quot;); }
  • 31. JsonPath { &amp;quot;email&amp;quot;:&amp;quot;test@hascode.com&amp;quot;, &amp;quot;firstName&amp;quot;:&amp;quot;Tim&amp;quot;, &amp;quot;id&amp;quot;:&amp;quot;1&amp;quot;, &amp;quot;lastName&amp;quot;:&amp;quot;Testerman&amp;quot; } @Test public void testGetSingleUserProgrammatic() { Response res = get(&amp;quot;/service/single-user&amp;quot;); assertEquals(200, res.getStatusCode()); String json = res.asString(); JsonPath jp = new JsonPath(json); assertEquals(&amp;quot;test@hascode.com&amp;quot;, jp.get(&amp;quot;email&amp;quot;)); assertEquals(&amp;quot;Tim&amp;quot;, jp.get(&amp;quot;firstName&amp;quot;)); assertEquals(&amp;quot;Testerman&amp;quot;, jp.get(&amp;quot;lastName&amp;quot;)); assertEquals(&amp;quot;1&amp;quot;, jp.get(&amp;quot;id&amp;quot;)); }
  • 32. Groovy Closures – The JSON { &amp;quot;person&amp;quot;:[ { &amp;quot;@id&amp;quot;:&amp;quot;1&amp;quot;, &amp;quot;email&amp;quot;:&amp;quot;test@hascode.com&amp;quot;, &amp;quot;firstName&amp;quot;:&amp;quot;Tim&amp;quot;, &amp;quot;lastName&amp;quot;:&amp;quot;Testerman&amp;quot; },{ &amp;quot;@id&amp;quot;:&amp;quot;20&amp;quot;, &amp;quot;email&amp;quot;:&amp;quot;dev@hascode.com&amp;quot;, &amp;quot;firstName&amp;quot;:&amp;quot;Sara&amp;quot;, &amp;quot;lastName&amp;quot;:&amp;quot;Stevens&amp;quot; },{ &amp;quot;@id&amp;quot;:&amp;quot;11&amp;quot;, &amp;quot;email&amp;quot;:&amp;quot;devnull@hascode.com&amp;quot;, &amp;quot;firstName&amp;quot;:&amp;quot;Mark&amp;quot;, &amp;quot;lastName&amp;quot;:&amp;quot;Mustache&amp;quot; } ] }
  • 33. Groovy Closures – The Test @Test public void testFindUsingGroovyClosure() { String json = get(&amp;quot;/service/persons/json&amp;quot;).asString(); JsonPath jp = new JsonPath(json); jp.setRoot(&amp;quot;person&amp;quot;); Map person = jp.get(&amp;quot;find {e -&gt; e.email =~ /test@/}&amp;quot;); assertEquals(&amp;quot;test@hascode.com&amp;quot;, person.get(&amp;quot;email&amp;quot;)); assertEquals(&amp;quot;Tim&amp;quot;, person.get(&amp;quot;firstName&amp;quot;)); assertEquals(&amp;quot;Testerman&amp;quot;, person.get(&amp;quot;lastName&amp;quot;)); }
  • 34. Verifying XML – The XML &lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;yes&amp;quot;?&gt; &lt;user&gt; &lt;email&gt;test@hascode.com&lt;/email&gt; &lt;firstName&gt;Tim&lt;/firstName&gt; &lt;id&gt;1&lt;/id&gt; &lt;lastName&gt;Testerman&lt;/lastName&gt; &lt;/user&gt;
  • 35. Verifying XML – The Test @Test public void testGetSingleUserAsXml() { expect(). statusCode(200). body( &amp;quot;user.email&amp;quot;, equalTo(&amp;quot;test@hascode.com&amp;quot;), &amp;quot;user.firstName&amp;quot;, equalTo(&amp;quot;Tim&amp;quot;), &amp;quot;user.lastName&amp;quot;, equalTo(&amp;quot;Testerman&amp;quot;), &amp;quot;user.id&amp;quot;, equalTo(&amp;quot;1&amp;quot;)). when(). get(&amp;quot;/service/single-user/xml&amp;quot;); }
  • 36. Verifying using Xpath – The XML &lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;yes&amp;quot;?&gt; &lt;people&gt; &lt;person id=&amp;quot;1&amp;quot;&gt; &lt;email&gt;test@hascode.com&lt;/email&gt; &lt;firstName&gt;Tim&lt;/firstName&gt; &lt;lastName&gt;Testerman&lt;/lastName&gt; &lt;/person&gt; &lt;person id=&amp;quot;20&amp;quot;&gt; &lt;email&gt;dev@hascode.com&lt;/email&gt; &lt;firstName&gt;Sara&lt;/firstName&gt; &lt;lastName&gt;Stevens&lt;/lastName&gt; &lt;/person&gt; &lt;person id=&amp;quot;11&amp;quot;&gt; &lt;email&gt;devnull@hascode.com&lt;/email&gt; &lt;firstName&gt;Mark&lt;/firstName&gt; &lt;lastName&gt;Mustache&lt;/lastName&gt; &lt;/person&gt; &lt;/people&gt;
  • 37. Verifying using XPath – the Test @Test public void testGetPersons() { expect(). statusCode(200). body( hasXPath(&amp;quot;//person[@id=&apos;1&apos;]/email[.=&apos;test@hascode.com&apos;] and firstName=&apos;Tim&apos; and lastName=&apos;Testerman&apos;&amp;quot;), hasXPath(&amp;quot;//person[@id=&apos;20&apos;]/email[.=&apos;dev@hascode.com&apos;] and firstName=&apos;Sara&apos; and lastName=&apos;Stevens&apos;&amp;quot;), hasXPath(&amp;quot;//person[@id=&apos;1&apos;]/email[.=&apos;devnull@hascode.com&apos;] and firstName=&apos;Mark&apos; and lastName=&apos;Mustache&apos;&amp;quot;)). when(). get(&amp;quot;/service/persons/xml&amp;quot;); }
  • 38. Verify using Schema – The XML &lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;yes&amp;quot;?&gt; &lt;user&gt; &lt;email&gt;test@hascode.com&lt;/email&gt; &lt;firstName&gt;Tim&lt;/firstName&gt; &lt;id&gt;1&lt;/id&gt; &lt;lastName&gt;Testerman&lt;/lastName&gt; &lt;/user&gt;
  • 39. Verifying using Schema – The XSD &lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; &lt;schema xmlns=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&gt; &lt;element name=&amp;quot;user&amp;quot;&gt; &lt;complexType&gt; &lt;sequence&gt; &lt;element name=&amp;quot;email&amp;quot;&gt; &lt;simpleType&gt; &lt;restriction base=&amp;quot;string&amp;quot;&gt; &lt;pattern value=&amp;quot;.+@.+&amp;quot;&gt;&lt;/pattern&gt; &lt;/restriction&gt; &lt;/simpleType&gt; &lt;/element&gt; &lt;element name=&amp;quot;firstName&amp;quot; type=&amp;quot;string&amp;quot;&gt;&lt;/element&gt; &lt;element name=&amp;quot;id&amp;quot; type=&amp;quot;int&amp;quot;&gt;&lt;/element&gt; &lt;element name=&amp;quot;lastName&amp;quot; type=&amp;quot;string&amp;quot;&gt;&lt;/element&gt; &lt;/sequence&gt; &lt;/complexType&gt; &lt;/element&gt; &lt;/schema&gt;
  • 40. Verifying using Schema – The Test @Test public void testGetSingleUserAgainstSchema() { InputStream xsd = getClass().getResourceAsStream(&amp;quot;/user.xsd&amp;quot;); assertNotNull(xsd); expect(). statusCode(200). body( matchesXsd(xsd)). when(). get(&amp;quot;/service/single-user/xml&amp;quot;); }
  • 41. Handling Request Parameters @Test public void testCreateuser() { final String email = &amp;quot;test@hascode.com&amp;quot;; final String firstName = &amp;quot;Tim&amp;quot;; final String lastName = &amp;quot;Tester&amp;quot;; given(). parameters( &amp;quot;email&amp;quot;, email, &amp;quot;firstName&amp;quot;, firstName, &amp;quot;lastName&amp;quot;, lastName). expect(). body(&amp;quot;email&amp;quot;, equalTo(email)). body(&amp;quot;firstName&amp;quot;, equalTo(firstName)). body(&amp;quot;lastName&amp;quot;, equalTo(lastName)). when(). get(&amp;quot;/service/user/create&amp;quot;); }
  • 42. Verifying HTTP Status Codes @Test public void testStatusNotFound() { expect(). statusCode(404). when(). get(&amp;quot;/service/status/notfound&amp;quot;); }
  • 43. Handling Basic Authentication @Test public void testAuthenticationWorking() { // we&apos;re not authenticated, service returns &amp;quot;401 Unauthorized&amp;quot; expect(). statusCode(401). when(). get(&amp;quot;/service/secure/person&amp;quot;); // with authentication it is working expect(). statusCode(200). when(). with(). authentication().basic(&amp;quot;admin&amp;quot;, &amp;quot;admin&amp;quot;). get(&amp;quot;/service/secure/person&amp;quot;); }
  • 44. Setting HTTP Headers @Test public void testSetRequestHeaders() { expect(). body(equalTo(&amp;quot;TEST&amp;quot;)). when(). with(). header(&amp;quot;myparam&amp;quot;, &amp;quot;TEST&amp;quot;). get(&amp;quot;/service/header/print&amp;quot;); expect(). body(equalTo(&amp;quot;foo&amp;quot;)). when(). with(). header(&amp;quot;myparam&amp;quot;, &amp;quot;foo&amp;quot;). get(&amp;quot;/service/header/print&amp;quot;); }
  • 45. Verifying HTTP Header @Test public void testReturnedHeaders() { expect(). headers(&amp;quot;customHeader1&amp;quot;, &amp;quot;foo&amp;quot;, &amp;quot;anotherHeader&amp;quot;, &amp;quot;bar&amp;quot;). when(). get(&amp;quot;/service/header/multiple&amp;quot;); }
  • 46. Setting Cookies @Test public void testAccessSecuredByCookie() { expect(). statusCode(403). when(). get(&amp;quot;/service/access/cookie-token-secured&amp;quot;); given(). cookie(&amp;quot;authtoken&amp;quot;, &amp;quot;abcdef&amp;quot;). expect(). statusCode(200). when(). get(&amp;quot;/service/access/cookie-token-secured&amp;quot;); }
  • 47. Verifying Cookies @Test public void testModifyCookie() { expect(). cookie(&amp;quot;userName&amp;quot;, equalTo(&amp;quot;Ted&amp;quot;)). when(). with().param(&amp;quot;name&amp;quot;, &amp;quot;Ted&amp;quot;). get(&amp;quot;/service/cookie/modify&amp;quot;); expect(). cookie(&amp;quot;userName&amp;quot;, equalTo(&amp;quot;Bill&amp;quot;)). when(). with().param(&amp;quot;name&amp;quot;, &amp;quot;Bill&amp;quot;). get(&amp;quot;/service/cookie/modify&amp;quot;); }
  • 48. File Uploads @Test public void testFileUpload() { final File file = new File(getClass().getClassLoader() .getResource(&amp;quot;test.txt&amp;quot;).getFile()); assertNotNull(file); assertTrue(file.canRead()); given(). multiPart(file). expect(). body(equalTo(&amp;quot;This is an uploaded test file.&amp;quot;)). when(). post(&amp;quot;/service/file/upload&amp;quot;); }
  • 49. Registering Custom Parsers @Test public void testRegisterParserForUnknownContentType() { RestAssured.registerParser(&amp;quot;text/json&amp;quot;, Parser.JSON); expect(). body(&amp;quot;test&amp;quot;, equalTo(true)). when(). get(&amp;quot;/service/detail/json&amp;quot;); }
  • 50. Specification Reuse @Test public void testSpecReuse() { ResponseSpecBuilder builder = new ResponseSpecBuilder(); builder.expectStatusCode(200); builder.expectBody(&amp;quot;email&amp;quot;, equalTo(&amp;quot;test@hascode.com&amp;quot;)); builder.expectBody(&amp;quot;firstName&amp;quot;, equalTo(&amp;quot;Tim&amp;quot;)); builder.expectBody(&amp;quot;lastName&amp;quot;, equalTo(&amp;quot;Testerman&amp;quot;)); builder.expectBody(&amp;quot;id&amp;quot;, equalTo(&amp;quot;1&amp;quot;)); ResponseSpecification responseSpec = builder.build(); // now we&apos;re able to use this specification for this test expect(). spec(responseSpec). when(). get(&amp;quot;/service/single-user&amp;quot;); // now re-use for another test that returns similar data .. you may // extend the specification with further tests as you wish final String email = &amp;quot;test@hascode.com&amp;quot;; final String firstName = &amp;quot;Tim&amp;quot;; final String lastName = &amp;quot;Testerman&amp;quot;; expect(). spec(responseSpec). when(). with(). parameters( &amp;quot;email&amp;quot;, email, &amp;quot;firstName&amp;quot;, firstName, &amp;quot;lastName&amp;quot;,lastName). get(&amp;quot;/service/user/create&amp;quot;); }
  • 51. Tutorial Sources <ul><li>On Bitbucket: http://bitbucket.org/hascode
  • 52. Clone with Mercurial: hg clone https://bitbucket.org/hascode/rest-assured-sample </li></ul>
  • 53. The End <ul><li>Thank you for your audience :)
  • 54. Please feel free to read further tutorials of mine on www.hascode.com
  • 55. I&apos;m also on Twitter: http://twitter.com/hascode </li></ul>

×