use of okhttp3.mockwebserver.MockWebServer in project retrofit by square.
the class JsonAndXmlConverters method main.
public static void main(String... args) throws IOException {
MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse().setBody("{\"name\": \"Jason\"}"));
server.enqueue(new MockResponse().setBody("<user name=\"Eximel\"/>"));
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new QualifiedTypeConverterFactory(GsonConverterFactory.create(), SimpleXmlConverterFactory.create())).build();
Service service = retrofit.create(Service.class);
User user1 = service.exampleJson().execute().body();
System.out.println("User 1: " + user1.name);
User user2 = service.exampleXml().execute().body();
System.out.println("User 2: " + user2.name);
server.shutdown();
}
use of okhttp3.mockwebserver.MockWebServer in project retrofit by square.
the class JsonQueryParameters method main.
public static void main(String... args) throws IOException, InterruptedException {
MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse());
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new JsonStringConverterFactory(GsonConverterFactory.create())).build();
Service service = retrofit.create(Service.class);
Call<ResponseBody> call = service.example(new Filter("123"));
Response<ResponseBody> response = call.execute();
// TODO handle user response...
// Print the request path that the server saw to show the JSON query param:
RecordedRequest recordedRequest = server.takeRequest();
System.out.println(recordedRequest.getPath());
server.shutdown();
}
use of okhttp3.mockwebserver.MockWebServer in project okhttp by square.
the class URLConnectionTest method redirectWithProxySelector.
@Test
public void redirectWithProxySelector() throws Exception {
final List<URI> proxySelectionRequests = new ArrayList<>();
urlFactory.setClient(urlFactory.client().newBuilder().proxySelector(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
proxySelectionRequests.add(uri);
MockWebServer proxyServer = (uri.getPort() == server.getPort()) ? server : server2;
return Arrays.asList(proxyServer.toProxyAddress());
}
@Override
public void connectFailed(URI uri, SocketAddress address, IOException failure) {
throw new AssertionError();
}
}).build());
server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.url("/b").url().toString()).setBody("This page has moved!"));
assertContent("This is the 2nd server!", urlFactory.open(server.url("/a").url()));
assertEquals(Arrays.asList(server.url("/").url().toURI(), server2.url("/").url().toURI()), proxySelectionRequests);
}
use of okhttp3.mockwebserver.MockWebServer in project okhttp by square.
the class URLConnectionTest method writeTimeouts.
/** Confirm that an unacknowledged write times out. */
@Test
public void writeTimeouts() throws IOException {
MockWebServer server = new MockWebServer();
// Sockets on some platforms can have large buffers that mean writes do not block when
// required. These socket factories explicitly set the buffer sizes on sockets created.
final int SOCKET_BUFFER_SIZE = 4 * 1024;
server.setServerSocketFactory(new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {
@Override
protected ServerSocket configureServerSocket(ServerSocket serverSocket) throws IOException {
serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
return serverSocket;
}
});
urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
@Override
protected Socket configureSocket(Socket socket) throws IOException {
socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
return socket;
}
}).writeTimeout(500, TimeUnit.MILLISECONDS).build());
server.start();
server.enqueue(new MockResponse().throttleBody(1, 1, // Prevent the server from reading!
TimeUnit.SECONDS));
connection = urlFactory.open(server.url("/").url());
connection.setDoOutput(true);
connection.setChunkedStreamingMode(0);
OutputStream out = connection.getOutputStream();
try {
// 2 MiB.
byte[] data = new byte[2 * 1024 * 1024];
out.write(data);
fail();
} catch (SocketTimeoutException expected) {
}
}
use of okhttp3.mockwebserver.MockWebServer in project okhttp by square.
the class OAuthSessionFactory method start.
public void start() throws Exception {
if (mockWebServer != null)
throw new IllegalStateException();
mockWebServer = new MockWebServer();
mockWebServer.setDispatcher(this);
mockWebServer.start(slackApi.port);
}
Aggregations