DefaultPacketSender implements PacketSender { public boolean send(Packet packet) { HttpURLConnection urlConnection = null; try { urlConnection = (HttpURLConnection) new URL(packet.getTargetURL()).openConnection(); Timber.tag(TAG).v("Connection is open to %s", urlConnection.getURL().toExternalForm()); Timber.tag(TAG).v("Sending: %s", packet); urlConnection.setConnectTimeout((int) mTimeout); urlConnection.setReadTimeout((int) mTimeout); if (packet.getPostData() != null) { urlConnection.setDoOutput(true); urlConnection.setRequestProperty("Content-Type", "application/json"); urlConnection.setRequestProperty("charset", "utf-8"); final String toPost = packet.getPostData().toString(); if (mGzip) { urlConnection.addRequestProperty("Content-Encoding", "gzip"); ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); GZIPOutputStream gzipStream = null; try { gzipStream = new GZIPOutputStream(byteArrayOS); gzipStream.write(toPost.getBytes(Charset.forName("UTF8"))); } finally { if (gzipStream != null) gzipStream.close(); } OutputStream outputStream = null; try { outputStream = urlConnection.getOutputStream(); outputStream.write(byteArrayOS.toByteArray()); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { Timber.tag(TAG).d(e, "Failed to close output stream after writing gzipped POST data."); } } } } else { BufferedWriter writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8")); writer.write(toPost); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { Timber.tag(TAG).d(e, "Failed to close output stream after writing POST data."); } } } } } else { urlConnection.setDoOutput(false); } int statusCode = urlConnection.getResponseCode(); Timber.tag(TAG).v("Transmission finished (code=%d).", statusCode); final boolean successful = checkResponseCode(statusCode); if (successful) { InputStream is = urlConnection.getInputStream(); if (is != null) { try { is.close(); } catch (IOException e) { Timber.tag(TAG).d(e, "Failed to close the error stream."); } } } else { final StringBuilder errorReason = new StringBuilder(); BufferedReader errorReader = null; try { errorReader = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream())); String line; while ((line = errorReader.readLine()) != null) errorReason.append(line); } finally { if (errorReader != null) { try { errorReader.close(); } catch (IOException e) { Timber.tag(TAG).d(e, "Failed to close the error stream."); } } } Timber.tag(TAG).w("Transmission failed (code=%d, reason=%s)", statusCode, errorReason.toString()); } return successful; } catch (Exception e) { Timber.tag(TAG).e(e, "Transmission failed unexpectedly."); return false; } finally { if (urlConnection != null) urlConnection.disconnect(); } }  boolean send(Packet packet); @Override void setTimeout(long timeout); @Override void setGzipData(boolean gzip);  }
@Test public void testDispatch() throws Exception { mMockWebServer.start(); Packet packet = mock(Packet.class); when(packet.getTargetURL()).thenReturn(mMockWebServer.url("/").toString()); JSONObject jsonObject = new JSONObject(); jsonObject.put("key", "value"); when(packet.getPostData()).thenReturn(jsonObject); mMockWebServer.enqueue(new MockResponse()); mDefaultPacketSender.send(packet); final RecordedRequest recordedRequest = mMockWebServer.takeRequest(); assertThat(recordedRequest, is(not(nullValue()))); String body = recordedRequest.getBody().readUtf8(); assertThat(jsonObject.toString(), is(body)); assertThat(recordedRequest.getHeader("Content-Encoding"), is((nullValue()))); }