Giter Club home page Giter Club logo

api_automation_framework's Introduction

API Automation Framework

HTTP URL Connection

HttpUrlConnection is a built-in Java class which provides a way of performing HTTP requests in Java. The HttpUrlConnection class allows us to perform basic HTTP requests without the use of any additional libraries. All the classes that we need are part of the java.net package.

Sample Usage

  1. Creating a request The HttpUrlConnection class is used for all types of requests by setting the requestMethod attribute to one of the values: GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE.
  URL url = new URL("http://example.com");
  HttpURLConnection con = (HttpURLConnection) url.openConnection();
  con.setRequestMethod("GET");
  1. Setting Request Headers Adding headers to a request can be achieved by using the setRequestProperty() method:
  con.setRequestProperty("Content-Type", "application/json");
  1. Reading the Response Reading the response of the request can be done by parsing the InputStream of the HttpUrlConnection instance.
  BufferedReader in = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
  String inputLine; 
  StringBuffer content = new StringBuffer();
  while ((inputLine = in.readLine()) != null) {
    content.append(inputLine);
  }
  in.close();

Reference: Refer to the src/test/java/com/apitest/httpurlconnection/ package for creating HTTP requests using HttpURLConnection

Rest Assured

REST Assured is a Java library that provides a domain-specific language (DSL) for writing powerful, maintainable tests for RESTful APIs. REST Assured is a high level Java DSL for simplified testing of REST based services built over HTTP. HttpClient is used by REST Assured under the hood for Http communication.

Sample Usage

  1. Invoking a GET request
  get(endpoint).then().assertThat().statusLine(containsString("200"));
  1. Using Response object for GET request
  Response response = get(endpoint);
  String statusLine = response.getStatusLine();
  System.out.println("Response statusLine: " + statusLine);
  1. Invoke a POST request
  // Create a JSONObject and add the request parameters 
  JSONObject requestParams = new JSONObject();
  requestParams.put("id", "3");
  requestParams.put("name", "Cashews");
  
  // Create a RequestSpecification object and add header and body of the request
  RequestSpecification requestSpecification = given();
  requestSpecification.header("Content-Type", "application/json");
  requestSpecification.body(requestParams.toString());
  
  // Invoke POST call
  Response response = requestSpecification.post(endpoint);  

Reference: Refer to the src/test/java/com/apitest/restassured/test/ package for RestAssured tests

api_automation_framework's People

Contributors

tiwariswapnil avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.