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

Combine cookies from original request and session file #932

Merged
merged 11 commits into from
Jun 18, 2020

Conversation

kbanc
Copy link
Contributor

@kbanc kbanc commented Jun 15, 2020

This pull request should address #824. Cookies that are set through a request in the CLI are added to the session cookies. @gmelodie

Copy link
Member

@jkbrzt jkbrzt left a comment

Choose a reason for hiding this comment

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

It looks very promising!

In addition to the inline comments, could you please use 'single quotes' for strings?

httpie/sessions.py Outdated Show resolved Hide resolved
httpie/sessions.py Outdated Show resolved Hide resolved
@gmelodie gmelodie force-pushed the combine-original-and-new-cookies branch from bda90d1 to d58e1cd Compare June 16, 2020 18:23
@kbanc kbanc requested a review from jkbrzt June 16, 2020 18:40
httpie/sessions.py Outdated Show resolved Hide resolved
@@ -79,6 +81,12 @@ def update_headers(self, request_headers: RequestHeadersDict):
if name == 'User-Agent' and value.startswith('HTTPie/'):
continue

if name.lower() == 'cookie':
for cookie_name, morsel in SimpleCookie(value).items():
self['cookies'][cookie_name] = {'value': morsel.value}
Copy link
Member

Choose a reason for hiding this comment

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

What is missing now is to define & document & test priority rules. With this implementation, request cookies seem to win over response cookies. But it feels like it should be the other way, i.e., last seen cookies should be the ones that get stored in the session. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We added a new test, test_cookie_storage_priority in TestSessions to define and document the priority rules! Is this sufficient, or should there also be documentation in the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After our call this morning, @gmelodie and I discussed the possible behaviours of cookies expiring in sessions and which cookies should take precedent.

Our understanding was regardless of the expiry behaviour, the storage priority should still be server set > command line set > session file. For that reason, we didn't write further related tests. Here's a couple of example cases illustrating why we didn't think further tests were necessary:

  • Case 1: Server expires a cookie. If a server expires a cookie, it will be removed from the session file regardless of how it was set (test_sessions.py already has test covering this)
  • Case 2: Cookies in session file expire. Cookies stored in a session file that expire will never be sent to the server and will be removed when the request is made
  • Case 3: User sets an expired cookie in the command line. We haven't been able to find a way for a user to set an expired cookie in the command line. However, if one is set, we would assume the intention would be to remove the cookie from the session. As this cookie would replace the cookie in the session file, and expired cookies are cleared from the session file when the request is sent, this behaviour is satisfied.

Copy link
Member

Choose a reason for hiding this comment

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

Our understanding was regardless of the expiry behaviour, the storage priority should still be server set > command line set > session file.

Agree 👍🏻

httpie/sessions.py Outdated Show resolved Hide resolved
@kbanc kbanc force-pushed the combine-original-and-new-cookies branch from dd36cf2 to 5b98e4a Compare June 17, 2020 13:48
@gmelodie
Copy link

gmelodie commented Jun 17, 2020

Hey @jakubroztocil we worked on some refactoring today. We added what you mentioned on
bfea90f and then we did some refactoring on the tests on 5b98e4a. Could you tell us which one you like best?

Obs: we were trying out some git stuff so we apologize for the commits not being the best. We'll be working to fix that later today

httpie/sessions.py Outdated Show resolved Hide resolved
@gmelodie gmelodie force-pushed the combine-original-and-new-cookies branch from 5b98e4a to 82ca7f1 Compare June 17, 2020 15:08
@kbanc kbanc requested a review from jkbrzt June 18, 2020 13:22
@kbanc kbanc force-pushed the combine-original-and-new-cookies branch from 82ca7f1 to 06af281 Compare June 18, 2020 16:52
@gmelodie gmelodie mentioned this pull request Jun 18, 2020
3. Manually set cookie parameters in the json file of the session

.. code-block:: json

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We tried to emphasize lines 12-18 in this code block using :emphasize-lines:, but we weren't able to get it to render properly.

@jkbrzt jkbrzt merged commit 9500ce1 into httpie:master Jun 18, 2020
@jkbrzt
Copy link
Member

jkbrzt commented Jun 18, 2020

Awesome work, thanks @kbanc and @gmelodie! 🍰 🚀

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.

3 participants