Monday, April 11, 2016

Using REST Assured


https://github.com/rest-assured/rest-assured/wiki/Usage
https://github.com/rest-assured/rest-assured/blob/master/examples/rest-assured-itest-java/src/test/java/io/restassured/itest/java/AuthenticationITest.java
given().auth().basic("jetty", "jetty").expect().statusCode(200).when().get("/secured/hello");
RestAssured.reset();

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>3.0.2</version>
</dependency>
http://stackoverflow.com/questions/15531767/rest-assured-generic-list-deserialization
UPDATE: Using Rest-Assured 1.8.1, looks like cast to List is not supported anymore. You need to declare and object array like this:
Person[] persons = given().when().get("person/").as(Person[].class);

You could also do this if you were interested in using "expect()"
expect().
 body("get(0).firstName", equalTo("Mike")).
when().
 get("person/");
List<Artwork> returnedArtworks = Arrays.asList(response.getBody().as(Artwork[].class));
http://blog.philipphauer.de/testing-restful-services-java-best-practices

Use Reusable RequestSpecifications

Create a RequestSpecification to reuse request configurations (base URL, parameters, content type, debugging logging) that you want to use for all requests.
private static RequestSpecification spec;
@BeforeClass
public static void initSpec(){
    spec = new RequestSpecBuilder()
            .setContentType(ContentType.JSON)
            .setBaseUri("http://localhost:8080/")
            .addFilter(new ResponseLoggingFilter())//log request and response for better debugging. You can also only log if a requests fails.
            .addFilter(new RequestLoggingFilter())
            .build();
}

//nice reusable method
private String createResource(String path, Object bodyPayload) {
    return given()
            .spec(spec)
            .body(bodyPayload)
            .when()
            .post(path)
            .then()
            .statusCode(201)
            .extract().header("location");
}
//nice reusable method
private <T> T getResource(String locationHeader, Class<T> responseClass) {
    return given()
                .spec(spec)
                .when()
                .get(locationHeader)
                .then()
                .statusCode(200)
                .extract().as(responseClass);

}
https://groups.google.com/forum/#!topic/rest-assured/E2f8K2gYAck
static final Matcher<Integer> SUCCESS = isOneOf(201, 202, 204);
I usually just do statusCode(and(greaterThanOrEqualTo(200), lessThan(300)).
http://stackoverflow.com/questions/27236917/rest-assured-sprint-security-login
You don't need to go to the login page to login, REST Assured does this automatically. This works:
RestAssured.baseURI = "http://myapp-breakid.rhcloud.com";
RestAssured.authentication = form("admin", "sababa1.", springSecurity());

get("/index").prettyPrint();
What happens here is that form authentication is automatically performed before each request. If it's important to use the same session in multiple requests you should use a session filter. For example:
RestAssured.baseURI = "http://myapp-breakid.rhcloud.com";

SessionFilter sessionFilter = new SessionFilter();

given().
        auth().form("admin", "sababa1.", springSecurity()).
        filter(sessionFilter).
when().
        get("/index");

// Session filter will automatically capture the session id and you can reuse it in subsequent requests.

given().
        filter(sessionFilter).
when().
        get("/another_protected_resource").
then().
        statusCode(200);
You can also get the sessionId from the SessionFilter and use it like this:
given().
        sessionId(sessionFilter.getSessionId()).
when().
        get("/another_protected_resource").
then().
        statusCode(200);

RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();

import static com.jayway.restassured.RestAssured.given;
http://allegro.tech/2014/11/testing-restful-service-and-clients.html
@Before
public void setUp() {
    // set default port for REST-assured
    RestAssured.port = 80;

    // set default URI for REST-assured.
    // In integration tests, this would most likely point to localhost.
    RestAssured.baseURI = "http://api.openweathermap.org";
}
http://james-willett.com/2015/06/extracting-a-json-response-with-rest-assured/
https://angeltechltd.wordpress.com/2014/10/19/rest-assured-validating-json/

https://github.com/jayway/rest-assured/wiki/Usage
given().
        config(newConfig().jsonConfig(jsonConfig().numberReturnType(BIG_DECIMAL))).
when().
        get("/price").
then().
        body("price", is(new BigDecimal(12.12));

http://testdetective.com/rest-assured-going-deeper/
Sending request’s body as string is easy and straightforward, but it can be inconvenient in the case of more complex operations on request / response properties. Proven solution for this is a good-known serialization of request/response body to objects. REST-Assured supports object mapping to (and from) JSON and XML. For JSON you need either to have Jackson or Gson in your classpath and for XML you need JAXB

  JSON Schema Validation Main idea behind JSON Schema Validation is to compare response correctness with declared schema
Second, declare schema definition in json file. Below you can see simple example for checking presence of response properties and types:
given().
    contentType("application/json").
when().
    get("/user/" + userId).
then().
    body(matchesJsonSchemaInClasspath("test-schema.json"));
After calling /user/{id} method, REST-Assured invokes json schema’s matchesJsonSchemaInClasspath() method, witch compares response against schema definition previously declared in project.

when()
  .get("/products")
.then()
  .body("music.albums.findAll { it.price > 18 }.album", hasItems("Sabbath Bloody Sabbath"));  
http://testdetective.com/rest-assured-framework-overview/
One of my favourite is log method. Below is an example of using built-in logger in rest-assured method.
when().get("/ping").
then().
    log().all().
    body(containsString("pong!"));

Another important feature is HTTPS Validation. If you have some HTTPS issues, and your test flow doesn’t check any security cases, you can simply call:
RestAssured.useRelaxedHTTPSValidation();
One of the most obvious use case would be extracting values.
Response response =
    given().log().all().
        contentType("application/json").
    when().
        get("/user/" + userId).
    then().
        statusCode(200).
    extract().response();  

JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(DRAFTV4).freeze()).freeze();
// When
get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json").using(jsonSchemaFactory));

Anonymous JSON root validation
A JSON document doesn't necessarily need a named root attribute. This is for example valid JSON:
[1, 2, 3]
An anonymous JSON root can be verified by using $ or an empty string as path

when().
     get("/json").
then().
     body("$", hasItems(1, 2, 3)); // An empty string "" would work as well
with().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().body("greeting.firstName", equalTo("John"), "greeting.lastName", equalTo("Doe"));

Getting Response Data
InputStream stream = get("/lotto").asInputStream(); // Don't forget to close this one when you're done
byte[] byteArray = get("/lotto").asByteArray();
String json = get("/lotto").asString();

String nextTitleLink =
given().
        param("param_name", "param_value").
when().
        get("/title").
then().
        contentType(JSON).
        body("title", equalTo("My Title")).
extract().
        path("_links.next.href");
get(nextTitleLink). ..

Response response =
given().
        param("param_name", "param_value").
when().
        get("/title").
then().
        contentType(JSON).
        body("title", equalTo("My Title")).
extract().
        response();
String nextTitleLink = response.path("_links.next.href");
String headerValue = response.header("headerName");

Once we have the response body we can then use the JsonPath to get data from the response body:

int lottoId = from(json).getInt("lotto.lottoId");
List<Integer> winnerIds = from(json).get("lotto.winners.winnerId");
Or a bit more efficiently:

JsonPath jsonPath = new JsonPath(json).setRoot("lotto");
int lottoId = jsonPath.getInt("lottoId");
List<Integer> winnerIds = jsonPath.get("winners.winnderId");


Multi-value parameter
given().param("myList", "value1", "value2"). ..
or using a list:

List<String> values = new ArrayList<String>();
values.add("value1");
values.add("value2");

given().param("myList", values). ..
Path parameters

You can also specify so called path parameters in your request, e.g.

post("/reserve/{hotelId}/{roomNumber}", "My Hotel", 23);
These kinds of path parameters are referred to "unnamed path parameters" in REST Assured since they are index based (hotelId will be equal to "My Hotel" since it's the first placeholder).

You can also use named path parameters:

given().
        pathParam("hotelId", "My Hotel").
        pathParam("roomNumber", 23).
when().
        post("/reserve/{hotelId}/{roomNumber}").

given().
        pathParam("hotelId", "My Hotel").      
when().
        post("/reserve/{hotelId}/{roomNumber}", 23).

Use the response to verify other parts of the response
{ "userId" : "some-id", "href" : "http://localhost:8080/some-id" }
get("/x").then().body("href", response -> equalTo("http://localhost:8080/" + response.path("userId"));
get("/x").then().body("href", endsWithPath("userId"));

get("/x").then().body("href", and(startsWith("http:/localhost:8080/"), endsWithPath("userId")));

Measuring Response Time
when().
      get("/lotto").
then().
      time(lessThan(2000L)); // Milliseconds
Authentication
given().auth().basic("username", "password"). ..
but you can also define authentication for all requests:

RestAssured.authentication = basic("username", "password");

Preemptive Basic Authentication

This will send the basic authentication credential even before the server gives an unauthorized response in certain situations, thus reducing the overhead of making an additional connection. This is typically what you want to use in most situations unless you're testing the servers ability to challenge. Example:

given().auth().preemptive().basic("username", "password").when().get("/secured/hello").then().statusCode(200);

Form Authentication
given().
        auth().form("John", "Doe", FormAuthConfig.springSecurity()).
when().
        get("/formAuth");
then().
        statusCode(200);

given().
        auth().form("John", "Doe", new FormAuthConfig("/j_spring_security_check", "j_username", "j_password")).
when().
        get("/formAuth");
then().
        statusCode(200);      
CSRF
Today it's common for the server to supply a CSRF token with the response in order to avoid these kinds of attacks. REST Assured has support for automatically parsing and supplying the CSRF token to the server. In order for this to work REST Assured must make an additional request and parse (parts) of the website.

You can enable CSRF support by doing the following:

given().
        auth().form("John", "Doe", formAuthConfig().withAutoDetectionOfCsrf()).
when().
        get("/formAuth");
then().
        statusCode(200);
Now REST Assured will automatically try to detect if the webpage contains a CSRF token. In order to assist REST Assured and make the parsing more robust it's possible to supply the CSRF field name (here we imagine that we're using Spring Security default values and thus can make use of the predefined springSecurity FormAuthConfig):

given().
        auth().form("John", "Doe", springSecurity().withCsrfFieldName("_csrf")).
when().
        get("/formAuth");
then().
        statusCode(200);
We've now told REST Assured to search for the CSRF field name called "_csrf" (which is it both faster and less prone to error).

By default the CSRF value is sent as a form parameter with the request but you can configure to send it as a header instead if that's required:

given().
        auth().form("John", "Doe", springSecurity().withCsrfFieldName("_csrf").sendCsrfTokenAsHeader()).
when().
        get("/formAuth");
then().
        statusCode(200);
given().multiPart("controlName", new File("/path/to/file")).when().post("/upload");

It's also possible to supply multiple "multi-parts" entities in the same request:

byte[] someData = ..
given().
        multiPart("controlName1", new File("/path/to/file")).
        multiPart("controlName2", "my_file_name.txt", someData).
        multiPart("controlName3", someJavaObject, "application/json").
when().post("/upload");

Message message = get("/message").as(Message.class);
Message message = get("/message").as(Message.class, ObjectMapperType.GSON);
RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig(myObjectMapper));

Response Logging
get("/x").then().log().body() ..
get("/x").then().log().all(). ..
get("/x").then().log().ifStatusCodeIsEqualTo(302). .. // Only log if the status code is equal to 302
get("/x").then().log().ifStatusCodeMatches(matcher). .. // Only log if the status code matches the supplied Hamcrest matcher

given().log().ifValidationFails(). ..
To log the response do:

.. .then().log().ifValidationFails(). ..
given().config(RestAssured.config().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails(HEADERS))). ..

Session Filter
SessionFilter sessionFilter = new SessionFilter();

given().auth().form("John", "Doe").filter(sessionFilter).when().get("/formAuth").then().statusCode(200);

given().filter(sessionFilter). // Reuse the same session filter instance to automatically apply the session id from the previous response
when().get("/x").then().statusCode(200);

RestAssured.useRelaxedHTTPSValidation();
SSL invalid hostname
RestAssured.config = RestAssured.config().sslConfig(sslConfig().allowAllHostnames());
http://www.jayway.com/2013/04/12/whats-new-in-rest-assured-1-8/
https://github.com/jayway/JsonPath

https://github.com/jayway/rest-assured
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5));
Get and verify all winner ids:
get("/lotto").then().assertThat().body("lotto.winners.winnerId", hasItems(23, 54));
     RestAssured.baseURI  = "http://127.0.0.1:5984/restassured"; 

     Response r = given()
     .contentType("application/json").
     body("{\"name\":\"Jimi Hendrix\"}").
        when().
        post("");

     String body = r.getBody().asString();

final Response response = given().headers(headers).filter(sessionFilter).
when().get(requestUrl).then().statusCode(isOneOf(200, 202, 204)).extract().response();

Response response =
given().contentType(ContentType.URLENC).header(originHeader).formParameters(parameters).when()
.post(url).then().extract().response();

return response.getBody().jsonPath().get("field_name");  
post(requestURL).then().extract().response().path("id")

http://unirest.io/java.html

HttpResponse<JsonNode> response = Unirest.get("http://httpbin.org/headers").basicAuth("username", "password").asJson();
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
  .header("accept", "application/json")
  .body("{\"parameter\":\"value\", \"foo\":\"bar\"}")
  .asJson();
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
  .header("accept", "application/json")
  .field("parameter", "value")
  .field("file", new File("/tmp/file"))

  .asJson();  

No comments:

Post a Comment

Labels

Review (554) System Design (293) System Design - Review (189) Java (178) Coding (75) Interview-System Design (65) Interview (60) Book Notes (59) Coding - Review (59) to-do (45) Knowledge (39) Linux (39) Interview-Java (35) Knowledge - Review (32) Database (30) Design Patterns (29) Product Architecture (28) Big Data (27) Soft Skills (27) Miscs (25) MultiThread (25) Concurrency (24) Cracking Code Interview (24) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Distributed (20) Interview Q&A (20) OOD Design (20) System Design - Practice (19) Security (17) Algorithm (15) How to Ace Interview (15) Brain Teaser (14) Google (13) Linux - Shell (13) Spark (13) Spring (13) Code Quality (12) How to (12) Interview-Database (12) Interview-Operating System (12) Redis (12) Tools (12) Architecture Principles (11) Company - LinkedIn (11) Testing (11) Resource (10) Solr (10) Amazon (9) Cache (9) Search (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Company - Uber (8) Interview - MultiThread (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Scalability (8) Cassandra (7) Git (7) Interview Corner (7) JVM (7) Java Basics (7) Machine Learning (7) NoSQL (7) C++ (6) Design (6) File System (6) Highscalability (6) How to Better (6) Kafka (6) Network (6) Restful (6) Trouble Shooting (6) CareerCup (5) Code Review (5) Company - Facebook (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Be Architect (4) Big Fata (4) C (4) Company Product Architecture (4) Data structures (4) Design Principles (4) Facebook (4) GeeksforGeeks (4) Generics (4) Google Interview (4) Hardware (4) JDK8 (4) Optimization (4) Product + Framework (4) Shopping System (4) Source Code (4) Web Service (4) node.js (4) Back-of-Envelope (3) Company - Pinterest (3) Company - Twiiter (3) Company - Twitter (3) Consistent Hash (3) GOF (3) Game Design (3) GeoHash (3) Growth (3) Guava (3) Interview-Big Data (3) Interview-Linux (3) Interview-Network (3) Java EE Patterns (3) Javarevisited (3) Map Reduce (3) Math - Probabilities (3) Performance (3) Puzzles (3) Python (3) Resource-System Desgin (3) Scala (3) UML (3) geeksquiz (3) AI (2) API Design (2) AngularJS (2) Behavior Question (2) Bugs (2) Coding Interview (2) Company - Netflix (2) Crawler (2) Cross Data Center (2) Data Structure Design (2) Database-Shard (2) Debugging (2) Docker (2) Elasticsearch (2) Garbage Collection (2) Go (2) Hadoop (2) Html (2) Interview - Soft Skills (2) Interview-Miscs (2) Interview-Web (2) JDK (2) Logging (2) POI (2) Papers (2) Programming (2) Project Practice (2) Random (2) Software Desgin (2) System Design - Feed (2) Thread Synchronization (2) Video (2) ZooKeeper (2) reddit (2) Ads (1) Advanced data structures (1) Algorithm - Review (1) Android (1) Approximate Algorithms (1) Base X (1) Bash (1) Books (1) C# (1) CSS (1) Chrome (1) Client-Side (1) Cloud (1) CodingHorror (1) Company - Yelp (1) Counter (1) DSL (1) Dead Lock (1) Difficult Puzzles (1) Distributed ALgorithm (1) Eclipse (1) Facebook Interview (1) Function Design (1) Functional (1) GoLang (1) How to Solve Problems (1) ID Generation (1) IO (1) Important (1) Internals (1) Interview - Dropbox (1) Interview - Project Experience (1) Interview Tips (1) Interview-Brain Teaser (1) Interview-How (1) Interview-Mics (1) Interview-Process (1) Jeff Dean (1) Joda (1) LeetCode - Review (1) Library (1) LinkedIn (1) LintCode (1) Mac (1) Micro-Services (1) Mini System (1) MySQL (1) Nigix (1) NonBlock (1) Process (1) Productivity (1) Program Output (1) Programcreek (1) Quora (1) RPC (1) Raft (1) RateLimiter (1) Reactive (1) Reading (1) Reading Code (1) Refactoring (1) Resource-Java (1) Resource-System Design (1) Resume (1) SQL (1) Sampling (1) Shuffle (1) Slide Window (1) Spotify (1) Stability (1) Storm (1) Summary (1) System Design - TODO (1) Tic Tac Toe (1) Time Management (1) Web Tools (1) algolist (1) corejavainterviewquestions (1) martin fowler (1) mitbbs (1)

Popular Posts