Created
January 31, 2013 05:20
-
-
Save swankjesse/4680456 to your computer and use it in GitHub Desktop.
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
import com.google.mockwebserver.Dispatcher; | |
import com.google.mockwebserver.MockResponse; | |
import com.google.mockwebserver.MockWebServer; | |
import com.google.mockwebserver.RecordedRequest; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
public class Redirector { | |
public static void main(String[] args) throws IOException { | |
MockWebServer server = new MockWebServer(); | |
server.setDispatcher(new Dispatcher() { | |
@Override | |
public MockResponse dispatch(RecordedRequest request) throws InterruptedException { | |
try { | |
int i = Integer.parseInt(request.getPath().substring(1)); | |
return new MockResponse() | |
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) | |
.setBody("Redirected " + i + " times") | |
.addHeader("Location: /" + (i + 1)); | |
} catch (NumberFormatException e) { | |
return new MockResponse() | |
.setBody("<a href=/0>click here</a>"); | |
} | |
} | |
}); | |
server.play(); | |
System.out.println(server.getUrl("/")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment