Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

bc-jz
Copy link
Contributor

@bc-jz bc-jz commented Feb 25, 2025

What?

Add a getHeadersList() method to the Connection class to allow returning a list of all response headers, including headers with duplicate keys.

The existing getHeaders() method and process for generating those headers will overwrite response headers that share the same key name leaving only the last one. This has become a problem in many of our tests where we depend on the Set-Cookie response header to retrieve an XSRF-TOKEN cookie. We have found that responses with multiple Set-Cookie response headers are not reliably making the XSRF-TOKEN cookie available as it gets overwritten by another Set-Cookie header.

This change is meant to be additive and not alter the output of any existing methods.

Tickets / Documentation

Screenshots (if appropriate)

Here is a live example of the difference between the results of the existing headers collection (responseHeaders) and this new headers collection (responseHeadersList). Note the responseHeaders only has less than the full list of headers:
Screenshot 2025-02-25 at 9 47 11 AM
Screenshot 2025-02-25 at 9 47 34 AM
^ You can also see that responseHeadersList shows multiple Set-Cookie response headers. This is what this PR is intending to add.

@bc-jz bc-jz force-pushed the STRF-13001 branch 2 times, most recently from d1c4dc7 to 73b6776 Compare February 25, 2025 13:14
Copy link

@bc-max bc-max left a comment

Choose a reason for hiding this comment

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

@bc-jz I see that there are some other then cookies duplicate headers. E.g. content-type. So my question is it possible we have the same header with different values? Why this is happening (non cookies duplicated)?

@bc-jz
Copy link
Contributor Author

bc-jz commented Feb 25, 2025

@bc-jz I see that there are some other then cookies duplicate headers. E.g. content-type. So my question is it possible we have the same header with different values? Why this is happening (non cookies duplicated)?

I see what you mean about other duplicates.....maybe I need to clear this list of headers between requests. Let me see what happens if I do.

@bc-jz
Copy link
Contributor Author

bc-jz commented Feb 25, 2025

@bc-jz I see that there are some other then cookies duplicate headers. E.g. content-type. So my question is it possible we have the same header with different values? Why this is happening (non cookies duplicated)?

Good callout! The results look a lot better by clearing the property during the initializeRequest() method.


/**
* @var array<string, string> Hash of headers from HTTP response
* @var array<string, string> Hash of headers from HTTP response. Will overwrite cookies with duplicates keys.
Copy link
Contributor

Choose a reason for hiding this comment

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

really this is valid for any header, not just cookies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah good point. I'll update the comment.

} else {
$trimHeader = trim($headers);
if (!empty($trimHeader)) {
$this->responseHeadersList[] = $trimHeader;
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 do we want to classify them by key? something like:

$this->responseHeaderList[$key][] = $value;

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 could. What I plan to do with them is iterate through looking for the Set-Cookie that has the XSRF-TOKEN. I suppose that is a little more performant if we have already filtered by the $key.

Do you think it is worth the extra complication here?

Copy link
Contributor

Choose a reason for hiding this comment

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

not worried about performance here, we don't expect 3000 headers 😅

just exposing an api that makes sense, considering the other api is arranged by header type, I was thinking it might be worth keeping the API similar.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind either way really 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya that looks good to me. Makes my parsing code a little easier to read in regards to searching the Set-Cookie headers. I think it would make it a little more annoying if reading other headers that only have a single entry. This is how it looks:
Screenshot 2025-02-25 at 9 42 08 AM

Copy link
Contributor

Choose a reason for hiding this comment

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

if you expect a single entry, shouldn't you use the other method? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. You could and if by some chance there is multiple then it would be available here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated pr. 👍

Copy link
Contributor Author

@bc-jz bc-jz Feb 25, 2025

Choose a reason for hiding this comment

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

Defining type was griefing me but got it going now 😅

@bc-jz bc-jz force-pushed the STRF-13001 branch 4 times, most recently from 3b34722 to 0d8ec02 Compare February 25, 2025 16:00
… returning a list of all response headers, including headers with duplicate keys.
@bc-jz bc-jz merged commit deebe4b into bigcommerce:master Feb 25, 2025
10 checks passed
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.

5 participants