Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[JENKINS-24671] Add username making the HTTP request, and add the ref…
…erer http header.
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/test/java/com/cloudbees/jenkins/support/slowrequest/InflightRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.cloudbees.jenkins.support.slowrequest; | ||
|
||
import com.gargoylesoftware.htmlunit.*; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper; | ||
import hudson.model.RootAction; | ||
import hudson.util.HttpResponses; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.http.HttpRequest; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Bug; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.TestExtension; | ||
import org.kohsuke.stapler.HttpResponse; | ||
import org.kohsuke.stapler.Stapler; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
import org.kohsuke.stapler.StaplerResponse; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletRequestWrapper; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.sql.ClientInfoStatus; | ||
import java.util.ResourceBundle; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
/** | ||
* @author schristou88 | ||
*/ | ||
public class InflightRequestTest { | ||
@Rule public JenkinsRule r = new JenkinsRule(); | ||
|
||
@Test | ||
@Bug(24671) | ||
public void verifyUsernameInflightRequest() throws Exception { | ||
r.jenkins.setSecurityRealm(r.createDummySecurityRealm()); | ||
JenkinsRule.WebClient webClient = r.createWebClient().login("bob", "bob"); | ||
MockSlowURLCall.seconds = 5; | ||
webClient.goTo("mockSlowURLCall/submit"); | ||
InflightRequest request = MockSlowURLCall.request; | ||
assertNotNull(request); | ||
assertEquals("bob", request.userName); | ||
assertEquals(webClient.getContextPath() + "mockSlowURLCall/submit", request.url); | ||
} | ||
|
||
@Test | ||
@Bug(24671) | ||
public void verifyRefererHeaderFromInflightRequest() throws Exception { | ||
JenkinsRule.WebClient webClient = r.createWebClient(); | ||
|
||
URL refererUrl = new URL(webClient.getContextPath() + "mockSlowURLCall/submitReferer"); | ||
|
||
((HtmlPage) webClient.getPage(refererUrl)).getHtmlElementById("link").click(); | ||
|
||
InflightRequest request = MockSlowURLCall.request; | ||
assertEquals(refererUrl.toString(), request.referer); | ||
} | ||
|
||
@TestExtension | ||
public static class MockSlowURLCall implements RootAction { | ||
public static int seconds; | ||
public static InflightRequest request; | ||
|
||
public HttpResponse doSubmitReferer(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException { | ||
String literalHTML = "<html>" + | ||
" <body>" + | ||
" <a href='" + req.getContextPath() + "/mockSlowURLCall/submit"+ "' id='link'>link</a>" + | ||
" </body>" + | ||
"</html>"; | ||
return HttpResponses.html(literalHTML); | ||
} | ||
|
||
public HttpResponse doSubmit(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException { | ||
request = new InflightRequest(req); | ||
return HttpResponses.redirectTo(".."); | ||
} | ||
|
||
public String getIconFileName() { | ||
return ""; | ||
} | ||
|
||
public String getDisplayName() { | ||
return "MockSlowURLCall"; | ||
} | ||
|
||
public String getUrlName() { | ||
return "mockSlowURLCall"; | ||
} | ||
} | ||
} |