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-73136] Adapt HTTP Request tests for Jetty 12 (EE 8) #172

Merged
merged 37 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5fbce5a
Update POM for jetty 12 EE 8
BorisYaoA Jul 8, 2024
ca5ac76
remove usage of MultiPartFormInputStream as cleanup is managed by the…
BorisYaoA Jul 8, 2024
69ad01d
revert
BorisYaoA Jul 8, 2024
9630a67
revert clean
BorisYaoA Jul 8, 2024
f6cf304
update pom
BorisYaoA Jul 16, 2024
48b7573
remove MultiPartFormInputStream usage
BorisYaoA Jul 16, 2024
43fb80c
Fix import
BorisYaoA Aug 6, 2024
71ceac4
Fix multipartconfig attribut
BorisYaoA Aug 6, 2024
4707871
adapt Handle methods
BorisYaoA Aug 7, 2024
9e3afe3
fix bugs
BorisYaoA Aug 7, 2024
777ca39
fix bugs
BorisYaoA Aug 7, 2024
5444d1f
fix Part by using multipartInputStream
BorisYaoA Aug 7, 2024
27e309a
Refactoring
BorisYaoA Aug 7, 2024
84003ca
Merge remote-tracking branch 'origin/master' into Adapt_http_request_…
basil Aug 7, 2024
f77e660
Apply suggestions from code review
basil Aug 7, 2024
77883ff
Avoid static import
basil Aug 7, 2024
7f4297b
Consistent indentation
basil Aug 7, 2024
6b81635
Remove unused imports
basil Aug 7, 2024
5be3c57
Remove unnecessary import
basil Aug 7, 2024
115871b
Remove unnecessary use of IOException
basil Aug 7, 2024
1bb0d4f
A synchronous use case calls for a synchronous method rather than an …
basil Aug 7, 2024
b56dee0
Consistent variable naming
basil Aug 7, 2024
a649e41
Minimize diff with default branch
basil Aug 7, 2024
7e38874
Programming error
basil Aug 7, 2024
8a6cbce
Fix bug
basil Aug 7, 2024
35b2135
Propagate return values
basil Aug 7, 2024
3f371d8
Merge branch 'master' into Adapt_http_request_for_jetty12
basil Aug 7, 2024
9d5da5b
Stop ignoring parameter
basil Aug 7, 2024
c6b650d
Merge remote-tracking branch 'origin/Adapt_http_request_for_jetty12' …
BorisYaoA Aug 8, 2024
f0b3e05
fix Jetty 12 core handler multipart
olamy Aug 9, 2024
9535e5d
Merge pull request #1 from olamy/fix-jetty-12-multiparts
BorisYaoA Aug 9, 2024
396acb1
Merge remote-tracking branch 'origin/Adapt_http_request_for_jetty12' …
BorisYaoA Aug 9, 2024
50ae44e
fix unused imports and unused method after handling Parts
BorisYaoA Aug 9, 2024
841f057
Code cleanup
basil Aug 9, 2024
5aaff3e
Per the Javadoc:
basil Aug 9, 2024
7f84d24
Return value cleanup
basil Aug 9, 2024
4986697
Use `Response.writeError` where possible
basil Aug 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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