forked from adamfisk/LittleProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test for invalid server responses
- Loading branch information
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
src/test/java/org/littleshoot/proxy/test/ServerErrorTest.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,94 @@ | ||
package org.littleshoot.proxy.test; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.littleshoot.proxy.HttpProxyServer; | ||
import org.littleshoot.proxy.impl.DefaultHttpProxyServer; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ServerErrorTest { | ||
private HttpProxyServer proxyServer; | ||
|
||
@Test | ||
public void testInvalidServerResponse() throws IOException { | ||
proxyServer = DefaultHttpProxyServer.bootstrap() | ||
.withPort(0) | ||
.start(); | ||
|
||
// we have to create our own socket here, since any proper http server (jetty, mockserver, etc.) won't allow us to | ||
// send invalid responses. | ||
try (ServerSocket socket = createServerWithBadResponse()) { | ||
org.apache.http.HttpResponse response = HttpClientUtil.performHttpGet("http://localhost:" + socket.getLocalPort(), proxyServer); | ||
|
||
assertEquals("Expected to receive a 502 Bad Gateway after server responded with invalid response", 502, response.getStatusLine().getStatusCode()); | ||
} | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
if (proxyServer != null) { | ||
proxyServer.abort(); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a ServerSocket that will read an HTTP request and response with an invalid response. | ||
* NOTE: the ServerSocket must be closed after the response is consumed. | ||
*/ | ||
private static ServerSocket createServerWithBadResponse() { | ||
final ServerSocket serverSocket; | ||
try { | ||
serverSocket = new ServerSocket(0); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
Runnable server = new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
Socket socket = serverSocket.accept(); | ||
|
||
try (PrintWriter out = new PrintWriter(socket.getOutputStream(), true); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { | ||
while (!in.readLine().isEmpty()) { | ||
// read the request up to the double-CRLF | ||
} | ||
|
||
// write a a response with an invalid HTTP version | ||
out.write("HTTP/1.12312312312312411231231231 200 OK\r\n" + | ||
"Connection: close\r\n" + | ||
"Content-Length: 0\r\n" + | ||
"\r\n"); | ||
out.flush(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}; | ||
|
||
// start the server in a separate thread | ||
Thread serverThread = new Thread(server); | ||
serverThread.setDaemon(true); | ||
serverThread.start(); | ||
|
||
// wait for the server to start | ||
try { | ||
Thread.sleep(500); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return serverSocket; | ||
} | ||
|
||
} |