-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
morgan.peng
committed
Nov 28, 2023
1 parent
be07f5c
commit 7e55f5e
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
framework/src/test/java/org/tron/common/jetty/JettyServerTest.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,63 @@ | ||
package org.tron.common.jetty; | ||
|
||
import java.net.URI; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.DefaultHttpClient; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.servlet.DefaultServlet; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.servlet.ServletHolder; | ||
import org.junit.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
@Slf4j | ||
public class JettyServerTest { | ||
private static Server server; | ||
private static URI serverUri; | ||
|
||
@BeforeClass | ||
public static void startJetty() throws Exception { | ||
server = new Server(); | ||
ServerConnector connector = new ServerConnector(server); | ||
connector.setPort(0); | ||
server.addConnector(connector); | ||
|
||
ServletContextHandler context = new ServletContextHandler(); | ||
ServletHolder defaultServ = new ServletHolder("default", DefaultServlet.class); | ||
context.addServlet(defaultServ, "/"); | ||
server.setHandler(context); | ||
server.start(); | ||
String host = connector.getHost(); | ||
if (host == null) { | ||
host = "localhost"; | ||
} | ||
int port = connector.getLocalPort(); | ||
serverUri = new URI(String.format("http://%s:%d/", host, port)); | ||
} | ||
|
||
@AfterClass | ||
public static void stopJetty() { | ||
try { | ||
server.stop(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGet() throws Exception { | ||
HttpClient client = new DefaultHttpClient(); | ||
HttpGet request = new HttpGet(serverUri.resolve("/")); | ||
request.setHeader("Content-Length", "+450"); | ||
HttpResponse mockResponse = client.execute(request); | ||
Assert.assertTrue(mockResponse.getStatusLine().toString().contains( | ||
"400 Invalid Content-Length Value")); | ||
} | ||
|
||
} |