API Testing : Check API internationalization

Verify that the API correctly handles API internationalization and returns the correct resources for each API internationalization metric.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming that we have the following to test the API:2/​/​Endpoint: https:/​/​api.example.com/​v1/​internationalization3/​/​Parameters: LanguageCode (e.g. en-US, fr-CA, es-MX)4/​/​Expected response: JSON object including appropriate resources for the given LanguageCode56import org.junit.jupiter.api.Test;7import static io.restassured.RestAssured.*;8import static org.hamcrest.Matchers.*;910public class InternationalizationTest {1112 @Test13 public void testInternationalizationAPI() {14 given()15 .param("LanguageCode", "en-US")16 .when()17 .get("https:/​/​api.example.com/​v1/​internationalization")18 .then()19 .statusCode(200)20 .body("resources.title", equalTo("Welcome"))21 .body("resources.subtitle", equalTo("Learn More"));22 23 /​/​Additional test cases with different LanguageCode parameters:24 /​/​Assuming that for fr-CA, expected resources are "Bienvenue" and "En savoir plus"25 }26 27 /​*28 /​/​Code to connect to remote client with desired capabilities:29 import java.net.URL;30 import org.openqa.selenium.Capabilities;31 import org.openqa.selenium.remote.RemoteWebDriver;32 import org.openqa.selenium.remote.DesiredCapabilities;33 34 public static void main(String[] args) throws Exception {35 DesiredCapabilities capabilities = DesiredCapabilities.chrome();36 capabilities.setCapability("version", "latest");37 capabilities.setCapability("platform", "WINDOWS");38 39 RemoteWebDriver driver = new RemoteWebDriver(new URL("https:/​/​example.com:4444/​wd/​hub"), capabilities);40 }41 */​42}4344/​/​Note: The above code assumes that we have an API endpoint that returns JSON data in response to a GET request with a LanguageCode parameter. The specific resources returned for each LanguageCode are assumed and need to be verified with the stakeholders. The code also uses Rest Assured framework for Java to simplify API testing. The commented code for connecting to a remote client with desired capabilities is also included.

Language: Javascript

copy
1/​/​ Mocha and Chai.234/​/​Assumptions: API endpoint is available and returns response in JSON format.5/​/​Assumptions: API supports internationalization indicated by language code parameter in URL.67const { expect } = require('chai');8const axios = require('axios');910describe('API internationalization testing', () => {11 it('should return the correct resources for each API internationalization metric', async () => {12 const url = 'https:/​/​example-api.com/​en_US/​path/​to/​resource'; /​/​Assuming API endpoint with language code parameter.13 14 const response = await axios.get(url); /​/​Sending GET request to API.15 16 expect(response.status).to.equal(200); /​/​Expecting response status code 200.17 expect(response.headers['content-type']).to.contains('application/​json'); /​/​Expecting response format to be JSON.1819 const responseBody = response.data;2021 expect(responseBody).to.have.property('greeting').to.include('Hello'); /​/​Expecting greeting message for en_US to include 'Hello'.22 expect(responseBody).to.have.property('farewell').to.include('Goodbye'); /​/​Expecting farewell message for en_US to include 'Goodbye'.2324 /​/​Additional tests can be added for other internationalization metrics.25 });2627 it('should be able to connect to remote client with desired capabilities', () => {28 /​/​Commented code to run test on remote client using Selenium webdriver.29 /​/​ const webdriver = require('selenium-webdriver');30 /​/​ const remote = require('selenium-webdriver/​remote');3132 /​/​ /​/​Desired capabilities for remote client.33 /​/​ const capabilities = {34 /​/​ 'browserName': 'chrome',35 /​/​ 'version': 'latest',36 /​/​ 'platform': 'WIN10'37 /​/​ };3839 /​/​ /​/​Connecting to remote client.40 /​/​ const driver = new webdriver.Builder()41 /​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')42 /​/​ .withCapabilities(capabilities)43 /​/​ .build();44 });45});46

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing