testing restful web services with rest assured
TRANSCRIPT
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
What are we going to do?
_RESTful web services
_REST Assured
_Get your hands dirty
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
What are RESTful web services?
_HTTP request methods (GET, POST, PUT, …)
_URI’s
_CRUD operations on dataPOST CreateGET ReadPUT UpdateDELETE Delete
An example
_GET http://api.zippopotam.us/us/90210
_Result:
Usage of RESTful web services
_Mobile applications
_Internet of Things
_API Economy
Why REST (and not SOAP, for example)?_Support for a multitude of data formats
_JSON_XML_…
_Smaller overhead and therefore better performance
Why SOAP(and not REST, for example)?_WS-Security
_WS-ReliableMessaging
_WS-AtomicTransaction
_Mostly used to secure banking applications
Tools for testing RESTful web services_Browser (using plugins like Postman for Chrome)
_Open source (SoapUI, REST Assured)
_COTS (Parasoft SOAtest, SoapUI Pro)
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
Configuring REST Assured
_Download from http://rest-assured.io
_Add as a dependency to your project
_Maven
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/
A sample test
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
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()
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
GPath example
http://goessner.net/articles/JsonPath/
Validating technical response data_HTTP status code
_MIME-type of received responses
_Cookies and their value
_…
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/
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
Demo_ API documentation
_ How to use the test suite_ Executing your tests_ Reviewing test results
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
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!
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
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
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:
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
Authentication
_Securing web services
_Basic authentication (preemptive / challenged)
_OAuth(2)
_Digest / Form
Basic authentication
_Username/password sent in header for every request
OAuth(2)
_Request of authentication token based on username and password (Basic authentication)
_Include authentication token in header of all subsequent requests
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
Measuring response times
_An example:
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
Sharing variables between tests
_Example: authentication tests
_Copy / paste required for OAuth2 token
_Preferably: store and retrieve for reuse!
Sharing variables between tests
_REST Assuredsupports thiswith extract()
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
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)
Questions
?