Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Result#withCookies override existing cookies with the same name #7177

Merged
merged 1 commit into from Apr 3, 2017

Conversation

gmethvin
Copy link
Member

Fixes #7174. Basically, this makes sure if we add new cookies, any existing cookies with the same name are removed from the list.

.withCookies(new Http.Cookie("bar", "Mars", 1000, "/", "example.com", false, true, null))
}
}) { response =>
response.allHeaders("Set-Cookie") must contain((s: String) => s.startsWith("bar=Mars;"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this test have failed without this change? i.e. should we also check that it doesn't contain bar=KitKat?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. This is testing the cookies after they are serialized, so there would be no difference. I can add another test for the Result object.

@@ -290,8 +292,12 @@ public Cookie get(String name) {
* @return the transformed copy.
*/
public Result withCookies(Cookie... newCookies) {
List<Cookie> finalCookies = new ArrayList<>(cookies);
finalCookies.addAll(Arrays.asList(newCookies));
List<Cookie> finalCookies = Stream.concat(cookies.stream().filter(cookie -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this really makes me think that cookies should be a map instead of a list. that seems like it'd be easier to use for end users as well since you're always going to want to be getting a cookie by name

Copy link
Member Author

@gmethvin gmethvin Apr 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Http.Cookies already allows getting a cookie by name. This API in Result is for adding new cookies. I guess we could use a map as the underlying data structure but it would require more changes. Obviously this is not the most efficient algorithm, but the expectation is that most people will have no more than a few cookies.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also do result.cookies().get("name")

@benmccann benmccann merged commit 410e9aa into playframework:master Apr 3, 2017
@gmethvin gmethvin deleted the cookies branch April 3, 2017 01:10
wsargent pushed a commit to wsargent/playframework that referenced this pull request Apr 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants