congrats Icon
New! Announcing our next generation AI code completions
Read here
Tabnine Logo
NetworkEventReporter
Code IndexAdd Tabnine to your IDE (free)

How to use
NetworkEventReporter
in
com.facebook.stetho.inspector.network

Best Java code snippets using com.facebook.stetho.inspector.network.NetworkEventReporter (Showing top 20 results out of 315)

origin: facebook/stetho

public boolean isStethoActive() {
 return mStethoHook.isEnabled();
}
origin: facebook/stetho

public StethoURLConnectionManagerImpl(@Nullable String friendlyName) {
 mRequestId = mStethoHook.nextRequestId();
 mFriendlyName = friendlyName;
}
origin: facebook/stetho

/**
 * @see StethoURLConnectionManager#httpExchangeFailed
 */
public void httpExchangeFailed(IOException ex) {
 throwIfNoConnection();
 if (isStethoActive()) {
  mStethoHook.httpExchangeFailed(getStethoRequestId(), ex.toString());
 }
}
origin: facebook/stetho

@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,
origin: facebook/stetho

    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());
origin: facebook/stetho

/**
 * @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;
}
origin: facebook/stetho

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;
}
origin: facebook/stetho

/**
 * @see StethoURLConnectionManager#postConnect
 */
public void postConnect() throws IOException {
 throwIfNoConnection();
 if (isStethoActive()) {
  if (mRequestBodyHelper != null && mRequestBodyHelper.hasBody()) {
   mRequestBodyHelper.reportDataSent();
  }
  mStethoHook.responseHeadersReceived(
    new URLConnectionInspectorResponse(
      getStethoRequestId(),
      mConnection));
 }
}
origin: facebook/stetho

 private void reportDataReceived() {
  mEventReporter.dataReceived(
    mRequestId,
    mBytesRead,
    mDecodedBytesRead >= 0 ? mDecodedBytesRead : mBytesRead);
 }
}
origin: facebook/stetho

public void reportDataSent() {
 throwIfNoBody();
 mEventReporter.dataSent(
   mRequestId,
   mDeflatedOutput.size(),
   (int)mDeflatingOutput.getCount());
}
origin: facebook/stetho

public void onEOF() {
 reportDataReceived();
 mEventReporter.responseReadFinished(mRequestId);
}
origin: facebook/stetho

@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,
origin: facebook/stetho

    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());
origin: facebook/stetho

final ByteArrayOutputStream capturedOutput = new ByteArrayOutputStream();
Mockito.when(
  mockEventReporter.interpretResponseStream(
    anyString(),
    anyString(),
origin: facebook/stetho

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;
}
origin: facebook/stetho

@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();
}
origin: facebook/stetho

.dataSent(
  anyString(),
  eq(decompressed.length),
origin: dabutaizha/juzimi

@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,
origin: facebook/stetho

@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();
}
origin: facebook/stetho

final ByteArrayOutputStream capturedOutput = new ByteArrayOutputStream();
Mockito.when(
  mockEventReporter.interpretResponseStream(
    anyString(),
    anyString(),
com.facebook.stetho.inspector.networkNetworkEventReporter

Javadoc

Interface that callers must invoke in order to supply data to the Network tab in the WebKit Inspector. For HTTP specific traffic, the following call flow must be met:
 
requestWillBeSent +---> responseHeadersReceived +---> interpretResponseStream 
|           |                 | 
|           `---> dataSent    | 
|                             | 
`-----------------------------`--------> httpExchangeFailed 

Note that #interpretResponseStream combined with DefaultResponseHandlerwill automatically invoke #dataReceived, #responseReadFailed and #responseReadFinished. If you use your own custom ResponseHandler you must be sure to invoke these methods manually.

For arbitrary sockets or explicitly for WebSockets, the following call flow must be met:

 
webSocketCreated +---> webSocketWillSendHandshakeRequest ----> webSocketHandshakeResponseReceived 
|                                                              | 
|                     ,----------------------+-----------------+--------, 
|                     v                      v                          | 
+---------> [ webSocketFrameSent | webSocketFrameReceived ] ---,        | 
|                     ^                      ^                 |        | 
|                     `----------------------+-----------------+        | 
|                                                              |        | 
`---------> webSocketClosed  

Explicitly worth nothing is that regular sockets in an Android app can be treated as WebSockets for the purpose of arbitrary socket inspection and can skip #webSocketWillSendHandshakeRequest and #webSocketHandshakeResponseReceivedwhich are only used for the WebSocket-specific HTTP upgrade.

Most used methods

  • interpretResponseStream
  • isEnabled
  • requestWillBeSent
    Indicates that a request is about to be sent, but has not yet been delivered over the wire.
  • responseHeadersReceived
  • nextRequestId
  • httpExchangeFailed
    Indicates that communication with the server has failed. You are expected to call this for any excep
  • dataReceived
    Indicates that raw data was received from the network.
  • dataSent
    Indicates that raw data was sent over the network. It is permissible to invoke this method just once
  • responseReadFinished
    Indicates that the response stream has been fully exhausted and the request is now complete. If you
  • responseReadFailed
    Indicates that there was a failure while reading from response stream. If you use #interpretResponse
  • webSocketClosed
    Socket has been closed for unknown reasons. Consider first invoking #webSocketFrameError even for st
  • webSocketCreated
    Invoked when a socket is created and implicitly being connected (but not necessarily connected yet).
  • webSocketClosed,
  • webSocketCreated,
  • webSocketFrameError,
  • webSocketFrameReceived,
  • webSocketFrameSent

Popular in Java

  • Reading from database using SQL prepared statement
  • setScale (BigDecimal)
  • scheduleAtFixedRate (Timer)
  • putExtra (Intent)
  • FileNotFoundException (java.io)
    Thrown when a file specified by a program cannot be found.
  • Enumeration (java.util)
    A legacy iteration interface.New code should use Iterator instead. Iterator replaces the enumeration
  • Vector (java.util)
    Vector is an implementation of List, backed by an array and synchronized. All optional operations in
  • Semaphore (java.util.concurrent)
    A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each #acquire blocks if
  • JTable (javax.swing)
  • SAXParseException (org.xml.sax)
    Encapsulate an XML parse error or warning.> This module, both source code and documentation, is in t
  • Top 17 Free Sublime Text Plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now