|
| 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 | +package org.glassfish.jersey.media.sse; |
| 18 | + |
| 19 | +import org.glassfish.jersey.server.ResourceConfig; |
| 20 | +import org.glassfish.jersey.test.JerseyTest; |
| 21 | +import org.hamcrest.MatcherAssert; |
| 22 | +import org.hamcrest.Matchers; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import javax.ws.rs.BadRequestException; |
| 26 | +import javax.ws.rs.GET; |
| 27 | +import javax.ws.rs.InternalServerErrorException; |
| 28 | +import javax.ws.rs.NotFoundException; |
| 29 | +import javax.ws.rs.Path; |
| 30 | +import javax.ws.rs.Produces; |
| 31 | +import javax.ws.rs.WebApplicationException; |
| 32 | +import javax.ws.rs.client.WebTarget; |
| 33 | +import javax.ws.rs.core.Application; |
| 34 | +import javax.ws.rs.core.Context; |
| 35 | +import javax.ws.rs.sse.InboundSseEvent; |
| 36 | +import javax.ws.rs.sse.Sse; |
| 37 | +import javax.ws.rs.sse.SseEventSink; |
| 38 | +import javax.ws.rs.sse.SseEventSource; |
| 39 | +import java.util.concurrent.CountDownLatch; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | +import java.util.concurrent.atomic.AtomicReference; |
| 42 | +import java.util.function.Consumer; |
| 43 | + |
| 44 | +public class SseEventSourceRegisterErrorHandlerTest extends JerseyTest { |
| 45 | + @Path("sse") |
| 46 | + public static class SseEventSourceRegisterTestSseEndpoint { |
| 47 | + |
| 48 | + @Path("hello") |
| 49 | + @GET |
| 50 | + @Produces(SseFeature.SERVER_SENT_EVENTS) |
| 51 | + public void hello(@Context SseEventSink output, @Context Sse sse) throws InterruptedException { |
| 52 | + output.send(sse.newEvent("HELLO")); |
| 53 | + } |
| 54 | + |
| 55 | + @Path("close") |
| 56 | + @GET |
| 57 | + @Produces(SseFeature.SERVER_SENT_EVENTS) |
| 58 | + public void close(@Context SseEventSink output, @Context Sse sse) throws InterruptedException { |
| 59 | + output.close(); |
| 60 | + } |
| 61 | + |
| 62 | + @Path("500") |
| 63 | + @GET |
| 64 | + @Produces(SseFeature.SERVER_SENT_EVENTS) |
| 65 | + public void throw500(@Context SseEventSink output, @Context Sse sse) throws InterruptedException { |
| 66 | + throw new WebApplicationException(); |
| 67 | + } |
| 68 | + |
| 69 | + @Path("400") |
| 70 | + @GET |
| 71 | + @Produces(SseFeature.SERVER_SENT_EVENTS) |
| 72 | + public void throw400(@Context SseEventSink output, @Context Sse sse) throws InterruptedException { |
| 73 | + throw new BadRequestException(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected Application configure() { |
| 79 | + return new ResourceConfig(SseEventSourceRegisterTestSseEndpoint.class); |
| 80 | + } |
| 81 | + |
| 82 | + private static final Consumer<InboundSseEvent> EMPTY = event -> { |
| 83 | + }; |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testConnection404() throws InterruptedException { |
| 87 | + WebTarget sseTarget = target("sse"); |
| 88 | + AtomicReference<Throwable> throwable = new AtomicReference<>(); |
| 89 | + CountDownLatch completeLatch = new CountDownLatch(1); |
| 90 | + |
| 91 | + SseEventSource eventSource = SseEventSource.target(sseTarget).build(); |
| 92 | + eventSource.register(EMPTY, throwable::set, completeLatch::countDown); |
| 93 | + eventSource.open(); |
| 94 | + completeLatch.await(10_000, TimeUnit.MILLISECONDS); |
| 95 | + MatcherAssert.assertThat(throwable.get(), Matchers.notNullValue()); |
| 96 | + MatcherAssert.assertThat(throwable.get().getClass(), Matchers.is(NotFoundException.class)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testError500() throws InterruptedException { |
| 101 | + WebTarget sseTarget = target("sse/500"); |
| 102 | + AtomicReference<Throwable> throwable = new AtomicReference<>(); |
| 103 | + CountDownLatch completeLatch = new CountDownLatch(1); |
| 104 | + |
| 105 | + SseEventSource eventSource = SseEventSource.target(sseTarget).build(); |
| 106 | + eventSource.register(EMPTY, throwable::set, completeLatch::countDown); |
| 107 | + eventSource.open(); |
| 108 | + completeLatch.await(10_000, TimeUnit.MILLISECONDS); |
| 109 | + MatcherAssert.assertThat(throwable.get(), Matchers.notNullValue()); |
| 110 | + MatcherAssert.assertThat(throwable.get().getClass(), Matchers.is(InternalServerErrorException.class)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testError400() throws InterruptedException { |
| 115 | + WebTarget sseTarget = target("sse/400"); |
| 116 | + AtomicReference<Throwable> throwable = new AtomicReference<>(); |
| 117 | + CountDownLatch completeLatch = new CountDownLatch(1); |
| 118 | + |
| 119 | + SseEventSource eventSource = SseEventSource.target(sseTarget).build(); |
| 120 | + eventSource.register(EMPTY, throwable::set, completeLatch::countDown); |
| 121 | + eventSource.open(); |
| 122 | + completeLatch.await(10_000, TimeUnit.MILLISECONDS); |
| 123 | + MatcherAssert.assertThat(throwable.get(), Matchers.notNullValue()); |
| 124 | + MatcherAssert.assertThat(throwable.get().getClass(), Matchers.is(BadRequestException.class)); |
| 125 | + } |
| 126 | +} |
0 commit comments