Skip to content

Commit

Permalink
Merge pull request #1907 from ballerina-platform/identical-cookie-fix
Browse files Browse the repository at this point in the history
Fix the inconsistency in overwriting the identical cookie
  • Loading branch information
TharmiganK authored Mar 20, 2024
2 parents 759d7ac + ff0155d commit b91eb3a
Show file tree
Hide file tree
Showing 29 changed files with 237 additions and 90 deletions.
6 changes: 3 additions & 3 deletions ballerina-tests/http-advanced-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_advanced_tests"
version = "2.10.11"
version = "2.10.12"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.11"
version = "2.10.12"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.11.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.12-SNAPSHOT.jar"
6 changes: 3 additions & 3 deletions ballerina-tests/http-advanced-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -105,7 +105,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_advanced_tests"
version = "2.10.11"
version = "2.10.12"
dependencies = [
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "file"},
Expand All @@ -125,7 +125,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_test_common"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "lang.string"},
Expand Down
139 changes: 139 additions & 0 deletions ballerina-tests/http-advanced-tests/tests/http_identical_cookies.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. 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/test;

service /api on new http:Listener(identicalCookiePort) {

resource function get .(@http:Header string? cookie) returns http:Response {
http:Response res = new;
http:Cookie resCookie = new ("cookie", "default", path = "/api");
res.addCookie(resCookie);
res.setTextPayload("Hello, World!");
if cookie is string {
res.setHeader("Req-Cookies", cookie);
}
return res;
}

resource function get foo(@http:Header string? cookie, string value = "foo") returns http:Response {
http:Response res = new;
http:Cookie resCookie = new ("cookie", value, path = "/api/foo");
res.addCookie(resCookie);
res.setTextPayload("Hello, World!");
if cookie is string {
res.setHeader("Req-Cookies", cookie);
}
return res;
}

resource function get bar(@http:Header string? cookie) returns http:Response {
http:Response res = new;
http:Cookie resCookie = new ("cookie", "bar", path = "/api/bar");
res.addCookie(resCookie);
res.setTextPayload("Hello, World!");
if cookie is string {
res.setHeader("Req-Cookies", cookie);
}
return res;
}

resource function 'default [string... path](@http:Header string? cookie) returns http:Response {
http:Response res = new;
http:Cookie resCookie = new ("cookie", string:'join("/", ...path), path = "/api/");
res.addCookie(resCookie);
res.setTextPayload("Hello, World!");
if cookie is string {
res.setHeader("Req-Cookies", cookie);
}
return res;
}
}

@test:Config {}
function testIdenticalCookieOverwrite1() returns error? {
http:Client cookieClient = check new (string `localhost:${identicalCookiePort}`,
cookieConfig = {
enabled: true
}
);
http:Response res = check cookieClient->/api/foo;
test:assertFalse(res.hasHeader("Req-Cookies"), "Req-Cookies header should not be present");

res = check cookieClient->/api/foo(value = "random");
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=foo", "Req-Cookies header value mismatched");

res = check cookieClient->/api/foo;
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=random", "Req-Cookies header value mismatched");
}

@test:Config {}
function testIdenticalCookieOverwrite2() returns error? {
http:Client cookieClient = check new (string `localhost:${identicalCookiePort}`,
cookieConfig = {
enabled: true
}
);
http:Response res = check cookieClient->/api/baz;
test:assertFalse(res.hasHeader("Req-Cookies"), "Req-Cookies header should not be present");

res = check cookieClient->/api/foo/baz();
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=baz", "Req-Cookies header value mismatched");

res = check cookieClient->/api/baz;
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=foo/baz", "Req-Cookies header value mismatched");
}

@test:Config {}
function testNonIdenticalCookieWithSameName1() returns error? {
http:Client cookieClient = check new (string `localhost:${identicalCookiePort}`,
cookieConfig = {
enabled: true
}
);
http:Response res = check cookieClient->/api;
test:assertFalse(res.hasHeader("Req-Cookies"), "Req-Cookies header should not be present");

res = check cookieClient->/api/foo;
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=default", "Req-Cookies header value mismatched");

res = check cookieClient->/api/foo(value = "new");
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=default; cookie=foo", "Req-Cookies header value mismatched");

res = check cookieClient->/api/foo;
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=default; cookie=new", "Req-Cookies header value mismatched");
}

@test:Config {}
function testNonIdenticalCookieWithSameName2() returns error? {
http:Client cookieClient = check new (string `localhost:${identicalCookiePort}`,
cookieConfig = {
enabled: true
}
);
http:Response res = check cookieClient->/api;
test:assertFalse(res.hasHeader("Req-Cookies"), "Req-Cookies header should not be present");

res = check cookieClient->/api/baz;
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=default", "Req-Cookies header value mismatched");

res = check cookieClient->/api/baz/foo;
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=default; cookie=baz", "Req-Cookies header value mismatched");

res = check cookieClient->/api/baz;
test:assertEquals(res.getHeader("Req-Cookies"), "cookie=default; cookie=baz/foo", "Req-Cookies header value mismatched");
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ const int serviceMediaTypeSubtypePrefixPort = 9579;

const int statusCodeErrorUseCasePort = 9090;
const int statusCodeErrorPort = 9092;

const int identicalCookiePort = 9093;
6 changes: 3 additions & 3 deletions ballerina-tests/http-client-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_client_tests"
version = "2.10.11"
version = "2.10.12"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.11"
version = "2.10.12"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.11.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.12-SNAPSHOT.jar"
6 changes: 3 additions & 3 deletions ballerina-tests/http-client-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -102,7 +102,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_client_tests"
version = "2.10.11"
version = "2.10.12"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
Expand All @@ -121,7 +121,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_test_common"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "lang.string"},
Expand Down
6 changes: 3 additions & 3 deletions ballerina-tests/http-dispatching-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_dispatching_tests"
version = "2.10.11"
version = "2.10.12"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.11"
version = "2.10.12"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.11.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.12-SNAPSHOT.jar"
6 changes: 3 additions & 3 deletions ballerina-tests/http-dispatching-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -102,7 +102,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_dispatching_tests"
version = "2.10.11"
version = "2.10.12"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
Expand All @@ -124,7 +124,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_test_common"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "lang.string"},
Expand Down
6 changes: 3 additions & 3 deletions ballerina-tests/http-interceptor-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_interceptor_tests"
version = "2.10.11"
version = "2.10.12"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.11"
version = "2.10.12"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.11.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.12-SNAPSHOT.jar"
6 changes: 3 additions & 3 deletions ballerina-tests/http-interceptor-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -99,7 +99,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_interceptor_tests"
version = "2.10.11"
version = "2.10.12"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "http_test_common"},
Expand All @@ -115,7 +115,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_test_common"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "lang.string"},
Expand Down
6 changes: 3 additions & 3 deletions ballerina-tests/http-misc-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_misc_tests"
version = "2.10.11"
version = "2.10.12"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.11"
version = "2.10.12"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.11.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.12-SNAPSHOT.jar"
6 changes: 3 additions & 3 deletions ballerina-tests/http-misc-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -99,7 +99,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_misc_tests"
version = "2.10.11"
version = "2.10.12"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "http_test_common"},
Expand All @@ -118,7 +118,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_test_common"
version = "2.10.11"
version = "2.10.12"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "lang.string"},
Expand Down
Loading

0 comments on commit b91eb3a

Please sign in to comment.