Skip to content

Commit

Permalink
Fix issue eclipse-ee4j#4773
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <[email protected]>
  • Loading branch information
tvallin committed May 12, 2021
1 parent e129ced commit b062901
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -303,12 +303,12 @@ public static void checkHeaderChanges(final Map<String, String> headersSnapshot,
/**
* Compare two NewCookies having the same name. See documentation RFC.
*
* @param first NewCookie to be compared.
* @param second NewCookie to be compared.
* @param first NewCookie to be compared.
* @param second NewCookie to be compared.
* @return the preferred NewCookie according to rules :
* - the latest maxAge.
* - if equal, compare the expiry date
* - if equal, compare name length
* - if equal, compare the expiry date.
* - if equal, compare path length.
*/
public static NewCookie getPreferredCookie(NewCookie first, NewCookie second) {

Expand All @@ -318,13 +318,14 @@ public static NewCookie getPreferredCookie(NewCookie first, NewCookie second) {
return first;
}

if (first.getMaxAge() != second.getMaxAge()){
if (first.getMaxAge() != second.getMaxAge()) {
return Comparator.comparing(NewCookie::getMaxAge).compare(first, second) > 0 ? first : second;
} else if (first.getExpiry() != null && second.getExpiry() != null && !first.getExpiry().equals(second.getExpiry())) {
return Comparator.comparing(NewCookie::getExpiry).compare(first, second) > 0 ? first : second;
} else {
} else if (first.getPath() != null && second.getPath() != null) {
return first.getPath().length() > second.getPath().length() ? first : second;
}
return first;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -185,7 +185,7 @@ public void testAsHeaderString() throws Exception {
}

@Test
public void testgetPreferredCookie(){
public void testGetPreferredCookie(){

Calendar calendar = Calendar.getInstance();
calendar.set(2000, Calendar.JANUARY, 1);
Expand Down Expand Up @@ -227,4 +227,13 @@ public void testgetPreferredCookie(){

}

@Test
public void testGetPreferredCookieWithNullPath() {
NewCookie first = new NewCookie("NAME", "VALUE");
NewCookie second = new NewCookie("NAME", "VALUE");
NewCookie returnedCookie = HeaderUtils.getPreferredCookie(first, second);

assertEquals(first, returnedCookie);
}

}

0 comments on commit b062901

Please sign in to comment.