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 13 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>2244.v77428109fd1f</jenkins-test-harness.version>
basil marked this conversation as resolved.
Show resolved Hide resolved
<maven.compiler.release>17</maven.compiler.release>
basil marked this conversation as resolved.
Show resolved Hide resolved
</properties>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.ExecutionException;

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 +637,12 @@ 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, ExecutionException, InterruptedException {
assertEquals("POST", request.getMethod());

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

Expand Down
32 changes: 21 additions & 11 deletions src/test/java/jenkins/plugins/http_request/HttpRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
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.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,23 +770,28 @@ 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 Exception {
Fields allParameters = Request.getParameters(request);
String username = allParameters.getValue(paramUsername);
String password = allParameters.getValue(paramPassword);

if (!username.equals(valueUsername) || !password.equals(valuePassword)) {
response.setStatus(401);
return;
return false;
}
response.addCookie(new Cookie(sessionName, "ok"));
okAllIsWell(response);
HttpCookie cookie = HttpCookie.build(sessionName, "ok")
.build();
Response.addCookie(response, cookie);
okAllIsWell(response, callback);
return true;
}
});
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) throws IOException {
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;
Expand All @@ -791,9 +800,10 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht

if (!jsessionValue.equals("ok")) {
response.setStatus(401);
return;
return false;
}
okAllIsWell(response);
okAllIsWell(response, callback);
return true;
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package jenkins.plugins.http_request;

import java.io.BufferedReader;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

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.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 +36,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 +93,33 @@ 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 Exception {
doHandle(request, response, callback);
return true;
}

String requestBody(HttpServletRequest request) throws IOException {
try (BufferedReader reader = request.getReader()) {
return IOUtils.toString(reader);
}
String requestBody(Request request) throws IOException, ExecutionException, InterruptedException {
CompletableFuture<String> completable = Content.Source.asStringAsync(request, UTF_8);
return completable.get();
}

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

void okText(HttpServletResponse response, String body) throws IOException {
body(response, HttpServletResponse.SC_OK, ContentType.TEXT_PLAIN, body);
void okText(Response response, String body, Callback callback) throws IOException {
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() : "");
void body(Response response, int status, ContentType contentType, String body, Callback callback) throws IOException {
response.getHeaders().put(String.valueOf(contentType), "text/plain; charset=UTF-8");
response.setStatus(status);
response.getWriter().append(body);
Content.Sink.write(response, true, body, callback);
}

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

private static final class ServerRunning {
Expand All @@ -134,17 +137,19 @@ 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;
handler.handle(request, response, callback);
return true;
}
}

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