|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +package org.glassfish.jersey.tests.e2e.entity; |
| 19 | + |
| 20 | +import org.glassfish.jersey.client.ChunkedInput; |
| 21 | +import org.glassfish.jersey.server.ResourceConfig; |
| 22 | +import org.glassfish.jersey.test.JerseyTest; |
| 23 | +import org.hamcrest.MatcherAssert; |
| 24 | +import org.hamcrest.Matchers; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import javax.ws.rs.GET; |
| 28 | +import javax.ws.rs.Path; |
| 29 | +import javax.ws.rs.WebApplicationException; |
| 30 | +import javax.ws.rs.client.ClientBuilder; |
| 31 | +import javax.ws.rs.client.ClientRequestContext; |
| 32 | +import javax.ws.rs.client.ClientRequestFilter; |
| 33 | +import javax.ws.rs.core.Application; |
| 34 | +import javax.ws.rs.core.GenericType; |
| 35 | +import javax.ws.rs.core.Response; |
| 36 | +import javax.ws.rs.ext.ReaderInterceptor; |
| 37 | +import javax.ws.rs.ext.ReaderInterceptorContext; |
| 38 | +import java.io.ByteArrayInputStream; |
| 39 | +import java.io.IOException; |
| 40 | +import java.io.InputStream; |
| 41 | +import java.lang.reflect.ParameterizedType; |
| 42 | +import java.lang.reflect.Type; |
| 43 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 44 | + |
| 45 | +public class ChunkedInputReaderTest extends JerseyTest { |
| 46 | + |
| 47 | + @Path("/") |
| 48 | + public static class ChunkedInputReaderTestResource { |
| 49 | + @GET |
| 50 | + public String get() { |
| 51 | + return "To_be_replaced_by_client_reader"; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected Application configure() { |
| 57 | + return new ResourceConfig(ChunkedInputReaderTestResource.class); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testChunkedInputStreamIsClosed() { |
| 62 | + AtomicBoolean closed = new AtomicBoolean(false); |
| 63 | + InputStream inputStream = new ByteArrayInputStream("TEST".getBytes()) { |
| 64 | + @Override |
| 65 | + public void close() throws IOException { |
| 66 | + closed.set(true); |
| 67 | + super.close(); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + final GenericType<ChunkedInput<String>> chunkedInputGenericType = new GenericType(new ParameterizedType() { |
| 72 | + @Override |
| 73 | + public Type[] getActualTypeArguments() { |
| 74 | + return new Type[]{String.class}; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Type getRawType() { |
| 79 | + return ChunkedInput.class; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Type getOwnerType() { |
| 84 | + return ChunkedInput.class; |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + |
| 89 | + ChunkedInput<String> response = target().register(new ReaderInterceptor() { |
| 90 | + @Override |
| 91 | + public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException { |
| 92 | + context.setInputStream(inputStream); |
| 93 | + return context.proceed(); |
| 94 | + }; |
| 95 | + }).request().get(chunkedInputGenericType); |
| 96 | + MatcherAssert.assertThat(response.read(), Matchers.is("TEST")); |
| 97 | + response.close(); |
| 98 | + MatcherAssert.assertThat(closed.get(), Matchers.is(true)); |
| 99 | + } |
| 100 | +} |
0 commit comments