|
1 | 1 | /* |
2 | | - * Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials are made available under the |
5 | 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
|
16 | 16 |
|
17 | 17 | package org.glassfish.jersey.logging; |
18 | 18 |
|
| 19 | +import org.mockito.stubbing.Answer; |
| 20 | + |
19 | 21 | import javax.ws.rs.core.MediaType; |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.nio.charset.StandardCharsets; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Random; |
20 | 27 |
|
21 | 28 | import org.junit.Test; |
22 | 29 | import static org.glassfish.jersey.logging.LoggingFeature.Verbosity.HEADERS_ONLY; |
23 | 30 | import static org.glassfish.jersey.logging.LoggingFeature.Verbosity.PAYLOAD_ANY; |
24 | 31 | import static org.glassfish.jersey.logging.LoggingFeature.Verbosity.PAYLOAD_TEXT; |
| 32 | +import static org.junit.Assert.assertEquals; |
25 | 33 | import static org.junit.Assert.assertFalse; |
26 | 34 | import static org.junit.Assert.assertTrue; |
| 35 | +import static org.mockito.Matchers.any; |
| 36 | +import static org.mockito.Matchers.eq; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.verify; |
| 39 | +import static org.mockito.Mockito.when; |
27 | 40 |
|
28 | 41 | import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE; |
29 | 42 | import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE; |
@@ -106,4 +119,67 @@ public void testVerbosityHeadersPrintBinaryEntity() { |
106 | 119 | assertFalse(LoggingInterceptor.printEntity(HEADERS_ONLY, APPLICATION_OCTET_STREAM_TYPE)); |
107 | 120 | } |
108 | 121 |
|
| 122 | + // |
| 123 | + // logInboundEntity |
| 124 | + // |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testLogInboundEntityMockedStream() throws Exception { |
| 128 | + int maxEntitySize = 20; |
| 129 | + LoggingInterceptor loggingInterceptor = new LoggingInterceptor(null, null, null, maxEntitySize) {}; |
| 130 | + |
| 131 | + StringBuilder buffer = new StringBuilder(); |
| 132 | + InputStream stream = mock(InputStream.class); |
| 133 | + when(stream.markSupported()).thenReturn(true); |
| 134 | + |
| 135 | + when(stream.read(any(), eq(0), eq(maxEntitySize + 1))) |
| 136 | + .thenAnswer(chunk(4, 'a')); |
| 137 | + when(stream.read(any(), eq(4), eq(maxEntitySize + 1 - 4))) |
| 138 | + .thenAnswer(chunk(3, 'b')); |
| 139 | + when(stream.read(any(), eq(7), eq(maxEntitySize + 1 - 7))) |
| 140 | + .thenAnswer(chunk(5, 'c')); |
| 141 | + when(stream.read(any(), eq(12), eq(maxEntitySize + 1 - 12))) |
| 142 | + .thenReturn(-1); |
| 143 | + |
| 144 | + loggingInterceptor.logInboundEntity(buffer, stream, StandardCharsets.UTF_8); |
| 145 | + |
| 146 | + assertEquals("aaaabbbccccc\n", buffer.toString()); |
| 147 | + verify(stream).mark(maxEntitySize + 1); |
| 148 | + verify(stream).reset(); |
| 149 | + } |
| 150 | + |
| 151 | + private Answer<?> chunk(int size, char filler) { |
| 152 | + return invocation -> { |
| 153 | + byte[] buf = invocation.getArgumentAt(0, byte[].class); |
| 154 | + int offset = invocation.getArgumentAt(1, Integer.class); |
| 155 | + Arrays.fill(buf, offset, offset + size, (byte) filler); |
| 156 | + return size; |
| 157 | + }; |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testLogInboundEntityRealStream() throws Exception { |
| 162 | + int maxEntitySize = 2000; |
| 163 | + String inputString = getRandomString(maxEntitySize * 2); |
| 164 | + |
| 165 | + LoggingInterceptor loggingInterceptor = new LoggingInterceptor(null, null, null, maxEntitySize) {}; |
| 166 | + StringBuilder buffer = new StringBuilder(); |
| 167 | + InputStream stream = new ByteArrayInputStream(inputString.getBytes()); |
| 168 | + |
| 169 | + loggingInterceptor.logInboundEntity(buffer, stream, StandardCharsets.UTF_8); |
| 170 | + |
| 171 | + assertEquals(inputString.substring(0, maxEntitySize) + "...more...\n", buffer.toString()); |
| 172 | + } |
| 173 | + |
| 174 | + private static String getRandomString(int length) { |
| 175 | + final String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 _"; |
| 176 | + StringBuilder result = new StringBuilder(); |
| 177 | + |
| 178 | + while (length > 0) { |
| 179 | + Random rand = new Random(); |
| 180 | + result.append(characters.charAt(rand.nextInt(characters.length()))); |
| 181 | + length--; |
| 182 | + } |
| 183 | + return result.toString(); |
| 184 | + } |
109 | 185 | } |
0 commit comments