Skip to content
Permalink
Browse files
[JENKINS-32376] Integration tests with client-based download and serv…
…er-based download.
  • Loading branch information
ikedam committed Jan 16, 2016
1 parent d5d4f7e commit a4e9b85239b415f3a66776f7d8e93111c1aabec4
Showing with 121 additions and 0 deletions.
  1. +121 −0 src/test/java/jp/ikedam/jenkins/plugins/updatesitesmanager/ManagedUpdateSiteJenkinsTest.java
@@ -33,12 +33,22 @@
import java.net.URL;
import java.security.GeneralSecurityException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jenkins.model.DownloadSettings;
import jenkins.model.Jenkins;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.jvnet.hudson.test.HudsonTestCase;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;
import org.mortbay.jetty.HttpConnection;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.handler.AbstractHandler;

import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
@@ -48,6 +58,17 @@
*/
public class ManagedUpdateSiteJenkinsTest extends HudsonTestCase
{
private Server server;
private URL baseUrl;

@Override
protected void tearDown() throws Exception {
if (server != null) {
server.stop();
}
super.tearDown();
}

static public class TestManagedUpdateSite extends ManagedUpdateSite
{
private static final long serialVersionUID = -6888318503867286760L;
@@ -104,9 +125,14 @@ public void testDoPostBack() throws URISyntaxException, IOException, GeneralSecu
// CrumbIssuer causes failures in POST request.
// I don't know why CrumbIssuer is enabled in a test environment...
CrumbIssuer crumb = Jenkins.getInstance().getCrumbIssuer();

// Jenkins >= 1.600 defaults to use server-based download.
// Ensure to use client-based download.
boolean isUseBrowser = DownloadSettings.get().isUseBrowser();
try
{
Jenkins.getInstance().setCrumbIssuer(null);
DownloadSettings.get().setUseBrowser(true);
String caCertificate = FileUtils.readFileToString(getResource("caCertificate.crt"));

TestManagedUpdateSite target = new TestManagedUpdateSite(
@@ -167,10 +193,105 @@ public void testDoPostBack() throws URISyntaxException, IOException, GeneralSecu
}
finally
{
DownloadSettings.get().setUseBrowser(isUseBrowser);
Jenkins.getInstance().setCrumbIssuer(crumb);
}
}

public void setUpWebServer() throws Exception {
server = new Server();
SocketConnector connector = new SocketConnector();
server.addConnector(connector);
server.setHandler(new AbstractHandler() {
@Override
public void handle(
String target, HttpServletRequest request,
HttpServletResponse response, int dispatch
) throws IOException, ServletException
{
String responseBody = null;
try {
responseBody = FileUtils.readFileToString(getResource(target), "UTF-8");
} catch(URISyntaxException e) {
HttpConnection.getCurrentConnection().getRequest().setHandled(true);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
if (responseBody != null) {
HttpConnection.getCurrentConnection().getRequest().setHandled(true);
response.setContentType("text/plain; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getOutputStream().write(responseBody.getBytes());
}
}
});
server.start();
baseUrl = new URL("http", "localhost", connector.getLocalPort(), "");
}

public void testDoCheckUpdateServer() throws Exception {
setUpWebServer();

// Ensure to use server-based download.
boolean isUseBrowser = DownloadSettings.get().isUseBrowser();
try
{
DownloadSettings.get().setUseBrowser(false);
String caCertificate = FileUtils.readFileToString(getResource("caCertificate.crt"));

TestManagedUpdateSite target = new TestManagedUpdateSite(
"test",
new URL(baseUrl, "update-center.json").toExternalForm(),
false,
null,
"test",
false
);
jenkins.getUpdateCenter().getSites().clear();
jenkins.getUpdateCenter().getSites().add(target);
jenkins.getUpdateCenter().save();

{
target.setCaCertificate(null);
HttpResponse rsp = jenkins.getPluginManager().doCheckUpdatesServer();
if (rsp instanceof FormValidation) {
assertEquals(
"Accessing update center with no certificate must fail",
FormValidation.Kind.ERROR,
((FormValidation)rsp).kind
);
}
}

{
target.setCaCertificate(caCertificate);
HttpResponse rsp = jenkins.getPluginManager().doCheckUpdatesServer();
if (rsp instanceof FormValidation) {
assertEquals(
"Accessing update center with a proper certificate must succeed",
FormValidation.Kind.OK,
((FormValidation)rsp).kind
);
}
}

{
target.setCaCertificate(null);
HttpResponse rsp = jenkins.getPluginManager().doCheckUpdatesServer();
if (rsp instanceof FormValidation) {
assertEquals(
"Accessing update center with no certificate must fail",
FormValidation.Kind.ERROR,
((FormValidation)rsp).kind
);
}
}
}
finally
{
DownloadSettings.get().setUseBrowser(isUseBrowser);
}
}

private ManagedUpdateSite.DescriptorImpl getDescriptor()
{
return (ManagedUpdateSite.DescriptorImpl)new ManagedUpdateSite(null, null, false, null, null, false).getDescriptor();

0 comments on commit a4e9b85

Please sign in to comment.