Skip to content

[JENKINS-68154] Add Custom headers with special characters in their names #201

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

PratikMane0112
Copy link
Contributor

@PratikMane0112 PratikMane0112 commented Apr 1, 2025

Description

This PR addresses JENKINS-68154 & improvement on PR - 194 to allow custom headers with special characters in their names, such as hyphens.

Changes Done

In HttpRequestNameValuePair.java:

  • Added a new constructor that accepts Map.Entry to support headers with special characters
  • Added a static factory method for easier creation of headers with special characters
  • Added DataBoundSetter for maskValue property

In HttpRequestStep.java:

  • Added a new method to set custom headers using a Map (headersMap)
  • This allows for simpler syntax when using headers with special characters

In HttpRequest.java:

  • Added a similar method to set custom headers using a Map
  • Ensures consistent API between freestyle and pipeline jobs

In README.adoc:

  • Added documentation for the new feature with examples showing how to use it

Added tests:

  • Created HttpRequestStepSpecialHeadersTest.java with tests for:
    • Using Map syntax for headers with special characters
    • Using the static factory method
    • Using the Map.Entry constructor

Testing done

  • All tests pass successfully
  • Manually verified with example headers containing hyphens in local jenkins controller
  • Pipeline I tested for customHeaders:
pipeline {
    agent any
    stages {
        stage('Test HTTP Request with Special Header Names') {
            steps {
                script {
                    echo "Testing HTTP Request with special characters in header names"
                    
                    def headerName1 = 'my-custom-header-with-hyphen-separator'
                    def headerValue1 = 'test-value-1'
                    def headerName2 = 'another-header-with-hyphens'
                    def headerValue2 = 'test-value-2'
                    
                    // Create a map of headers with special characters
                    def headers = [
                        "${headerName1}": "${headerValue1}",
                        "${headerName2}": "${headerValue2}"
                    ]
                    
                    // Make an HTTP request with these headers
                    def response = httpRequest(
                        url: 'https://httpbin.org/headers', // This endpoint returns the headers it receives
                        headersMap: headers  // Using our new headersMap parameter
                    )
                    
                    // Log the response
                    echo "Response status: ${response.status}"
                    echo "Response content: ${response.content}"
                    
                    // The response from httpbin.org has capitalized header names
                    // For example: 'my-custom-header-with-hyphen-separator' becomes 'My-Custom-Header-With-Hyphen-Separator'
                    def responseContent = response.content.toLowerCase()
                    assert responseContent.contains(headerValue1.toLowerCase())
                    assert responseContent.contains(headerValue2.toLowerCase())
                    
                    echo "Custom headers with special characters were successfully used!"
                }
            }
        }
    }
}
  • Its build console output
Started by user unknown or anonymous
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins
 in /home/pratik_0112/GSoC/http-request-plugin/work/workspace/TestCustomHeaders
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test HTTP Request with Special Header Names)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Testing HTTP Request with special characters in header names
[Pipeline] httpRequest
HttpMethod: GET
URL: https://httpbin.org/headers
my-custom-header-with-hyphen-separator: test-value-1
another-header-with-hyphens: test-value-2
Sending request to url: https://httpbin.org/headers
Response Code: HTTP/1.1 200 OK
Success: Status code 200 is in the accepted range: 100:399
[Pipeline] echo
Response status: 200
[Pipeline] echo
Response content: {
  "headers": {
    "Accept-Encoding": "gzip,deflate", 
    "Another-Header-With-Hyphens": "test-value-2", 
    "Host": "httpbin.org", 
    "My-Custom-Header-With-Hyphen-Separator": "test-value-1", 
    "User-Agent": "Apache-HttpClient/4.5.14 (Java/17.0.14)", 
    "X-Amzn-Trace-Id": "Root=1-67ebbb1f-6a9bd01214701c9d7c91565a"
  }
}

[Pipeline] echo
Custom headers with special characters were successfully used!
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests - that demonstrates feature works or fixes the issue

@PratikMane0112 PratikMane0112 requested a review from a team as a code owner April 1, 2025 10:25
@PratikMane0112 PratikMane0112 changed the title [JENKINS-68154] Fix Custom headers with special characters in their names [JENKINS-68154] Add Custom headers with special characters in their names Apr 1, 2025
@PratikMane0112
Copy link
Contributor Author

@MarkEWaite @gounthar, Can you please review & tell the changes that needs to be done further, I am ready to update & implement them.

@MarkEWaite
Copy link
Contributor

@MarkEWaite @gounthar, Can you please review & tell the changes that needs to be done further, I am ready to update & implement them.

I won't be able to review the proposed change in the near future. Other tasks are higher priority for me.

@PratikMane0112
Copy link
Contributor Author

@MarkEWaite @gounthar, Can you please review & tell the changes that needs to be done further, I am ready to update & implement them.

I won't be able to review the proposed change in the near future. Other tasks are higher priority for me.

It's fine, I respect your time sir

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.

2 participants