testing restful web services with rest assured
TRANSCRIPT
![Page 1: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/1.jpg)
Test the RESTTesting RESTful web services using REST Assured
An open source workshop by …
Originally created by Bas Dijkstra – [email protected] – http://www.testautomation.com - @_basdijkstra
![Page 2: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/2.jpg)
What are we going to do?
_RESTful web services
_REST Assured
_Get your hands dirty
![Page 3: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/3.jpg)
Preparation
_Install Eclipse (or any other IDE)
_Install TestNG plugin (for Eclipse)
_Install m2e (or similar for any other IDE)
_Import Maven project into IDE
_Update project (Eclipse) or similar
![Page 4: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/4.jpg)
What are RESTful web services?
_HTTP request methods (GET, POST, PUT, …)
_URI’s
_CRUD operations on dataPOST CreateGET ReadPUT UpdateDELETE Delete
![Page 5: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/5.jpg)
An example
_GET http://api.zippopotam.us/us/90210
_Result:
![Page 6: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/6.jpg)
Usage of RESTful web services
_Mobile applications
_Internet of Things
_API Economy
![Page 7: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/7.jpg)
Why REST (and not SOAP, for example)?_Support for a multitude of data formats
_JSON_XML_…
_Smaller overhead and therefore better performance
![Page 8: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/8.jpg)
Why SOAP(and not REST, for example)?_WS-Security
_WS-ReliableMessaging
_WS-AtomicTransaction
_Mostly used to secure banking applications
![Page 9: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/9.jpg)
Tools for testing RESTful web services_Browser (using plugins like Postman for Chrome)
_Open source (SoapUI, REST Assured)
_COTS (Parasoft SOAtest, SoapUI Pro)
![Page 10: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/10.jpg)
REST Assured
_Java library for writing tests for RESTful web services
_Removes a lot of boilerplate code
_Integrates seamlessly with existing Java-based testing frameworks_JUnit, TestNG_Selenium WebDriver
![Page 11: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/11.jpg)
Configuring REST Assured
_Download from http://rest-assured.io
_Add as a dependency to your project
_Maven
![Page 12: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/12.jpg)
REST Assured documentation
_Usage guide_https://github.com/rest-assured/rest-assured/wiki/Usage
_Links to other documentation (JavaDoc, getting started, release notes)_http://rest-assured.io/
![Page 13: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/13.jpg)
A sample test
![Page 14: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/14.jpg)
REST Assured features
_Support for HTTP methods (GET, POST, PUT, …)_Support for BDD / Gherkin (Given/When/Then)_Use of Hamcrest matchers for checks (equalTo)_Use of GPath for selecting elements from JSON response
![Page 15: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/15.jpg)
About Hamcrest matchers
_Express expectations in natural language
_Examples:
_ http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html
equalTo(X) Does the object equal X?
hasItem(“Rome”) Does the collection contain an item “Rome”?
hasSize(3) Does the size of the collection equal 3?
not(equalTo(X)) Inverts matcher equalTo()
![Page 16: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/16.jpg)
About GPath
_GPath is a path expression language integrated into Groovy _REST Assured is built in Groovy
_Similar aims and scope as XPath for XML
_Documentation and examples:_http://groovy-lang.org/processing-xml.html#_gpath_http://groovy.jmiguel.eu/groovy.codehaus.org/GPath.html
![Page 17: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/17.jpg)
GPath example
http://goessner.net/articles/JsonPath/
![Page 18: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/18.jpg)
Validating technical response data_HTTP status code
_MIME-type of received responses
_Cookies and their value
_…
![Page 19: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/19.jpg)
Our application under test
_Ergast F1 API
_Presents historical data of Formula 1 races, drivers, circuits, etc.
_Data can be returned in JSON and XML format
_API documentation at http://ergast.com/mrd/
![Page 20: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/20.jpg)
Some examples
_Data for driver Max Verstappen (in JSON):
http://ergast.com/api/f1/drivers/max_verstappen.json
_A list of circuits for the 2015 season (in JSON):
http://ergast.com/api/f1/2015/circuits.json
![Page 21: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/21.jpg)
Demo_ API documentation
_ How to use the test suite_ Executing your tests_ Reviewing test results
![Page 22: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/22.jpg)
Get your hands dirty!
_RestAssuredExercises1
_Simple checks_Validating individual elements_Validating collections and items therein_Validating technical response properties
_RestAssuredExamples contains all examples from the presentation
![Page 23: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/23.jpg)
Parameters in RESTful web services_Path parameters
_http://ergast.com/api/f1/drivers/max_verstappen.json_http://ergast.com/api/f1/drivers/hamilton.json
_Query string parameters_http://md5.jsontest.com/?text=testcaseOne_http://md5.jsontest.com/?text=testcaseTwo
_There is no official standard!
![Page 24: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/24.jpg)
Using parameters in REST Assured
_Examples for query parameters:_ Call to http://md5.jsontest.com/?text=testcaseOne
_ Call to http://api.openweathermap.org/data/2.5/weather/?q=Kopenhagen&mode=xml
![Page 25: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/25.jpg)
Using parameters in REST Assured
_Examples for path parameters:_ Call to http://ergast.com/api/f1/drivers/max_verstappen.json
_ Call to http://ergast.com/api/f1/drivers/alonso/constructors/renault/seasons.json
![Page 26: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/26.jpg)
Using parameters in REST Assured
_Iterating over a collection of parameter values:
_ Creating pairs of driver ID’s and numbers:
_ Using test data in API calls and validation of response data:
![Page 27: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/27.jpg)
Get your hands dirty!
_RestAssuredExercises2
_Data driven tests_Creating a test data object_Using test data in calling the right URI_Using test data in assertions
_RestAssuredExamples contains all examples from the presentation
![Page 28: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/28.jpg)
Authentication
_Securing web services
_Basic authentication (preemptive / challenged)
_OAuth(2)
_Digest / Form
![Page 29: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/29.jpg)
Basic authentication
_Username/password sent in header for every request
![Page 30: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/30.jpg)
OAuth(2)
_Request of authentication token based on username and password (Basic authentication)
_Include authentication token in header of all subsequent requests
![Page 31: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/31.jpg)
Measuring response times
_Measuring response times for individual requests
_Setting response time thresholds_Test fails when threshold is exceeded
_No full-fledged performance test
_First rough indication of API performance
![Page 32: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/32.jpg)
Measuring response times
_An example:
![Page 33: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/33.jpg)
Get your hands dirty!_RestAssuredExercises3
_Communicating with an OAuth2-secured API_ Requesting authentication token_ Using authentication token in subsequent requests
_Measuring API response times_ Execute a specific API call_ Evaluate response time against predefined threshold
_RestAssuredExamples contains all examples from the presentation
![Page 34: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/34.jpg)
Sharing variables between tests
_Example: authentication tests
_Copy / paste required for OAuth2 token
_Preferably: store and retrieve for reuse!
![Page 35: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/35.jpg)
Sharing variables between tests
_REST Assuredsupports thiswith extract()
![Page 36: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/36.jpg)
Get your hands dirty!
_RestAssuredExercises4
_Try it for yourself
_Can you apply this to the Formula 1 API?
_RestAssuredExamplesParameterPassing contains all examples from the presentation
![Page 37: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/37.jpg)
Executing tests in CI
_REST Assured-tests are no different from other Java (unit) tests
_Can be easily added to your CI/CD pipeline
_Part of the build process
_Demonstration (using Jenkins)
![Page 38: Testing RESTful web services with REST Assured](https://reader031.vdocuments.us/reader031/viewer/2022011721/587b5c481a28ab38258b4bb7/html5/thumbnails/38.jpg)
Questions
?