From cca53b9ee14cbf4ae2fd31a2a8ec790acd4d832b Mon Sep 17 00:00:00 2001 From: Tharuja Date: Wed, 22 Jan 2020 16:32:11 +0530 Subject: [PATCH] Add Integration Tests --- stdlib/http/build.gradle | 1 + .../http/sample/HTTPCookiesTestCase.java | 66 +++++++- .../http/src/cookie/cookieClient_01.bal | 17 +- .../http/src/cookie/cookieClient_02.bal | 18 ++- .../http/src/cookie/cookieClient_03.bal | 4 +- .../http/src/cookie/cookieClient_04.bal | 7 +- .../http/src/cookie/cookieClient_05.bal | 15 +- .../http/src/cookie/cookieClient_06.bal | 18 ++- .../http/src/cookie/cookieClient_07.bal | 46 ++++++ .../http/src/cookie/cookieClient_08.bal | 38 +++++ .../http/src/cookie/cookieClient_09.bal | 38 +++++ .../http/src/cookie/cookieClient_10.bal | 38 +++++ .../http/src/cookie/cookieClient_11.bal | 40 +++++ .../http/src/httpservices/43_http_cookies.bal | 147 +++++++++++++++++- 14 files changed, 451 insertions(+), 42 deletions(-) create mode 100644 tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_07.bal create mode 100644 tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_08.bal create mode 100644 tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_09.bal create mode 100644 tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_10.bal create mode 100644 tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_11.bal diff --git a/stdlib/http/build.gradle b/stdlib/http/build.gradle index 7a4927a06e28..5f4832241d24 100644 --- a/stdlib/http/build.gradle +++ b/stdlib/http/build.gradle @@ -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') } diff --git a/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/service/http/sample/HTTPCookiesTestCase.java b/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/service/http/sample/HTTPCookiesTestCase.java index b33e3205ab54..5862688c7a1b 100644 --- a/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/service/http/sample/HTTPCookiesTestCase.java +++ b/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/service/http/sample/HTTPCookiesTestCase.java @@ -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") @@ -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(); @@ -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")); } } diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_01.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_01.bal index 4660aa3d4695..5b63f6dd4e4e 100644 --- a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_01.bal +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_01.bal @@ -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 @@ -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. } diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_02.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_02.bal index 9b2830ad6d82..d95a4f9dc32e 100644 --- a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_02.bal +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_02.bal @@ -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 @@ -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); } diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_03.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_03.bal index 4de2b0e1c7df..83bf98b0508b 100644 --- a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_03.bal +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_03.bal @@ -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 @@ -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); diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_04.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_04.bal index c692495b7506..f2946ed66eb4 100644 --- a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_04.bal +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_04.bal @@ -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 @@ -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); diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_05.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_05.bal index 9a2af54af956..31478f368e62 100644 --- a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_05.bal +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_05.bal @@ -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 @@ -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(); @@ -46,4 +48,5 @@ public function main() { io:println(item.name); } } + error? removeResults = file:remove("./cookie-test-data", true); } diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_06.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_06.bal index 13b3c4c2e483..e5e0ab3722f1 100644 --- a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_06.bal +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_06.bal @@ -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 @@ -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, @@ -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); } diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_07.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_07.bal new file mode 100644 index 000000000000..200002646e25 --- /dev/null +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_07.bal @@ -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); +} diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_08.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_08.bal new file mode 100644 index 000000000000..27dd754ee520 --- /dev/null +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_08.bal @@ -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); +} diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_09.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_09.bal new file mode 100644 index 000000000000..9073c4caa742 --- /dev/null +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_09.bal @@ -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-9.csv"); + http:Client cookieClientEndpoint = new ("http://localhost:9253", { + cookieConfig: { enabled: true, persistentCookieHandler: myPersistentStore } + }); + http:Request req = new; + // Server sends a session cookie and a similar persistent cookie in the response for the first request. + var response = cookieClientEndpoint->get("/cookie/cookieBackend_5", req); + // Sends second request after replacing the session cookie with the new persistent cookie. + response = cookieClientEndpoint->get("/cookie/cookieBackend_5", 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); +} diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_10.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_10.bal new file mode 100644 index 000000000000..ece5a3e2c0e2 --- /dev/null +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_10.bal @@ -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-10.csv"); + http:Client cookieClientEndpoint = new ("http://localhost:9253", { + cookieConfig: { enabled: true, persistentCookieHandler: myPersistentStore } + }); + http:Request req = new; + // Server sends a persistent cookie and a similar session cookie in the response for the first request. + var response = cookieClientEndpoint->get("/cookie/cookieBackend_6", req); + // Sends second request after replacing the persistent cookie with the new session cookie. + response = cookieClientEndpoint->get("/cookie/cookieBackend_6", 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); +} diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_11.bal b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_11.bal new file mode 100644 index 000000000000..0ee6a2a1a866 --- /dev/null +++ b/tests/jballerina-integration-test/src/test/resources/http/src/cookie/cookieClient_11.bal @@ -0,0 +1,40 @@ +// 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-11.csv"); + http:Client cookieClientEndpoint = new ("http://localhost:9253", { + cookieConfig: { enabled: true, persistentCookieHandler: myPersistentStore } + }); + http:Request req = new; + // Server sends cookies in the response for the first request. + var response = cookieClientEndpoint->get("/cookie/cookieBackend_7", req); + // Server removes an existing persistent cookie in the cookie store by sending an expired cookie in the response. + response = cookieClientEndpoint->get("/cookie/cookieBackend_7", req); + // Third request after removing the cookie. + response = cookieClientEndpoint->get("/cookie/cookieBackend_7", 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); +} diff --git a/tests/jballerina-integration-test/src/test/resources/http/src/httpservices/43_http_cookies.bal b/tests/jballerina-integration-test/src/test/resources/http/src/httpservices/43_http_cookies.bal index d4832c984982..be5f16040306 100644 --- a/tests/jballerina-integration-test/src/test/resources/http/src/httpservices/43_http_cookies.bal +++ b/tests/jballerina-integration-test/src/test/resources/http/src/httpservices/43_http_cookies.bal @@ -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 @@ -23,24 +23,25 @@ service cookie on new http:Listener(9253) { @http:ResourceConfig { methods: ["GET"], - path: "/cookieBackend" + path: "/cookieBackend_1" } - resource function addSessionCookies(http:Caller caller, http:Request req) { - // Creates the cookies. + resource function addPersistentAndSessionCookies(http:Caller caller, http:Request req) { http:Cookie cookie1 = new("SID001", "239d4dmnmsddd34"); - cookie1.path = "/cookie"; + cookie1.path = "/cookie/cookieBackend_1"; cookie1.domain = "localhost:9253"; cookie1.httpOnly = true; cookie1.secure = false; + cookie1.expires = "2030-06-26 05:46:22"; http:Cookie cookie2 = new("SID002", "178gd4dmnmsddd34"); - cookie2.path = "/cookie/cookieBackend"; + cookie2.path = "/cookie/cookieBackend_1"; cookie2.domain = "localhost:9253"; cookie2.httpOnly = true; cookie2.secure = false; + cookie2.expires = "2030-07-15 05:46:22"; http:Cookie cookie3 = new("SID003", "895gd4dmnmsddd34"); - cookie3.path = "/cookie/cookieBackend"; + cookie3.path = "/cookie/cookieBackend_1"; cookie3.domain = "localhost:9253"; cookie3.httpOnly = true; cookie3.secure = false; @@ -109,6 +110,138 @@ service cookie on new http:Listener(9253) { cookie2.httpOnly = true; cookie2.secure = false; + http:Response res = new; + http:Cookie[] reqstCookies=req.getCookies(); + // Adds cookies if there are no cookies in the inbound request. + if (reqstCookies.length() == 0) { + res.addCookie(cookie1); + res.addCookie(cookie2); + var result = caller->respond(res); + } else if (reqstCookies.length() == 2) { + res.removeCookiesFromRemoteStore(cookie1); + var result = caller->respond(res); + } else { + string cookieHeader = req.getHeader("Cookie"); + res.setPayload(<@untainted> cookieHeader); + var result = caller->respond(res); + } + } + + @http:ResourceConfig { + methods: ["GET"], + path: "/cookieBackend_4" + } + resource function sendSimilarPersistentCookies(http:Caller caller, http:Request req) { + http:Cookie cookie1 = new("SID001", "239d4dmnmsddd34"); + cookie1.path = "/cookie/cookieBackend_4"; + cookie1.domain = "localhost:9253"; + cookie1.httpOnly = false; + cookie1.secure = false; + cookie1.expires = "2030-06-26 05:46:22"; + + http:Cookie cookie3 = new("SID001", "895gd4dmnmsddd34"); + cookie3.path = "/cookie/cookieBackend_4"; + cookie3.domain = "localhost:9253"; + cookie3.httpOnly = true; + cookie3.secure = false; + cookie3.expires = "2030-06-26 05:46:22"; + http:Response res = new; + + http:Cookie[] reqstCookies=req.getCookies(); + // Adds cookies if there are no cookies in the inbound request. + if (reqstCookies.length() == 0) { + res.addCookie(cookie1); + res.addCookie(cookie3); + var result = caller->respond(res); + } else { + string cookieHeader = req.getHeader("Cookie"); + res.setPayload(<@untainted> cookieHeader); + var result = caller->respond(res); + } + } + + @http:ResourceConfig { + methods: ["GET"], + path: "/cookieBackend_5" + } + resource function sendSimilarPersistentAndSessionCookies_1(http:Caller caller, http:Request req) { + http:Cookie cookie2 = new("SID003", "895gd4dmnmsddd34"); + cookie2.path = "/cookie/cookieBackend_5"; + cookie2.domain = "localhost:9253"; + cookie2.httpOnly = true; + cookie2.secure = false; + + http:Cookie cookie3 = new("SID003", "aeaa895gd4dmnmsddd34"); + cookie3.path = "/cookie/cookieBackend_5"; + cookie3.domain = "localhost:9253"; + cookie3.httpOnly = false; + cookie3.secure = false; + cookie3.expires = "2030-07-15 05:46:22"; + + http:Response res = new; + http:Cookie[] reqstCookies=req.getCookies(); + // Adds cookies if there are no cookies in the inbound request. + if (reqstCookies.length() == 0) { + res.addCookie(cookie2); // Adds a session cookie. + res.addCookie(cookie3); // Adds a similar persistent cookie. + var result = caller->respond(res); + } else { + string cookieHeader = req.getHeader("Cookie"); + res.setPayload(<@untainted> cookieHeader); + var result = caller->respond(res); + } + } + + @http:ResourceConfig { + methods: ["GET"], + path: "/cookieBackend_6" + } + resource function sendSimilarPersistentAndSessionCookies_2(http:Caller caller, http:Request req) { + http:Cookie cookie2 = new("SID003", "aeaa895gd4dmnmsddd34"); + cookie2.path = "/cookie/cookieBackend_6"; + cookie2.domain = "localhost:9253"; + cookie2.httpOnly = false; + cookie2.secure = false; + cookie2.expires = "2030-07-15 05:46:22"; + + http:Cookie cookie3 = new("SID003", "895gd4dmnmsddd34"); + cookie3.path = "/cookie/cookieBackend_6"; + cookie3.domain = "localhost:9253"; + cookie3.httpOnly = true; + cookie3.secure = false; + + http:Response res = new; + http:Cookie[] reqstCookies=req.getCookies(); + // Adds cookies if there are no cookies in the inbound request. + if (reqstCookies.length() == 0) { + res.addCookie(cookie2); // Adds a persistent cookie. + res.addCookie(cookie3); // Adds a similar session cookie. + var result = caller->respond(res); + } else { + string cookieHeader = req.getHeader("Cookie"); + res.setPayload(<@untainted> cookieHeader); + var result = caller->respond(res); + } + } + + @http:ResourceConfig { + methods: ["GET"], + path: "/cookieBackend_7" + } + resource function removePersistentCookieByServer(http:Caller caller, http:Request req) { + // Creates the cookies. + http:Cookie cookie1 = new("SID001", "239d4dmnmsddd34"); + cookie1.path = "/cookie/cookieBackend_7"; + cookie1.domain = "localhost:9253"; + cookie1.httpOnly = true; + cookie1.expires = "2030-07-15 05:46:22"; + + http:Cookie cookie2 = new("SID002", "178gd4dmnmsddd34"); + cookie2.path = "/cookie/cookieBackend_7"; + cookie2.domain = "localhost:9253"; + cookie2.httpOnly = true; + cookie2.secure = false; + http:Response res = new; http:Cookie[] reqstCookies=req.getCookies(); // Adds cookies if there are no cookies in the inbound request.