API Testing : Check output encoding

Verify that the API correctly handles output encoding and returns the correct HTTP status code.

Language: Java

Framework: Rest assured

copy
1/​/​Assumptions:2/​/​1. The API endpoint is accessible and functioning3/​/​2. The expected output encoding is UTF-84/​/​3. The expected HTTP status code is 200 OK56import org.junit.jupiter.api.Test;7import static io.restassured.RestAssured.*;8import static org.hamcrest.Matchers.*;910public class APITest {1112 @Test13 public void testOutputEncoding() {14 given()15 .when()16 .get("https:/​/​example.api.com/​output")17 .then()18 .statusCode(200)19 .header("Content-Type", containsString("charset=UTF-8"));20 }21 22 /​/​ Uncomment the following code to run the test on a remote client with desired capabilities23 24/​/​ import org.openqa.selenium.remote.DesiredCapabilities;25/​/​ import org.openqa.selenium.remote.RemoteWebDriver;26/​/​ import java.net.URL;27/​/​28/​/​ @Test29/​/​ public void testOutputEncodingRemote() throws Exception {30/​/​ DesiredCapabilities capabilities = new DesiredCapabilities();31/​/​ capabilities.setBrowserName("chrome");32/​/​ capabilities.setVersion("91");33/​/​ RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);34/​/​ 35/​/​ try {36/​/​ given()37/​/​ .when()38/​/​ .get("https:/​/​example.api.com/​output")39/​/​ .then()40/​/​ .statusCode(200)41/​/​ .header("Content-Type", containsString("charset=UTF-8"));42/​/​ } finally {43/​/​ driver.quit();44/​/​ }45/​/​ }46}

Language: Javascript

copy
1/​/​ Mocha and Chai. 23/​/​Assumptions:4/​/​1. The API endpoint URL is known and accessible. 5/​/​2. The output encoding expected is UTF-8.67const { expect } = require('chai');8const request = require('request');910describe('API output encoding', () => {11 it('should return correct HTTP status code and output encoding', (done) => {12 const options = {13 method: 'GET',14 url: 'https:/​/​example.com/​api',15 encoding: 'utf-8'16 };17 18 /​/​ Uncomment below code for connecting to remote client with desired capabilities19 20 /​/​ const capabilities = {21 /​/​ browserName: 'chrome',22 /​/​ version: 'latest'23 /​/​ };24 25 /​/​ const remoteBrowser = webdriverio.remote({26 /​/​ capabilities: capabilities,27 /​/​ protocol: 'https',28 /​/​ hostname: 'localhost',29 /​/​ port: 444430 /​/​ });31 32 /​/​ const browser = remoteBrowser.init().url(options.url);3334 request(options, (error, response, body) => {35 expect(error).to.be.null;36 expect(response.statusCode).to.equal(200);37 expect(response.headers['content-type']).to.include('charset=utf-8');38 done();39 });40 41 /​/​ Uncomment below code to close remote client connection42 43 /​/​ after(async () => {44 /​/​ await remoteBrowser.end();45 /​/​ });46 });47});

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