public boolean isStethoActive() { return mStethoHook.isEnabled(); }
public StethoURLConnectionManagerImpl(@Nullable String friendlyName) { mRequestId = mStethoHook.nextRequestId(); mFriendlyName = friendlyName; }
/** * @see StethoURLConnectionManager#httpExchangeFailed */ public void httpExchangeFailed(IOException ex) { throwIfNoConnection(); if (isStethoActive()) { mStethoHook.httpExchangeFailed(getStethoRequestId(), ex.toString()); } }
@Override public Response intercept(Chain chain) throws IOException { String requestId = mEventReporter.nextRequestId(); if (mEventReporter.isEnabled()) { requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId); OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper); mEventReporter.requestWillBeSent(inspectorRequest); response = chain.proceed(request); } catch (IOException e) { if (mEventReporter.isEnabled()) { mEventReporter.httpExchangeFailed(requestId, e.toString()); if (mEventReporter.isEnabled()) { if (requestBodyHelper != null && requestBodyHelper.hasBody()) { requestBodyHelper.reportDataSent(); "did you use addInterceptor instead of addNetworkInterceptor?"); mEventReporter.responseHeadersReceived( new OkHttpInspectorResponse( requestId, responseStream = mEventReporter.interpretResponseStream( requestId, contentType != null ? contentType.toString() : null,
new SimpleTestChain(request, reply, mock(Connection.class))); inOrder.verify(mMockEventReporter).isEnabled(); inOrder.verify(mMockEventReporter) .requestWillBeSent(any(NetworkEventReporter.InspectorRequest.class)); inOrder.verify(mMockEventReporter) .dataSent( anyString(), eq(requestText.length()), eq(requestText.length())); inOrder.verify(mMockEventReporter) .responseHeadersReceived(any(NetworkEventReporter.InspectorResponse.class)); inOrder.verify(mMockEventReporter).dataReceived(anyString(), anyInt(), anyInt()); inOrder.verify(mMockEventReporter).responseReadFinished(anyString());
/** * @see StethoURLConnectionManager#interpretResponseStream */ public InputStream interpretResponseStream(@Nullable InputStream responseStream) { throwIfNoConnection(); if (isStethoActive()) { // Note that Content-Encoding is stripped out by HttpURLConnection on modern versions of // Android (fun fact, it's powered by okhttp) when decompression is handled transparently. // When this occurs, we will not be able to report the compressed size properly. Callers, // however, can disable this behaviour which will once again give us access to the raw // Content-Encoding so that we can handle it properly. responseStream = mStethoHook.interpretResponseStream( getStethoRequestId(), mConnection.getHeaderField("Content-Type"), mConnection.getHeaderField("Content-Encoding"), responseStream, new DefaultResponseHandler(mStethoHook, getStethoRequestId())); } return responseStream; }
private static AtomicReference<NetworkEventReporter.InspectorRequest> hookAlmostRealRequestWillBeSent( final NetworkEventReporter mockEventReporter) { final AtomicReference<NetworkEventReporter.InspectorRequest> capturedRequest = new AtomicReference<>(null); Mockito.doAnswer( new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); NetworkEventReporter.InspectorRequest request = (NetworkEventReporter.InspectorRequest)args[0]; capturedRequest.set(request); // Access the body, causing the body helper to perform decompression... request.body(); return null; } }) .when(mockEventReporter) .requestWillBeSent( any(NetworkEventReporter.InspectorRequest.class)); return capturedRequest; }
/** * @see StethoURLConnectionManager#postConnect */ public void postConnect() throws IOException { throwIfNoConnection(); if (isStethoActive()) { if (mRequestBodyHelper != null && mRequestBodyHelper.hasBody()) { mRequestBodyHelper.reportDataSent(); } mStethoHook.responseHeadersReceived( new URLConnectionInspectorResponse( getStethoRequestId(), mConnection)); } }
private void reportDataReceived() { mEventReporter.dataReceived( mRequestId, mBytesRead, mDecodedBytesRead >= 0 ? mDecodedBytesRead : mBytesRead); } }
public void reportDataSent() { throwIfNoBody(); mEventReporter.dataSent( mRequestId, mDeflatedOutput.size(), (int)mDeflatingOutput.getCount()); }
public void onEOF() { reportDataReceived(); mEventReporter.responseReadFinished(mRequestId); }
@Override public Response intercept(Chain chain) throws IOException { String requestId = mEventReporter.nextRequestId(); if (mEventReporter.isEnabled()) { requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId); OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper); mEventReporter.requestWillBeSent(inspectorRequest); response = chain.proceed(request); } catch (IOException e) { if (mEventReporter.isEnabled()) { mEventReporter.httpExchangeFailed(requestId, e.toString()); if (mEventReporter.isEnabled()) { if (requestBodyHelper != null && requestBodyHelper.hasBody()) { requestBodyHelper.reportDataSent(); "did you use addInterceptor instead of addNetworkInterceptor?"); mEventReporter.responseHeadersReceived( new OkHttpInspectorResponse( requestId, responseStream = mEventReporter.interpretResponseStream( requestId, contentType != null ? contentType.toString() : null,
new SimpleTestChain(request, reply, mock(Connection.class))); inOrder.verify(mMockEventReporter).isEnabled(); inOrder.verify(mMockEventReporter) .requestWillBeSent(any(NetworkEventReporter.InspectorRequest.class)); inOrder.verify(mMockEventReporter) .dataSent( anyString(), eq(requestText.length()), eq(requestText.length())); inOrder.verify(mMockEventReporter) .responseHeadersReceived(any(NetworkEventReporter.InspectorResponse.class)); inOrder.verify(mMockEventReporter).dataReceived(anyString(), anyInt(), anyInt()); inOrder.verify(mMockEventReporter).responseReadFinished(anyString());
final ByteArrayOutputStream capturedOutput = new ByteArrayOutputStream(); Mockito.when( mockEventReporter.interpretResponseStream( anyString(), anyString(),
private static AtomicReference<NetworkEventReporter.InspectorRequest> hookAlmostRealRequestWillBeSent( final NetworkEventReporter mockEventReporter) { final AtomicReference<NetworkEventReporter.InspectorRequest> capturedRequest = new AtomicReference<>(null); Mockito.doAnswer( new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); NetworkEventReporter.InspectorRequest request = (NetworkEventReporter.InspectorRequest)args[0]; capturedRequest.set(request); // Access the body, causing the body helper to perform decompression... request.body(); return null; } }) .when(mockEventReporter) .requestWillBeSent( any(NetworkEventReporter.InspectorRequest.class)); return capturedRequest; }
@Test public void testWithResponseCompression() throws IOException { ByteArrayOutputStream capturedOutput = hookAlmostRealInterpretResponseStream(mMockEventReporter); byte[] uncompressedData = repeat(".", 1024).getBytes(); byte[] compressedData = compress(uncompressedData); MockWebServer server = new MockWebServer(); server.start(); server.enqueue(new MockResponse() .setBody(new Buffer().write(compressedData)) .addHeader("Content-Encoding: gzip")); Request request = new Request.Builder() .url(server.url("/")) .build(); Response response = mClientWithInterceptor.newCall(request).execute(); // Verify that the final output and the caller both saw the uncompressed stream. assertArrayEquals(uncompressedData, response.body().bytes()); assertArrayEquals(uncompressedData, capturedOutput.toByteArray()); // And verify that the StethoInterceptor was able to see both. Mockito.verify(mMockEventReporter) .dataReceived( anyString(), eq(compressedData.length), eq(uncompressedData.length)); server.shutdown(); }
.dataSent( anyString(), eq(decompressed.length),
@Override public Response intercept(Chain chain) throws IOException { String requestId = mEventReporter.nextRequestId(); if (mEventReporter.isEnabled()) { requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId); OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper); mEventReporter.requestWillBeSent(inspectorRequest); response = chain.proceed(request); } catch (IOException e) { if (mEventReporter.isEnabled()) { mEventReporter.httpExchangeFailed(requestId, e.toString()); if (mEventReporter.isEnabled()) { if (requestBodyHelper != null && requestBodyHelper.hasBody()) { requestBodyHelper.reportDataSent(); "did you use addInterceptor instead of addNetworkInterceptor?"); mEventReporter.responseHeadersReceived( new OkHttpInspectorResponse( requestId, responseStream = mEventReporter.interpretResponseStream( requestId, contentType != null ? contentType.toString() : null,
@Before public void setUp() { PowerMockito.mockStatic(NetworkEventReporterImpl.class); mMockEventReporter = mock(NetworkEventReporter.class); Mockito.when(mMockEventReporter.isEnabled()).thenReturn(true); PowerMockito.when(NetworkEventReporterImpl.get()).thenReturn(mMockEventReporter); mInterceptor = new StethoInterceptor(); mClientWithInterceptor = new OkHttpClient.Builder() .addNetworkInterceptor(mInterceptor) .build(); }
final ByteArrayOutputStream capturedOutput = new ByteArrayOutputStream(); Mockito.when( mockEventReporter.interpretResponseStream( anyString(), anyString(),