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

XMLHttpRequest getResponseHeader case sensitivity #89

Merged

Conversation

kalopilato
Copy link
Contributor

@kalopilato kalopilato commented Feb 4, 2021

I came across a problem when using MSW in testing: the component under test has a dependency that makes API calls (authentication) and inspects the "Content-Type" response header (note the casing), but the value of this header was always null. I traced this to the XMLHttpRequestOverride's getResponseHeader function which evaluated "Content-Type" (the passed in header name) against "content-type" (the actual header name in the response), thereby failing and returning null.

This PR updates the XHR getResponseHeader function to normalise the passed in header name and the response headers to lowercase when evaluating equality. This looks to be similar behaviour to jsdom's implementation and aligns with HTTP spec.

@@ -422,13 +422,7 @@ export const createXMLHttpRequestOverride = (
const headerValue = Object.entries(this.responseHeaders).reduce<
string | null
>((_, [headerName, headerValue]) => {
// Ignore header name casing while still allowing to set response headers
// with an arbitrary casing (no normalization).
Copy link
Contributor Author

@kalopilato kalopilato Feb 4, 2021

Choose a reason for hiding this comment

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

This gives me pause - it looks like it was a conscious decision not to normalise case so I might be missing something here. The comment mentions "set"ing response headers but this function is read only, so I don't see the harm in forcing all header names to lowercase for comparison?

Copy link
Member

@kettanaito kettanaito Feb 4, 2021

Choose a reason for hiding this comment

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

I think you're correct: header names must be case-insensitive when retrieving a header. We forgot to normalize the input name to ensure that.

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Thank you for spotting and fixing this! Tremendous appreciation for adding a test covering the regression ❤️

Welcome to contributors, @kalopilato! 🎉

@kettanaito kettanaito force-pushed the xhr-get-response-header-case-sensitivity branch from 48ce657 to e48e391 Compare February 4, 2021 08:32
@kettanaito
Copy link
Member

@kalopilato could you please set the right email for your commits? They don't seem to be recognized as yours on GitHub. This often happens if your email in Git doesn't match the email on GitHub. You deserve your contribution to be associated with you.

@kalopilato kalopilato force-pushed the xhr-get-response-header-case-sensitivity branch from e48e391 to 6ed70f7 Compare February 4, 2021 09:30
@kalopilato
Copy link
Contributor Author

Thanks @kettanaito!! And thanks for the heads up about my git config, have updated and amended those commits. Pleased to have finally made my first open source contribution 😄

@kettanaito
Copy link
Member

kettanaito commented Feb 4, 2021

We are happy to see you contributing, especially with this superb finding. Thank you!

If you're interested in request interception in Node, take a look through the code of this package, I'm sure it may need improvement. We also have a few issues we need help with in the MSW repository.

@kettanaito kettanaito force-pushed the xhr-get-response-header-case-sensitivity branch from 6ed70f7 to d62b3ed Compare February 4, 2021 10:10
@kettanaito kettanaito merged commit 79d2ee3 into mswjs:master Feb 4, 2021
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.

XMLHttpRequest: getResponseHeader() is case sensitive
2 participants