API Testing : Check cookie handling

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

Language: Java

Framework: Rest assured

copy
1/​/​Assuming API URL is https:/​/​example.com/​api23import static io.restassured.RestAssured.*;4import static org.hamcrest.Matchers.*;56public class APITest {78 @Test9 public void testCookieHandling() {10 given().11 header("Content-Type", "application/​json").12 cookie("session_id", "12345").13 when().14 get("https:/​/​example.com/​api").15 then().16 assertThat().statusCode(200).17 assertThat().cookie("session_id", equalTo("12345")). /​/​Assuming session_id cookie should be returned18 assertThat().header("Set-Cookie", not(emptyOrNullString())); /​/​Assuming API should set some cookies19 }20 21 /​/​Code to connect to remote client with desired capabilities22 /​*23 DesiredCapabilities capabilities = new DesiredCapabilities();24 /​/​Set capability properties25 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);26 */​27}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assuming API URL4const apiURL = 'https:/​/​example-api.com';56describe('API Cookie Handling Test', function() {78 it('should return correct HTTP status code', function(done) {9 10 /​/​Assuming test cookie name and value11 const cookieName = 'testCookie';12 const cookieValue = 'testValue';1314 /​/​Assuming endpoint that sets cookie15 const setCookieEndpoint = '/​setCookie';1617 /​/​Assuming endpoint that checks cookie is set18 const checkCookieEndpoint = '/​checkCookie';1920 /​/​Assuming expected HTTP status code21 const expectedStatusCode = 200;2223 /​/​Assuming cookie handling24 const cookieHandling = true;2526 const request = require('request');2728 /​/​Assuming POST request to set cookie with desired capabilities of remote client29 const setCookieRequest = request.post({30 url: apiURL + setCookieEndpoint,31 json: {cookieName: cookieName, cookieValue: cookieValue},32 withCredentials: cookieHandling,33 capabilities: {34 browserName: 'chrome',35 chromeOptions: {36 args: ['--headless', '--disable-gpu', '--no-sandbox']37 }38 }39 }, function(error, httpResponse, body) {40 expect(httpResponse.statusCode).to.equal(expectedStatusCode);41 done();42 });4344 request(apiURL+checkCookieEndpoint, function (error, response, body) {45 /​/​Assuming response should contain the set cookie46 expect(body).to.include(cookieName + '=' + cookieValue);47 expect(response.statusCode).to.equal(expectedStatusCode);48 done();49 });50 51 });52 53});

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