API Testing : Check API localization

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

Language: Java

Framework: Rest assured

copy
1import org.junit.jupiter.api.Test;2import static io.restassured.RestAssured.*;3import static org.hamcrest.Matchers.*;45public class APITest {67 @Test8 /​/​ Assumptions9 /​/​ - API endpoint: http:/​/​localhost:8080/​my-api10 /​/​ - Supported localization: en_US, es_ES, fr_FR11 /​/​ - API returns JSON with a language field indicating the language of the resource12 public void testAPILocalization() {1314 given()15 .queryParam("lang", "en_US")16 .when()17 .get("http:/​/​localhost:8080/​my-api")18 .then()19 .assertThat()20 .statusCode(200)21 .body("language", equalTo("en_US"));2223 given()24 .queryParam("lang", "es_ES")25 .when()26 .get("http:/​/​localhost:8080/​my-api")27 .then()28 .assertThat()29 .statusCode(200)30 .body("language", equalTo("es_ES"));3132 given()33 .queryParam("lang", "fr_FR")34 .when()35 .get("http:/​/​localhost:8080/​my-api")36 .then()37 .assertThat()38 .statusCode(200)39 .body("language", equalTo("fr_FR"));4041 /​/​ Code to connect to remote client with desired capabilities42 /​/​ DesiredCapabilities caps = new DesiredCapabilities();43 /​/​ caps.setCapability("browser", "Chrome");44 /​/​ WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), caps);45 }46}

Language: Javascript

copy
1/​/​ Mocha and Chai23/​/​ Assumptions: 4/​/​ - The API endpoint is functional5/​/​ - The API localization metric is specified in the request header67const chai = require('chai');8const chaiHttp = require('chai-http');9const expect = chai.expect;1011chai.use(chaiHttp);1213describe('API Localization Testing', function () {14 it('should correctly handle API localization', function (done) {15 /​/​ Set up desired local driver16 const driver = require('selenium-webdriver');17 const localDriver = new driver.Builder().forBrowser('chrome').build();18 /​/​ Add commented code to connect to remote client with desired capabilities19 /​/​ const remoteDriver = new driver.Builder()20 /​/​ .withCapabilities(desiredCapabilities)21 /​/​ .usingServer(webdriverUrl)22 /​/​ .build();23 24 chai.request('http:/​/​api.example.com')25 .get('/​resources')26 .set('Accept-Language', 'en-US')27 .end(function (err, res) {28 expect(res).to.have.status(200);29 expect(res.body).to.have.property('resource').to.be.a('string');30 done();31 });32 });33});

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