|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package tests.sse; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.mockito.ArgumentMatchers.any; |
| 10 | +import static org.mockito.Mockito.*; |
| 11 | + |
| 12 | +import com.google.testing.compile.CompilationSubject; |
| 13 | +import com.google.testing.compile.Compiler; |
| 14 | +import com.google.testing.compile.JavaFileObjects; |
| 15 | +import io.jooby.Context; |
| 16 | +import io.jooby.ServerSentEmitter; |
| 17 | +import io.jooby.StatusCode; |
| 18 | +import io.jooby.apt.JoobyProcessor; |
| 19 | +import io.jooby.apt.ProcessorRunner; |
| 20 | +import io.jooby.value.Value; |
| 21 | +import io.jooby.value.ValueFactory; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +public class SseTest { |
| 25 | + @Test |
| 26 | + public void shouldGenerateSseRoutesAndInvokeEmitter() throws Exception { |
| 27 | + new ProcessorRunner(new SseController()) |
| 28 | + .withSourceCode( |
| 29 | + source -> { |
| 30 | + assertThat(source).contains("app.sse(\"/events/sse\", this::sse)"); |
| 31 | + assertThat(source).contains("app.sse(\"/events/alias\", this::sse)"); |
| 32 | + assertThat(source).contains("app.sse(\"/events/{name}\", this::named)"); |
| 33 | + assertThat(source).contains("public void sse(io.jooby.ServerSentEmitter emitter)"); |
| 34 | + assertThat(source).doesNotContain("return ctx.getResponseCode()"); |
| 35 | + }) |
| 36 | + .withRouter( |
| 37 | + app -> { |
| 38 | + assertThat(app.getRoutes()).hasSize(3); |
| 39 | + var route = app.getRoutes().getFirst(); |
| 40 | + assertThat(route.getMethod()).isEqualTo("SSE"); |
| 41 | + var ctx = mock(Context.class, RETURNS_SELF); |
| 42 | + var emitter = mock(ServerSentEmitter.class); |
| 43 | + when(emitter.getContext()).thenReturn(ctx); |
| 44 | + doAnswer( |
| 45 | + call -> { |
| 46 | + call.getArgument(0, ServerSentEmitter.Handler.class).handle(emitter); |
| 47 | + return ctx; |
| 48 | + }) |
| 49 | + .when(ctx) |
| 50 | + .upgrade(any(ServerSentEmitter.Handler.class)); |
| 51 | + |
| 52 | + route.getHandler().apply(ctx); |
| 53 | + |
| 54 | + verify(ctx).setResponseType("text/event-stream; charset=utf-8"); |
| 55 | + verify(emitter).send("Welcome"); |
| 56 | + verify(emitter).onClose(any()); |
| 57 | + verify(emitter).close(); |
| 58 | + verify(ctx).setResponseCode(StatusCode.OK); |
| 59 | + |
| 60 | + when(ctx.path("name")).thenReturn(Value.value(new ValueFactory(), "name", "Ada")); |
| 61 | + when(ctx.getRequestPath()).thenReturn("/events/Ada"); |
| 62 | + app.getRoutes().get(2).getHandler().apply(ctx); |
| 63 | + verify(emitter).send("Ada:/events/Ada"); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void shouldGenerateKotlinEmitterHandler() throws Exception { |
| 69 | + new ProcessorRunner(new SseController()) |
| 70 | + .withSourceCode( |
| 71 | + true, |
| 72 | + source -> { |
| 73 | + assertThat(source).contains("app.sse(\"/events/sse\", this::sse)"); |
| 74 | + assertThat(source).contains("fun sse(emitter: io.jooby.ServerSentEmitter)"); |
| 75 | + assertThat(source).contains("val ctx = emitter.getContext()"); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void shouldRejectNonVoidSseMethods() { |
| 81 | + var compilation = |
| 82 | + Compiler.javac() |
| 83 | + .withProcessors(new JoobyProcessor()) |
| 84 | + .compile( |
| 85 | + JavaFileObjects.forSourceString( |
| 86 | + "InvalidSse", |
| 87 | + """ |
| 88 | + import io.jooby.annotation.SSE; |
| 89 | + public class InvalidSse { |
| 90 | + @SSE("/sse") |
| 91 | + public String sse(io.jooby.ServerSentEmitter emitter) { |
| 92 | + return "invalid"; |
| 93 | + } |
| 94 | + } |
| 95 | + """)); |
| 96 | + CompilationSubject.assertThat(compilation).failed(); |
| 97 | + CompilationSubject.assertThat(compilation).hadErrorContaining("@SSE method must return void"); |
| 98 | + } |
| 99 | +} |
0 commit comments