Skip to main content

API 2

 

https://www.blogger.com/blog/post/edit/695939364463034111/8924884184314606966


Question: How do you handle scenarios where the API returns unexpected content types?
Answer: RestAssured allows you to validate unexpected content types using the contentType() method.

import static io.restassured.RestAssured.*;

public class UnexpectedContentTypeHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .get("/resource")
        .then()
            .contentType("text/plain"); // Validates if the response content type is "text/plain"
    }
}

Question: How do you handle scenarios where the API returns unexpected HTTP status codes?
Answer: RestAssured allows you to validate unexpected HTTP status codes using the not() method. 

import static io.restassured.RestAssured.*;

public class UnexpectedStatusCodeHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .get("/resource")
        .then()
            .statusCode(not(404)); // Validates if the response status code is not 404
    }
}

Question: How do you handle exceptions such as connection timeouts in RestAssured tests?
Answer: You can handle connection timeouts by setting a timeout duration using the timeout() method. Additionally, you can catch the java.net.SocketTimeoutException exception.

import static io.restassured.RestAssured.*;

public class ConnectionTimeoutHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        try {
            given()
                .get("/resource")
                .timeout(5000) // Set a timeout of 5 seconds
            .then()
                .statusCode(200);
        } catch (java.net.SocketTimeoutException e) {
            // Handle timeout exception
            System.out.println("Connection timed out.");
        }
    }
}

Question: How do you handle exceptions such as invalid SSL certificates in RestAssured tests?
Answer: You can handle invalid SSL certificates by disabling SSL certificate validation using the relaxedHTTPSValidation() method.


 
import static io.restassured.RestAssured.*;

public class InvalidSSLCertificateHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .relaxedHTTPSValidation()
            .get("/resource")
        .then()
            .statusCode(200);
    }
}

Question: How do you handle exceptions such as resource not found errors in RestAssured tests?
Answer: You can handle resource not found errors by expecting specific error responses using the expect() method.

import static io.restassured.RestAssured.*;

public class ResourceNotFoundErrorHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .get("/nonexistent-resource")
        .then()
            .statusCode(404)
            .statusLine("HTTP/1.1 404 Not Found");
    }
}

Question: How do you handle exceptions such as JSON parsing errors in RestAssured tests?
Answer: You can handle JSON parsing errors by expecting specific error responses or by catching the io.restassured.path.json.exception.JsonPathException exception.

import static io.restassured.RestAssured.*;

public class JsonParsingErrorHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        try {
            given()
                .get("/malformed-json-resource")
            .then()
                .statusCode(200)
                .extract().jsonPath();
        } catch (io.restassured.path.json.exception.JsonPathException e) {
            // Handle JSON parsing error
            System.out.println("JSON parsing error occurred.");
        }
    }
}
Question: How do you handle exceptions such as server errors (5xx) in RestAssured tests?
Answer: You can handle server errors by expecting specific error responses using the expect() method. 
import static io.restassured.RestAssured.*;

public class ServerErrorHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .get("/error-endpoint")
        .then()
            .statusCode(500)
            .statusLine("HTTP/1.1 500 Internal Server Error");
    }
}

Comments

popular

Privacy policy for Sri Bhagavat Gita

 Privacy Policy for Sri Bhagavad Gita This respects the privacy of its users and is committed to protecting their personal information. This privacy policy outlines the information collected by This and how it is used. Information We Collect : We are not collecting any personal information such as name and email address. This may collect non-personal information such as device type, operating system version, and app usage data to improve the app's functionality and user experience. Sharing of Information This does not sell or share personal information with third parties for marketing purposes. This may share personal information with service providers for the purpose of providing registration or support services to the user. Security of Information This takes reasonable measures to protect user data against unauthorized access, alteration, or destruction. However, This cannot guarantee the security of user data transmitted over the internet. Children's Privacy This does not kn

Privacy policy for BMI calculator

Privacy Policy for BMI Calculator  Effective Date: 5th July 2023 1.1 Personal Information: We do not collect any personally identifiable information (PII) such as your name, address, email, or phone number when you use the App. 1.2 Non-Personal Information: The App may collect certain non-personal information automatically, such as your device's unique identifier (UDID), device type, operating system, language preferences, and anonymous usage statistics. This information is collected to improve the functionality and user experience of the App and is not linked to any personally identifiable information. 2. Use of Information: 2.1 Personal Information: As stated earlier, we do not collect any personal information through the App. Therefore, we do not use or share any personal information. 2.2 Non-Personal Information: The non-personal information collected by the App may be used for the following purposes: - To improve the performance, functionality, and user experience of the App.

privacy policy for Selenium App

 Effective Date: 16 Sep 2023 URL -  https://play.google.com/store/apps/details?id=com.csj.selenium 1. Introduction :   This Privacy Policy outlines how we collect, use, disclose, and safeguard your personal information when you use our Android application ("App"). By accessing or using the App, you agree to the terms and practices described in this Privacy Policy. If you do not agree with our policies and practices, please do not use the App. 2. Information We Collect : - 2.1. Personal Information: We do not collect any personal information from you directly. However, we may collect non-personal information such as device information (e.g., device type, operating system, unique device identifier), and usage data (e.g., pages visited, interactions within the App). 2.2. User-Generated Content: The App allows you to submit questions and answers. Any content you submit will be stored on your local device.  3. How We Use Your Information -We may use non-personal information for an