selenium icon indicating copy to clipboard operation
selenium copied to clipboard

[java] case insensitive header names in http requests

Open iampopovich opened this issue 1 year ago • 11 comments

User description

Thanks for contributing to Selenium! A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines. Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

I opened PR with initial changes. i have already asked some questions about feature in feature request ticket I will implement new tests as soon as I get more information about task details

Motivation and Context

according to #12697 I started to discover how to implement this feature

Types of changes

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • [x] I have read the contributing document.
  • [ ] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.
  • [ ] I have added tests to cover my changes.
  • [ ] All new and existing tests passed.

PR Type

Enhancement


Description

  • Normalize header names to lowercase in setHeader method to ensure case-insensitive handling.
  • Normalize header names to lowercase in addHeader method to ensure case-insensitive handling.

Changes walkthrough 📝

Relevant files
Enhancement
HttpMessage.java
Normalize header names to lowercase in HTTP message handling.

java/src/org/openqa/selenium/remote/http/HttpMessage.java

  • Convert header names to lowercase in setHeader method.
  • Convert header names to lowercase in addHeader method.
  • +2/-2     

    💡 PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    iampopovich avatar Jun 06 '24 17:06 iampopovich

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are limited to a few lines in a single file, modifying header handling to be case-insensitive. The logic is straightforward, but the impact on existing functionality should be carefully evaluated.

    🧪 Relevant tests

    No

    ⚡ Possible issues

    Consistency Issue: The PR changes headers to be stored in lowercase, which might affect other parts of the system that rely on the original case format of headers.

    🔒 Security concerns

    No

    qodo-merge-pro[bot] avatar Jun 06 '24 17:06 qodo-merge-pro[bot]

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add a null check for the name parameter to prevent NullPointerException

    Consider adding a null check for the name parameter before converting it to lowercase to
    avoid potential NullPointerException.

    java/src/org/openqa/selenium/remote/http/HttpMessage.java [126]

    +if (name == null) {
    +    throw new IllegalArgumentException("Header name cannot be null");
    +}
     return removeHeader(name).addHeader(name.toLowerCase(), value);
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding a null check is crucial to prevent runtime exceptions when the 'name' parameter is null. This is a significant improvement for robustness.

    8
    Best practice
    Use Locale.ROOT in toLowerCase to ensure consistent behavior across locales

    Consider using Locale.ROOT in the toLowerCase method to ensure consistent behavior across
    different locales.

    java/src/org/openqa/selenium/remote/http/HttpMessage.java [126]

    -return removeHeader(name).addHeader(name.toLowerCase(), value);
    +return removeHeader(name).addHeader(name.toLowerCase(Locale.ROOT), value);
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using Locale.ROOT for toLowerCase ensures consistent behavior across different system locales, which is important for the predictability of HTTP header handling.

    7

    qodo-merge-pro[bot] avatar Jun 06 '24 17:06 qodo-merge-pro[bot]

    Let's at least wait to merge this after the upcoming release so that Appium can run their tests against nightly and see if it causes them problems before a production release.

    titusfortner avatar Jun 18 '24 19:06 titusfortner

    Several tests failed. Can you please check?

    diemol avatar Aug 09 '24 15:08 diemol

    @diemol sure The FederatedCredentialManagementTest could not be executed due to a process creation error on Windows, related to the long path to the executable file. As a result, the test failed three times, despite the successful execution of other tests. Screenshot 2024-08-09 at 22 51 06

    This failure occurs frequently in test runs on Windows and is not directly related to the changes in the pull request.

    iampopovich avatar Aug 09 '24 15:08 iampopovich

    i found issue that can help fix it https://github.com/bazelbuild/bazel/issues/19710

    iampopovich avatar Aug 09 '24 16:08 iampopovich

    I meant this ones https://github.com/SeleniumHQ/selenium/actions/runs/10321185029/job/28573474046?pr=14095

    diemol avatar Aug 09 '24 16:08 diemol

    oh...i see I mentioned this in the feature request here. The failing tests need to be fixed under the assumption that headers are now case-insensitive. I will correct the existing tests and resubmit them for review.

    Screenshot 2024-08-09 at 23 29 04

    iampopovich avatar Aug 09 '24 16:08 iampopovich

    @diemol please verify my changes in tests I tried to preserve the logic of the checks, considering that header names are now case-insensitive. I also fixed the HttpMessage.setHeader method. Previously, it couldn't remove a header because the headers were set in lowercase, while the name was passed in an arbitrary case.

    public M setHeader(String name, String value) {
        return removeHeader(name.toLowerCase()).addHeader(name.toLowerCase(), value);
    }
    

    iampopovich avatar Aug 09 '24 16:08 iampopovich

    i noticed the problem with the formatting 👀

    iampopovich avatar Aug 09 '24 20:08 iampopovich

    @diemol i fixed the tests with headers

    iampopovich avatar Aug 12 '24 17:08 iampopovich