http-request-plugin icon indicating copy to clipboard operation
http-request-plugin copied to clipboard

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

Open PratikMane0112 opened this issue 8 months ago • 3 comments

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

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

PratikMane0112 avatar Apr 01 '25 10:04 PratikMane0112

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

PratikMane0112 avatar Apr 01 '25 13:04 PratikMane0112

@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.

MarkEWaite avatar Apr 01 '25 13:04 MarkEWaite

@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

PratikMane0112 avatar Apr 01 '25 13:04 PratikMane0112