Testing REST services Verificao e Validao de Software Departamento - - PowerPoint PPT Presentation

testing rest services
SMART_READER_LITE
LIVE PREVIEW

Testing REST services Verificao e Validao de Software Departamento - - PowerPoint PPT Presentation

Testing REST services Verificao e Validao de Software Departamento de Informtica Faculdade de Cincias da Universidade de Lisboa Vasco T. Vasconcelos Some tools Web browser: allows only for GET operations For the remaining


slide-1
SLIDE 1

Testing REST services

Verificação e Validação de Software Departamento de Informática Faculdade de Ciências da Universidade de Lisboa

Vasco T. Vasconcelos

slide-2
SLIDE 2

Some tools

  • Web browser: allows only for GET operations
  • For the remaining verbs use, for example,
  • curl from the command line
  • a dedicated REST client (preferably one that

allows to save common requests)

slide-3
SLIDE 3

Specifying REST APIs

  • Open API (was swagger), http://swagger.io
  • Includes an editor and SDK generators, server and clients,

for multiple platforms

  • Apiary, https://apiary.io
  • Provides for API mocking
  • Clients may use an API before it is implemented (no support

for persistence)

  • Proprietary language (API blueprint), but may use Swagger

specs

slide-4
SLIDE 4

Swagger editor

slide-5
SLIDE 5

REST-assured

  • https://github.com/jayway/rest-assured
  • Testing REST APIs in Java
  • Fluent interface:
  • In software engineering, a fluent interface (as first

coined by Eric Evans and Martin Fowler) is an implementation of an object oriented API that aims to provide more readable code (https:// en.wikipedia.org/wiki/Fluent_interface)

slide-6
SLIDE 6

import static com.jayway.restassured.RestAssured.get; @Test public void testWelcomeStatusCode() { get(“http://localhost:8080/v1/lotto"). then(). statusCode(200); }

Getting started

Verb Path In the Response header Success (better: use a constant)

Note the indentation

slide-7
SLIDE 7

Better

import static com.jayway.restassured.RestAssured.baseURI; @BeforeClass public static void testSetup() { baseURI = "http://localhost:8080/lotto"; // Setup the database } @Test public void testWelcomeStatusCode() { get(). then(). statusCode(200); }

Implicit path

slide-8
SLIDE 8

Analysing responses

  • Assume that GET returns the following JSON

{ "lotto":{ "lottoId":5, "winning-numbers":[2,45,34,23,7,5,3], "winners":[{ "winnerId":23, "numbers":[2,45,34,23,3,5] },{ "winnerId":54, "numbers":[52,3,12,11,18,22] }] } }

slide-9
SLIDE 9

Paths in JSON

import static org.hamcrest.Matchers.equalTo; @Test public void testLottoIdIs5() { get().then().body("lotto.lottoId", equalTo(5));; }

An hamcrest Matcher A path in JSON

slide-10
SLIDE 10

More paths

import static org.hamcrest.Matchers.hasItems; @Test public void testLottoHasItems() { get(). then(). body(“lotto.winners.winnerId", hasItems(23, 54)); }

Another hamcrest Matcher

slide-11
SLIDE 11

Java Hamcrest

  • “Matchers that can be combined to create flexible

expressions of intent”

  • Check the various matchers available at http://

hamcrest.org/JavaHamcrest/javadoc/1.3/org/ hamcrest/Matchers.html

slide-12
SLIDE 12

Preparing JSON with a Map

@Test public void testPOSTPet() { Map<String, Object> pet = new HashMap<>(); pet.put("id", 32); pet.put("name", "Felix"); pet.put("status", “alive"); given(). contentType("application/json"). body(pet). when(). post(). then(). statusCode(201); }

Created JSON with 3 properties Specify the content type

slide-13
SLIDE 13

Preparing JSON with a Builder

import javax.json.Json; @Test public void testPOSTPetBuilder() { String pet = Json.createObjectBuilder(). add("id", 32). add("name", "Felix"). add("status", "alive"). build().toString(); given(). contentType("application/json"). body(pet). when(). post(). then(). statusCode(201); }

slide-14
SLIDE 14

Saving responses for later use

  • Suppose that a POST returns the following header
  • and that one whats to recall the location for further

use

HTTP 201 Created Location: http://localhost:8080/v1/258 Content-Type: text/html Date: Mon, 16 May 2016 11:09:28 GMT Content-Length: 74 X-Powered-By: Undertow/1 Connection: keep-alive Server: WildFly/10

slide-15
SLIDE 15

Saving responses

@Test public void testGet() { String pet = Json.createObjectBuilder()…; String location = given(). contentType("application/json"). body(pet). when(). post(). then(). statusCode(201). extract().header("Location"); get(location).then().statusCode(200); }

Created Extract the string under property Location Now check that the pet was properly POSTed Extract from the header