Skip to content

Commit

Permalink
[JENKINS-73136] Adapt HTTP Request tests for Jetty 12 (EE 8) (#172)
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Lamy <[email protected]>
Co-authored-by: Basil Crow <[email protected]>
Co-authored-by: Olivier Lamy <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2024
1 parent 018c44c commit fa04893
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 170 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ THE SOFTWARE.
<spotbugs.failOnError>true</spotbugs.failOnError>
<spotbugs.threshold>Low</spotbugs.threshold>
<hpi.compatibleSinceVersion>1.16</hpi.compatibleSinceVersion>
<!-- TODO JENKINS-73339 until in parent POM -->
<jenkins-test-harness.version>2250.v03a_1295b_0a_30</jenkins-test-harness.version>
<maven.compiler.testRelease>17</maven.compiler.testRelease>
</properties>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import java.util.ArrayList;
import java.util.List;

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

import org.apache.commons.io.FilenameUtils;
import org.apache.http.entity.ContentType;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.Callback;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand Down Expand Up @@ -636,11 +636,11 @@ public void testPostBody() throws Exception {
//configure server
registerHandler("/doPostBody", HttpMode.POST, new SimpleHandler() {
@Override
void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
boolean doHandle(Request request, Response response, Callback callback) throws IOException {
assertEquals("POST", request.getMethod());

String body = requestBody(request);
body(response, HttpServletResponse.SC_OK, ContentType.TEXT_PLAIN, body);
return body(response, HttpStatus.OK_200, ContentType.TEXT_PLAIN, body, callback);
}
});

Expand Down Expand Up @@ -671,7 +671,7 @@ public void testFileUpload() throws Exception {
final File testFolder = folder.newFolder();
File uploadFile = File.createTempFile("upload", ".zip", testFolder);
String responseText = "File upload successful!";
registerFileUpload(testFolder, uploadFile, responseText);
registerFileUpload(uploadFile, responseText);

// Prepare HttpRequest
WorkflowJob proj = j.jenkins.createProject(WorkflowJob.class, "uploadFile");
Expand Down Expand Up @@ -700,13 +700,12 @@ public void testFileUpload() throws Exception {

@Test
public void testFormData() throws Exception {
final File testFolder = folder.newFolder();
File projectRoot = Paths.get("").toAbsolutePath().toFile();
String responseText = "File upload successful!";
String json = "{\"foo\": \"bar\"}";
File file1 = new File(projectRoot, "src/test/resources/testdata/readme.txt");
File file2 = new File(projectRoot, "src/test/resources/testdata/small.zip");
registerFormData(testFolder, json, file1, file2, responseText);
registerFormData(json, file1, file2, responseText);

// Let's upload these files and a JSON
String script = "node {\n"
Expand Down
46 changes: 28 additions & 18 deletions src/test/java/jenkins/plugins/http_request/HttpRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpResponse;
import org.eclipse.jetty.http.HttpCookie;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.Fields;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Rule;
Expand Down Expand Up @@ -766,34 +768,42 @@ public void testFormAuthentication() throws Exception {

registerHandler("/form-auth", HttpMode.POST, new SimpleHandler() {
@Override
void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
String username = request.getParameter(paramUsername);
String password = request.getParameter(paramPassword);
boolean doHandle(Request request, Response response, Callback callback) throws ServletException {
Fields parameters;
try {
parameters = Request.getParameters(request);
} catch (Exception e) {
throw new ServletException(e);
}
String username = parameters.getValue(paramUsername);
String password = parameters.getValue(paramPassword);

if (!username.equals(valueUsername) || !password.equals(valuePassword)) {
response.setStatus(401);
return;
Response.writeError(request, response, callback, HttpStatus.UNAUTHORIZED_401);
return true;
}
response.addCookie(new Cookie(sessionName, "ok"));
okAllIsWell(response);
HttpCookie cookie = HttpCookie.build(sessionName, "ok").build();
Response.addCookie(response, cookie);
return okAllIsWell(response, callback);
}
});
registerHandler("/test-auth", HttpMode.GET, new SimpleHandler() {
@Override
void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
boolean doHandle(Request request, Response response, Callback callback) {
String jsessionValue = "";
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
List<HttpCookie> cookies = Request.getCookies(request);
for (HttpCookie cookie : cookies) {
if (cookie.getName().equals(sessionName)) {
jsessionValue = cookie.getValue();
break;
}
}

if (!jsessionValue.equals("ok")) {
response.setStatus(401);
return;
Response.writeError(request, response, callback, HttpStatus.UNAUTHORIZED_401);
return true;
}
okAllIsWell(response);
return okAllIsWell(response, callback);
}
});

Expand Down Expand Up @@ -982,7 +992,7 @@ public void testFileUpload() throws Exception {
final File testFolder = folder.newFolder();
File uploadFile = File.createTempFile("upload", ".zip", testFolder);
String responseText = "File upload successful!";
registerFileUpload(testFolder, uploadFile, responseText);
registerFileUpload(uploadFile, responseText);

// Prepare HttpRequest
HttpRequest httpRequest = new HttpRequest(baseURL() + "/uploadFile");
Expand Down
57 changes: 33 additions & 24 deletions src/test/java/jenkins/plugins/http_request/HttpRequestTestBase.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package jenkins.plugins.http_request;

import java.io.BufferedReader;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.http.entity.ContentType;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.util.Callback;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
Expand All @@ -31,7 +40,6 @@
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import org.apache.commons.io.IOUtils;

/**
* @author Martin d'Anjou
Expand Down Expand Up @@ -89,34 +97,35 @@ public void cleanHandlers() {
}
}

public static abstract class SimpleHandler extends DefaultHandler {
public static abstract class SimpleHandler extends Handler.Abstract {
@Override
public final void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
doHandle(target, baseRequest, request, response);
baseRequest.setHandled(true);
public final boolean handle(Request request, Response response, Callback callback) throws IOException, ServletException {
return doHandle(request, response, callback);
}

String requestBody(HttpServletRequest request) throws IOException {
try (BufferedReader reader = request.getReader()) {
return IOUtils.toString(reader);
}
String requestBody(Request request) throws IOException {
return Content.Source.asString(request, StandardCharsets.UTF_8);
}

void okAllIsWell(HttpServletResponse response) throws IOException {
okText(response, ALL_IS_WELL);
boolean okAllIsWell(Response response, Callback callback) {
return okText(response, ALL_IS_WELL, callback);
}

void okText(HttpServletResponse response, String body) throws IOException {
body(response, HttpServletResponse.SC_OK, ContentType.TEXT_PLAIN, body);
boolean okText(Response response, String body, Callback callback) {
return body(response, HttpStatus.OK_200, ContentType.TEXT_PLAIN, body, callback);
}

void body(HttpServletResponse response, int status, ContentType contentType, String body) throws IOException {
response.setContentType(contentType != null ? contentType.toString() : "");
boolean body(Response response, int status, ContentType contentType, String body, Callback callback) {
assertThat(status, is(both(greaterThanOrEqualTo(200)).and(lessThan(300))));
if (contentType != null) {
response.getHeaders().add(HttpHeader.CONTENT_TYPE, contentType.toString());
}
response.setStatus(status);
response.getWriter().append(body);
Content.Sink.write(response, true, body, callback);
return true;
}

abstract void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException;
abstract boolean doHandle(Request request, Response response, Callback callback) throws IOException, ServletException;
}

private static final class ServerRunning {
Expand All @@ -134,17 +143,17 @@ private ServerRunning() throws Exception {
context.setContextPath("/");
context.setHandler(new DefaultHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
public boolean handle(Request request, Response response, Callback callback) throws Exception {
String target = request.getHttpURI().getPath();
Map<HttpMode, Handler> handlerByMethod = handlersByMethodByTarget.get(target);
if (handlerByMethod != null) {
Handler handler = handlerByMethod.get(HttpMode.valueOf(request.getMethod()));
if (handler != null) {
handler.handle(target, baseRequest, request, response);
return;
return handler.handle(request, response, callback);
}
}

super.handle(target, baseRequest, request, response);
return super.handle(request, response, callback);
}
});
server.setHandler(context);
Expand Down
Loading

0 comments on commit fa04893

Please sign in to comment.