Verify that the API correctly handles various types of data such as strings, numbers, dates, and binary data.
Language: Java
Framework: Rest assured
1import io.restassured.RestAssured;2import io.restassured.response.Response;3import io.restassured.specification.RequestSpecification;4import org.testng.Assert;56public class API_Type_Test {7 private static RequestSpecification request;8 private static Response response;9 private static String endpoint = "https://api.example.com/";1011 public static void main(String[] args) {12 RestAssured.baseURI = endpoint;13 request = RestAssured.given();1415 // Test string data16 response = request.get("/string");17 Assert.assertEquals(response.getStatusCode(), 200);18 Assert.assertEquals(response.getPath("dataType"), "string");1920 // Test number data21 response = request.get("/number");22 Assert.assertEquals(response.getStatusCode(), 200);23 Assert.assertEquals(response.getPath("dataType"), "number");2425 // Test date data26 response = request.get("/date");27 Assert.assertEquals(response.getStatusCode(), 200);28 Assert.assertEquals(response.getPath("dataType"), "date");2930 // Test binary data31 response = request.get("/binary");32 Assert.assertEquals(response.getStatusCode(), 200);33 Assert.assertEquals(response.getPath("dataType"), "binary");34 }35 36 // Uncomment the following code to connect to remote client with desired capabilities37 /*38 public static void main(String[] args) {39 DesiredCapabilities capabilities = new DesiredCapabilities();40 capabilities.setBrowserName("chrome");41 capabilities.setVersion("87.0");4243 // Set up the remote client44 RemoteWebDriver driver = new RemoteWebDriver(new URL("http://remote-client.example.com/wd/hub"), capabilities);45 RestAssured.defaultParser = Parser.HTML;4647 // Perform the API tests48 ...49 }50 */51}
Language: Javascript
1// Mocha + chai + Supertest23//Assuming API endpoint is "https://example.com/api"45//Test suite6describe('API Data Types Test', function(){7 //Test case 1 - Verify API handles string data correctly8 it('should handle string data correctly', function(done){9 //Assuming API request for string data is "https://example.com/api/data?type=string"10 request('https://example.com/api')11 .get('/data?type=string')12 .expect(200)13 .end(function(err, res){14 expect(res.body).to.be.a('string'); //Assert response body is a string15 done();16 });17 });1819 //Test case 2 - Verify API handles numeric data correctly20 it('should handle numeric data correctly', function(done){21 //Assuming API request for numeric data is "https://example.com/api/data?type=numeric"22 request('https://example.com/api')23 .get('/data?type=numeric')24 .expect(200)25 .end(function(err, res){26 expect(res.body).to.be.a('number'); //Assert response body is a number27 done();28 });29 });3031 //Test case 3 - Verify API handles date/time data correctly32 it('should handle date/time data correctly', function(done){33 //Assuming API request for date/time data is "https://example.com/api/data?type=dateTime"34 request('https://example.com/api')35 .get('/data?type=dateTime')36 .expect(200)37 .end(function(err, res){38 expect(res.body).to.be.a('string'); //Assert response body is a string39 //Assuming date/time format is ISO 8601 - "YYYY-MM-DDTHH:mm:ss.sssZ"40 expect(res.body).to.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/);41 done();42 });43 });4445 //Test case 4 - Verify API handles binary data correctly46 it('should handle binary data correctly', function(done){47 //Assuming API request for binary data is "https://example.com/api/data?type=binary"48 request('https://example.com/api')49 .get('/data?type=binary')50 .expect(200)51 .end(function(err, res){52 expect(res.body).to.be.an('array'); //Assert response body is an array53 expect(res.body[0]).to.be.a('number'); //Assert each element in the array is a number54 expect(res.body[0]).to.be.within(0, 255); //Assert each number is within the range of 0-25555 done();56 });57 });58});5960//Commented code to connect to remote client with desired capabilities and use remote driver instead of local driver61/*var webdriver = require('selenium-webdriver');62var remote = require('selenium-webdriver/remote');6364var capabilities = webdriver.Capabilities.chrome();65capabilities.set('chromeOptions', {66 'args': ['--headless', '--disable-gpu']67});6869var driver = new webdriver.Builder()70 .usingServer('http://remote-server:4444/wd/hub')71 .withCapabilities(capabilities)72 .build();*/
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing