Skip to content

Commit

Permalink
Add Integration Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharuja committed Jan 22, 2020
1 parent 90bd156 commit cca53b9
Show file tree
Hide file tree
Showing 14 changed files with 451 additions and 42 deletions.
1 change: 1 addition & 0 deletions stdlib/http/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ dependencies {
testCompile project(':ballerina-core')
testCompile 'org.apache.ws.commons.axiom:axiom-api'
testCompile 'org.testng:testng'
testCompile 'com.h2database:h2'

interopImports project(':ballerina-crypto')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testSendRequestsByCookieClient() throws BallerinaTestException {
BMainInstance bMainInstance = new BMainInstance(balServer);
String output = bMainInstance.runMainAndReadStdOut("run", new String[]{
"cookieClient_01.bal"}, balFilepath);
Assert.assertTrue(output.contains("SID003=895gd4dmnmsddd34; SID002=178gd4dmnmsddd34; SID001=239d4dmnmsddd34"));
Assert.assertTrue(output.contains("SID001=239d4dmnmsddd34; SID003=895gd4dmnmsddd34; SID002=178gd4dmnmsddd34"));
}

@Test(description = "Test remove session cookie by client")
Expand All @@ -51,12 +51,12 @@ public void testRemoveSessionCookieByClient() throws BallerinaTestException {
BMainInstance bMainInstance = new BMainInstance(balServer);
String output = bMainInstance.runMainAndReadStdOut("run", new String[]{
"cookieClient_02.bal"}, balFilepath);
Assert.assertTrue(output.contains("SID003=895gd4dmnmsddd34"));
Assert.assertTrue(output.contains("SID001=239d4dmnmsddd34"));
}

@Test(description = "Test sending similar session cookies in the response by server,old cookie is replaced by new" +
" cookie in the cookie store")
public void testAddSimilarSessionCookie() throws BallerinaTestException {
public void testAddSimilarSessionCookies() throws BallerinaTestException {
String balFilepath = (new File("src" + File.separator + "test" + File.separator + "resources" +
File.separator + "http" + File.separator + "src" + File.separator +
"cookie")).getAbsolutePath();
Expand Down Expand Up @@ -98,6 +98,64 @@ public void testSendRequestsByClient() throws BallerinaTestException {
BMainInstance bMainInstance = new BMainInstance(balServer);
String output = bMainInstance.runMainAndReadStdOut("run", new String[]{
"cookieClient_06.bal"}, balFilepath);
Assert.assertTrue(output.contains("SID003=895gd4dmnmsddd34; SID002=178gd4dmnmsddd34; SID001=239d4dmnmsddd34"));
Assert.assertTrue(output.contains("SID001=239d4dmnmsddd34; SID003=895gd4dmnmsddd34; SID002=178gd4dmnmsddd34"));
}

@Test(description = "Test remove persistent cookie by client")
public void testRemovePersistentCookieByClient() throws BallerinaTestException {
String balFilepath = (new File("src" + File.separator + "test" + File.separator + "resources" +
File.separator + "http" + File.separator + "src" + File.separator +
"cookie")).getAbsolutePath();
BMainInstance bMainInstance = new BMainInstance(balServer);
String output = bMainInstance.runMainAndReadStdOut("run", new String[]{
"cookieClient_07.bal"}, balFilepath);
Assert.assertTrue(output.contains("SID003=895gd4dmnmsddd34"));
}

@Test(description = "Test send similar persistent cookies in the response by server, old cookie is replaced by " +
"new cookie in the cookie store")
public void testAddSimilarPersistentCookies() throws BallerinaTestException {
String balFilepath = (new File("src" + File.separator + "test" + File.separator + "resources" +
File.separator + "http" + File.separator + "src" + File.separator +
"cookie")).getAbsolutePath();
BMainInstance bMainInstance = new BMainInstance(balServer);
String output = bMainInstance.runMainAndReadStdOut("run", new String[]{
"cookieClient_08.bal"}, balFilepath);
Assert.assertTrue(output.contains("SID001=895gd4dmnmsddd34"));
}

@Test(description = "Test send a session cookie and a similar persistent cookie in the response by server, old " +
"session cookie is replaced by new persistent cookie in the cookie store")
public void testSendSimilarPersistentAndSessionCookies_1() throws BallerinaTestException {
String balFilepath = (new File("src" + File.separator + "test" + File.separator + "resources" +
File.separator + "http" + File.separator + "src" + File.separator +
"cookie")).getAbsolutePath();
BMainInstance bMainInstance = new BMainInstance(balServer);
String output = bMainInstance.runMainAndReadStdOut("run", new String[]{
"cookieClient_09.bal"}, balFilepath);
Assert.assertTrue(output.contains("SID003=aeaa895gd4dmnmsddd34"));
}

@Test(description = "Test send a persistent cookie and a similar session cookie in the response by server, old " +
"persistent cookie is replaced by new session cookie in the cookie store")
public void testSendSimilarPersistentAndSessionCookies_2() throws BallerinaTestException {
String balFilepath = (new File("src" + File.separator + "test" + File.separator + "resources" +
File.separator + "http" + File.separator + "src" + File.separator +
"cookie")).getAbsolutePath();
BMainInstance bMainInstance = new BMainInstance(balServer);
String output = bMainInstance.runMainAndReadStdOut("run", new String[]{
"cookieClient_10.bal"}, balFilepath);
Assert.assertTrue(output.contains("SID003=895gd4dmnmsddd34"));
}

@Test(description = "Test remove persistent cookie by server")
public void testRemovePersistentCookieByServer() throws BallerinaTestException {
String balFilepath = (new File("src" + File.separator + "test" + File.separator + "resources" +
File.separator + "http" + File.separator + "src" + File.separator +
"cookie")).getAbsolutePath();
BMainInstance bMainInstance = new BMainInstance(balServer);
String output = bMainInstance.runMainAndReadStdOut("run", new String[]{
"cookieClient_11.bal"}, balFilepath);
Assert.assertTrue(output.contains("SID002=178gd4dmnmsddd34"));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
Expand All @@ -16,22 +16,25 @@

import ballerina/http;
import ballerina/io;
import ballerina/file;

public function main() {
http:CsvPersistentCookieHandler myPersistentStore = new("./cookie-test-data/client-1.csv");
http:Client cookieClientEndpoint = new ("http://localhost:9253", {
cookieConfig: { enabled: true }
cookieConfig: { enabled: true, persistentCookieHandler: myPersistentStore }
});
http:Request req = new;
// Server sends the session cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
// Server sends the cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
// Second request is with cookie header and server sends more cookies in the response.
response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
// Third request is with cookie header including all relevant session cookies.
response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
// Third request is with cookie header including all relevant cookies.
response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
if (response is http:Response) {
var payload = response.getTextPayload();
if (payload is string) {
io:print(payload);
}
}
error? removeResults = file:remove("./cookie-test-data", true); // Removes persistent store file.
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
Expand All @@ -16,25 +16,31 @@

import ballerina/http;
import ballerina/io;
import ballerina/file;

public function main() {
http:CsvPersistentCookieHandler myPersistentStore = new("./cookie-test-data/client-2.csv");
http:Client cookieClientEndpoint = new ("http://localhost:9253", {
cookieConfig: { enabled: true }
cookieConfig: { enabled: true, persistentCookieHandler: myPersistentStore }
});
http:Request req = new;
// Server sends the session cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
// Server sends cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
// Removes a session cookie.
http:CookieStore? myCookieStore = cookieClientEndpoint.getCookieStore();
if (myCookieStore is http:CookieStore) {
boolean isRemoved = myCookieStore.removeCookie("SID001", "localhost:9253", "/cookie");
var removeResult = myCookieStore.removeCookie("SID003", "localhost:9253", "/cookie/cookieBackend_1");
if (removeResult is error) {
io:println(removeResult);
}
}
// Sends a request again after one session cookie is removed.
response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
if (response is http:Response) {
var payload = response.getTextPayload();
if (payload is string) {
io:print(payload);
}
}
error? removeResults = file:remove("./cookie-test-data", true);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
Expand All @@ -22,7 +22,7 @@ public function main() {
cookieConfig: { enabled: true }
});
http:Request req = new;
// Server sends similar session cookies in the response for the first request..
// Server sends similar session cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend_2", req);
// Sends second request after replacing the old cookie with the new.
response = cookieClientEndpoint->get("/cookie/cookieBackend_2", req);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
Expand All @@ -18,13 +18,14 @@ import ballerina/http;
import ballerina/io;

public function main() {
http:CsvPersistentCookieHandler myPersistentStore = new("./cookies-test-data/client-4.csv");
http:Client cookieClientEndpoint = new ("http://localhost:9253", {
cookieConfig: { enabled: true, enablePersistence: true }
cookieConfig: { enabled: true, persistentCookieHandler: myPersistentStore }
});
http:Request req = new;
// Server sends the session cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend_3", req);
// Server sends an expired cookie in the response in order to remove the existing cookie in the cookie store.
// Server removes an existing session cookie in the cookie store by sending an expired cookie in the response.
response = cookieClientEndpoint->get("/cookie/cookieBackend_3", req);
// Third request after removing the cookie.
response = cookieClientEndpoint->get("/cookie/cookieBackend_3", req);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
Expand All @@ -16,26 +16,28 @@

import ballerina/http;
import ballerina/io;
import ballerina/file;

public function main() {
http:CsvPersistentCookieHandler myPersistentStore = new("./cookie-test-data/client-5.csv");
http:Client cookieClientEndpoint = new ("http://localhost:9253", {
cookieConfig: { enabled: true }
cookieConfig: { enabled: true, persistentCookieHandler: myPersistentStore }
});
worker w1 {
http:Request req = new;
var response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
var response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
}
worker w2 {
http:Request req = new;
var response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
var response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
}
worker w3 {
http:Request req = new;
var response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
var response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
}
worker w4 {
http:Request req = new;
var response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
var response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
}
_ = wait {w1, w2, w3, w4};
http:CookieStore? myCookieStore = cookieClientEndpoint.getCookieStore();
Expand All @@ -46,4 +48,5 @@ public function main() {
io:println(item.name);
}
}
error? removeResults = file:remove("./cookie-test-data", true);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
Expand All @@ -16,8 +16,10 @@

import ballerina/http;
import ballerina/io;
import ballerina/file;

public function main() {
http:CsvPersistentCookieHandler myPersistentStore = new("./cookie-test-data/client-6.csv");
http:Client cookieClientEndpoint = new ("http://localhost:9253", {
retryConfig: {
intervalInMillis: 3000,
Expand All @@ -36,20 +38,22 @@ public function main() {
statusCodes: [400, 404, 500]
},
cookieConfig: {
enabled: true
enabled: true,
persistentCookieHandler: myPersistentStore
}
});
http:Request req = new;
// Server sends the session cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
// Server sends cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
// Second request is with cookie header and server sends more cookies in the response.
response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
// Third request is with cookie header including all relevant session cookies.
response = cookieClientEndpoint->get("/cookie/cookieBackend", req);
response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
// Third request is with cookie header including all relevant cookies.
response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
if (response is http:Response) {
var payload = response.getTextPayload();
if (payload is string) {
io:print(payload);
}
}
error? removeResults = file:remove("./cookie-test-data", true);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;
import ballerina/io;
import ballerina/file;

public function main() {
http:CsvPersistentCookieHandler myPersistentStore = new("./cookie-test-data/client-7.csv");
http:Client cookieClientEndpoint = new ("http://localhost:9253", {
cookieConfig: { enabled: true, persistentCookieHandler: myPersistentStore }
});
http:Request req = new;
// Server sends the cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
// Removes a persistent cookie.
http:CookieStore? myCookieStore = cookieClientEndpoint.getCookieStore();
if (myCookieStore is http:CookieStore) {
var removeResult = myCookieStore.removeCookie("SID001", "localhost:9253", "/cookie/cookieBackend_1");
if (removeResult is error) {
io:println(removeResult);
}
}
// Sends a request again after one persistent cookie is removed.
response = cookieClientEndpoint->get("/cookie/cookieBackend_1", req);
if (response is http:Response) {
var payload = response.getTextPayload();
if (payload is string) {
io:print(payload);
}
}
error? removeResults = file:remove("./cookie-test-data", true);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;
import ballerina/io;
import ballerina/file;

public function main() {
http:CsvPersistentCookieHandler myPersistentStore = new("./cookie-test-data/client-8.csv");
http:Client cookieClientEndpoint = new ("http://localhost:9253", {
cookieConfig: { enabled: true, persistentCookieHandler: myPersistentStore }
});
http:Request req = new;
// Server sends similar persistent cookies in the response for the first request.
var response = cookieClientEndpoint->get("/cookie/cookieBackend_4", req);
// Sends second request after replacing the old cookie with the new.
response = cookieClientEndpoint->get("/cookie/cookieBackend_4", req);
if (response is http:Response) {
var payload = response.getTextPayload();
if (payload is string) {
io:print(payload);
}
}
error? removeResults = file:remove("./cookie-test-data", true);
}
Loading

0 comments on commit cca53b9

Please sign in to comment.