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

[JENKINS-75164] Fix Custom headers with special characters in their names #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PratikMane0112
Copy link
Contributor

Fixed JIRA Issue 75164

Changes Done

In HttpRequest.java:

  • Introduced logic to handle customHeaders as either a List of HttpRequestNameValuePair objects or a List of Maps with name and value keys.
  • If the customHeaders is provided as a List of Maps, it is converted into a list of HttpRequestNameValuePair objects.
  • If it’s provided as a Map, it is converted into a List of HttpRequestNameValuePair using the Map entries.
  • Previous method is commented

In HttpRequestStep.java:

  • Added the necessary imports to handle the new Map and List formats for customHeaders
  • Implemented the @DataBoundSetter method to properly set customHeaders and handle conversion from List of Maps or - - - Map to a List of HttpRequestNameValuePair objects.
  • Previous method is commented

Testing done

On local jenkins controller with mvn hpi:run command

  • pipelines tested -

1] before & after build successfully on jenkins controller

pipeline {
    agent any
    stages {
        stage('Test HTTP Request with Custom Headers') {
            steps {
                script {
                    echo "Starting HTTP Request with custom headers"

                    def response = httpRequest(
                        url: 'https://httpbin.org/headers',
                        customHeaders: [[name: 'foo', value: 'bar']]
                    )
                    
                    echo "HTTP Response Content: ${response.content}"
                }
            }
        }
    }
}
  • build

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/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test HTTP Request with Custom Headers)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Starting HTTP Request with custom headers
[Pipeline] httpRequest
HttpMethod: GET
URL: https://httpbin.org/headers
foo: bar
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
HTTP Response Content: {
  "headers": {
    "Accept-Encoding": "gzip,deflate", 
    "Foo": "bar", 
    "Host": "httpbin.org", 
    "User-Agent": "Apache-HttpClient/4.5.14 (Java/17.0.13)", 
    "X-Amzn-Trace-Id": "Root=1-679b5aca-20f9da5c67f49f647b226682"
  }
}

[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

2] before build failed & after build successfull on jenkins controller

pipeline {
    agent any
    stages {
        stage('Test HTTP Request with Groovy Map Syntax') {
            steps {
                script {
                    echo "Starting HTTP Request with Groovy Map Syntax"

                    def headerName1 = 'my-custom-header-with-hyphen-separator'
                    def varName1 = 'abc'
                    def headerName2 = 'another-header'
                    def varName2 = 'xyz'

                    // Create a map of headers
                    def headers = [
                        "${headerName1}": "${varName1}",
                        "${headerName2}": "${varName2}"
                    ]

                    // Make an HTTP GET request with these custom headers
                    def response = httpRequest(
                        url: 'https://httpbin.org/headers', // Echoes back the headers received
                        customHeaders: headers
                    )

                    // Log the response content
                    echo "HTTP Response Content: ${response.content}"
                }
            }
        }
    }
}
  • build
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/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test HTTP Request with Groovy Map Syntax)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Starting HTTP Request with Groovy Map Syntax
[Pipeline] httpRequest
HttpMethod: GET
URL: https://httpbin.org/headers
my-custom-header-with-hyphen-separator: abc
another-header: xyz
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
HTTP Response Content: {
  "headers": {
    "Accept-Encoding": "gzip,deflate", 
    "Another-Header": "xyz", 
    "Host": "httpbin.org", 
    "My-Custom-Header-With-Hyphen-Separator": "abc", 
    "User-Agent": "Apache-HttpClient/4.5.14 (Java/17.0.13)", 
    "X-Amzn-Trace-Id": "Root=1-679b5cb8-1158e2f653e341c53b551fb5"
  }
}

[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

  • Currently test case not done for new updated code, only testes it on jenkins controller.

  • Let me know if any changes needs to be done, please also correct me if I went wrong somewhere

@PratikMane0112 PratikMane0112 requested a review from a team as a code owner January 30, 2025 11:38
@PratikMane0112
Copy link
Contributor Author

@MarkEWaite sir, I will restart from scratch before it can you please comment on this. It will help me to avoid from any wrong path as I done before

@MarkEWaite
Copy link
Contributor

@MarkEWaite sir, I will restart from scratch before it can you please comment on this. It will help me to avoid from any wrong path as I done before

@PratikMane0112 I'm in the middle of a business trip and won't be able to comment for a while.

@PratikMane0112
Copy link
Contributor Author

@MarkEWaite sir, I will restart from scratch before it can you please comment on this. It will help me to avoid from any wrong path as I done before

@PratikMane0112 I'm in the middle of a business trip and won't be able to comment for a while.

So sorry sir, I was not aware about it.

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