Sniffy - capture network traffic

Capture all traffic from JVM-based apps without privileged access

Capture network traffic sent and received by JVM-based applications

    @Test
    public void testCaptureTraffic() throws Exception {

        try (Spy spy = Sniffy.spy(
                SpyConfiguration.builder().captureNetworkTraffic(true).build()) // <1>
        ) {

            performSocketOperation(); // <2>

            Map> networkTraffic = spy.getNetworkTraffic( // <3>
                    Threads.ANY, // <4>
                    AddressMatchers.anyAddressMatcher(), // <5>
                    GroupingOptions.builder().
                            groupByThread(false). // <6>
                            groupByStackTrace(false). // <7>
                            groupByConnection(false). // <8>
                            build()
            );

            assertEquals(1, networkTraffic.size());

            for (Map.Entry> entry : networkTraffic.entrySet()) {

                SocketMetaData socketMetaData = entry.getKey(); // <9>

                Protocol protocol = socketMetaData.getProtocol(); // say TCP
                String hostName = socketMetaData.getAddress().getHostName(); // say "hostname.acme.com"
                int port = socketMetaData.getAddress().getPort(); // say 443

                List networkPackets = entry.getValue(); // <10>

                assertArrayEquals(REQUEST, networkPackets.get(0).getBytes());
                assertTrue(networkPackets.get(0).isSent());

                assertArrayEquals(RESPONSE, networkPackets.get(1).getBytes());
                assertFalse(networkPackets.get(1).isSent());

            }

        }

    }