|
| 1 | +/* |
| 2 | + * ==================================================================== |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * ==================================================================== |
| 20 | + * |
| 21 | + * This software consists of voluntary contributions made by many |
| 22 | + * individuals on behalf of the Apache Software Foundation. For more |
| 23 | + * information on the Apache Software Foundation, please see |
| 24 | + * <http://www.apache.org/>. |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +package org.apache.hc.client5.http.impl.classic; |
| 29 | + |
| 30 | +import static org.mockito.ArgumentMatchers.any; |
| 31 | +import static org.mockito.Mockito.never; |
| 32 | + |
| 33 | +import java.io.ByteArrayOutputStream; |
| 34 | +import java.io.IOException; |
| 35 | + |
| 36 | +import org.apache.hc.core5.http.ClassicHttpRequest; |
| 37 | +import org.apache.hc.core5.http.HttpEntity; |
| 38 | +import org.junit.jupiter.api.Assertions; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.mockito.ArgumentCaptor; |
| 41 | +import org.mockito.Mockito; |
| 42 | + |
| 43 | +class RequestEntityProxyTest { |
| 44 | + |
| 45 | + @Test |
| 46 | + void testEnhanceWrapsNonRepeatableEntity() { |
| 47 | + final HttpEntity entity = Mockito.mock(HttpEntity.class); |
| 48 | + Mockito.when(entity.isRepeatable()).thenReturn(false); |
| 49 | + |
| 50 | + final ClassicHttpRequest request = Mockito.mock(ClassicHttpRequest.class); |
| 51 | + Mockito.when(request.getEntity()).thenReturn(entity); |
| 52 | + |
| 53 | + RequestEntityProxy.enhance(request); |
| 54 | + |
| 55 | + final ArgumentCaptor<HttpEntity> captor = ArgumentCaptor.forClass(HttpEntity.class); |
| 56 | + Mockito.verify(request).setEntity(captor.capture()); |
| 57 | + final HttpEntity proxy = captor.getValue(); |
| 58 | + |
| 59 | + Assertions.assertInstanceOf(RequestEntityProxy.class, proxy, "Entity should be wrapped as RequestEntityProxy"); |
| 60 | + Assertions.assertSame(entity, ((RequestEntityProxy) proxy).getOriginal(), "The proxy should wrap the original entity"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testEnhanceDoesNotWrapRepeatableEntity() { |
| 65 | + final HttpEntity entity = Mockito.mock(HttpEntity.class); |
| 66 | + Mockito.when(entity.isRepeatable()).thenReturn(true); |
| 67 | + |
| 68 | + final ClassicHttpRequest request = Mockito.mock(ClassicHttpRequest.class); |
| 69 | + Mockito.when(request.getEntity()).thenReturn(entity); |
| 70 | + |
| 71 | + RequestEntityProxy.enhance(request); |
| 72 | + |
| 73 | + Mockito.verify(request, never()).setEntity(any(HttpEntity.class)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testIsRepeatableBehavior() throws IOException { |
| 78 | + final HttpEntity entity = Mockito.mock(HttpEntity.class); |
| 79 | + Mockito.when(entity.isRepeatable()).thenReturn(false); |
| 80 | + |
| 81 | + final RequestEntityProxy proxy = new RequestEntityProxy(entity); |
| 82 | + |
| 83 | + Assertions.assertTrue(proxy.isRepeatable(), "Proxy should be repeatable before consumption"); |
| 84 | + |
| 85 | + final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 86 | + proxy.writeTo(out); |
| 87 | + |
| 88 | + Assertions.assertFalse(proxy.isRepeatable(), "Proxy should not be repeatable after consumption"); |
| 89 | + } |
| 90 | +} |
0 commit comments