|
| 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 io.jooby.problem; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.*; |
| 9 | +import static org.mockito.ArgumentMatchers.any; |
| 10 | +import static org.mockito.Mockito.*; |
| 11 | + |
| 12 | +import java.net.URI; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.DisplayName; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.mockito.ArgumentCaptor; |
| 20 | +import org.slf4j.Logger; |
| 21 | + |
| 22 | +import com.typesafe.config.Config; |
| 23 | +import io.jooby.*; |
| 24 | +import io.jooby.exception.NotAcceptableException; |
| 25 | + |
| 26 | +class ProblemDetailsHandlerTest { |
| 27 | + |
| 28 | + private ProblemDetailsHandler handler; |
| 29 | + private Context ctx; |
| 30 | + private Router router; |
| 31 | + private Logger log; |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + void setUp() { |
| 35 | + handler = new ProblemDetailsHandler(); |
| 36 | + ctx = mock(Context.class); |
| 37 | + router = mock(Router.class); |
| 38 | + log = mock(Logger.class); |
| 39 | + |
| 40 | + when(ctx.getRouter()).thenReturn(router); |
| 41 | + when(router.getLog()).thenReturn(log); |
| 42 | + |
| 43 | + // Mock these to prevent DefaultErrorHandler from crashing |
| 44 | + when(ctx.getMethod()).thenReturn("GET"); |
| 45 | + when(ctx.getRequestPath()).thenReturn("/test"); |
| 46 | + when(ctx.getRequestType(any())).thenReturn(MediaType.json); |
| 47 | + |
| 48 | + // Default accept behavior |
| 49 | + when(ctx.accept(anyList())).thenReturn(MediaType.html); |
| 50 | + when(ctx.setResponseType(any(MediaType.class))).thenReturn(ctx); |
| 51 | + when(ctx.setResponseCode(anyInt())).thenReturn(ctx); |
| 52 | + when(ctx.setResponseCode(any(StatusCode.class))).thenReturn(ctx); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @DisplayName("Verify static from(Config) parses full configuration") |
| 57 | + void testFromConfig() { |
| 58 | + Config conf = mock(Config.class); |
| 59 | + Config problemConfig = mock(Config.class); |
| 60 | + |
| 61 | + when(conf.hasPath(ProblemDetailsHandler.ROOT_CONFIG_PATH)).thenReturn(true); |
| 62 | + when(conf.getConfig(ProblemDetailsHandler.ROOT_CONFIG_PATH)).thenReturn(problemConfig); |
| 63 | + |
| 64 | + when(problemConfig.hasPath("log4xxErrors")).thenReturn(true); |
| 65 | + when(problemConfig.getBoolean("log4xxErrors")).thenReturn(true); |
| 66 | + |
| 67 | + when(problemConfig.hasPath("muteCodes")).thenReturn(true); |
| 68 | + when(problemConfig.getIntList("muteCodes")).thenReturn(List.of(404)); |
| 69 | + |
| 70 | + when(problemConfig.hasPath("muteTypes")).thenReturn(true); |
| 71 | + when(problemConfig.getStringList("muteTypes")) |
| 72 | + .thenReturn(List.of("java.lang.IllegalArgumentException")); |
| 73 | + |
| 74 | + ProblemDetailsHandler result = ProblemDetailsHandler.from(conf); |
| 75 | + assertNotNull(result); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("Verify NotAcceptableException triggers immediate HTML response") |
| 80 | + void testApplyNotAcceptable() { |
| 81 | + NotAcceptableException ex = new NotAcceptableException("No match"); |
| 82 | + handler.apply(ctx, ex, StatusCode.NOT_ACCEPTABLE); |
| 83 | + |
| 84 | + verify(ctx).setResponseType(MediaType.html); |
| 85 | + verify(ctx).send(contains("<h1>Not Acceptable</h1>")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + @DisplayName("Verify JSON content negotiation and problem response mapping") |
| 90 | + @SuppressWarnings("unchecked") |
| 91 | + void testApplyJsonProblem() { |
| 92 | + when(ctx.accept(anyList())).thenReturn(MediaType.json); |
| 93 | + IllegalArgumentException ex = new IllegalArgumentException("Invalid ID"); |
| 94 | + |
| 95 | + handler.apply(ctx, ex, StatusCode.BAD_REQUEST); |
| 96 | + |
| 97 | + verify(ctx).setResponseType(MediaType.PROBLEM_JSON); |
| 98 | + ArgumentCaptor<Object> resultCaptor = ArgumentCaptor.forClass(Object.class); |
| 99 | + verify(ctx).render(resultCaptor.capture()); |
| 100 | + |
| 101 | + Map<String, Object> map = (Map<String, Object>) resultCaptor.getValue(); |
| 102 | + assertEquals("Bad Request", map.get("title")); |
| 103 | + assertEquals("Invalid ID", map.get("detail")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + @DisplayName("Verify XML content negotiation") |
| 108 | + void testApplyXmlProblem() { |
| 109 | + when(ctx.accept(anyList())).thenReturn(MediaType.xml); |
| 110 | + handler.apply(ctx, new Exception("XML Error"), StatusCode.BAD_REQUEST); |
| 111 | + |
| 112 | + verify(ctx).setResponseType(MediaType.PROBLEM_XML); |
| 113 | + verify(ctx).render(any()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + @DisplayName("Verify Plain Text content negotiation") |
| 118 | + void testApplyTextProblem() { |
| 119 | + when(ctx.accept(anyList())).thenReturn(MediaType.text); |
| 120 | + handler.apply(ctx, new Exception("Text Error"), StatusCode.BAD_REQUEST); |
| 121 | + |
| 122 | + verify(ctx).setResponseType(MediaType.text); |
| 123 | + verify(ctx).send(contains("title='Bad Request'")); |
| 124 | + verify(ctx).send(contains("Text Error")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + @DisplayName("Verify internal error fallback when render fails") |
| 129 | + void testApplyRenderFailureFallback() { |
| 130 | + when(ctx.accept(anyList())).thenReturn(MediaType.json); |
| 131 | + doThrow(new NotAcceptableException("foo")).when(ctx).render(any()); |
| 132 | + |
| 133 | + handler.apply(ctx, new Exception("fail"), StatusCode.BAD_REQUEST); |
| 134 | + |
| 135 | + verify(ctx, atLeastOnce()).setResponseType(MediaType.html); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + @DisplayName("Evaluate problem: HttpProblem instance") |
| 140 | + void testEvaluateHttpProblem() { |
| 141 | + HttpProblem problem = HttpProblem.valueOf(StatusCode.FORBIDDEN, "Forbidden", "Access Denied"); |
| 142 | + handler.apply(ctx, problem, StatusCode.FORBIDDEN); |
| 143 | + |
| 144 | + // apply() calls setResponseCode, and fallback sendHtml() also calls setResponseCode |
| 145 | + verify(ctx, atLeastOnce()).setResponseCode(403); |
| 146 | + verify(ctx).send(contains("Access Denied")); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + @DisplayName("Evaluate problem: 500 error mapping") |
| 151 | + void testEvaluateInternalError() { |
| 152 | + handler.apply(ctx, new RuntimeException("boom"), StatusCode.SERVER_ERROR); |
| 153 | + verify(ctx).send(contains("Server Error")); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + @DisplayName("Evaluate problem: message multi-line splitting") |
| 158 | + void testEvaluateMessageSplitting() { |
| 159 | + handler.apply(ctx, new Exception("First Line\nSecond Line"), StatusCode.BAD_REQUEST); |
| 160 | + verify(ctx).send(contains("First Line")); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + @DisplayName("Logging: Server Error (Error level)") |
| 165 | + void testLogServerError() { |
| 166 | + handler.apply(ctx, new Exception("critical"), StatusCode.SERVER_ERROR); |
| 167 | + verify(log, atLeastOnce()).error(anyString(), any(Throwable.class)); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + @DisplayName("Logging: 4xx Errors (Info/Debug level)") |
| 172 | + void testLog4xxError() { |
| 173 | + handler.log4xxErrors(); |
| 174 | + when(log.isDebugEnabled()).thenReturn(false); |
| 175 | + |
| 176 | + handler.apply(ctx, new Exception("client error"), StatusCode.BAD_REQUEST); |
| 177 | + verify(log).info(anyString()); |
| 178 | + |
| 179 | + reset(log); |
| 180 | + when(log.isDebugEnabled()).thenReturn(true); |
| 181 | + handler.apply(ctx, new Exception("client error debug"), StatusCode.BAD_REQUEST); |
| 182 | + verify(log).debug(anyString(), any(Throwable.class)); |
| 183 | + } |
| 184 | + |
| 185 | + // Dummy exception class that correctly extends Throwable to satisfy the type constraints |
| 186 | + private static class MappableException extends RuntimeException implements HttpProblemMappable { |
| 187 | + private final HttpProblem problem; |
| 188 | + |
| 189 | + public MappableException(HttpProblem problem) { |
| 190 | + this.problem = problem; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public HttpProblem toHttpProblem() { |
| 195 | + return problem; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + @DisplayName("HTML Generation: verify extra details (instance, params, errors)") |
| 201 | + void testHtmlExtraDetails() { |
| 202 | + HttpProblem problem = |
| 203 | + HttpProblem.builder() |
| 204 | + .status(StatusCode.BAD_REQUEST) |
| 205 | + .title("Bad Request") |
| 206 | + .instance(URI.create("/path")) |
| 207 | + .detail("Some details") |
| 208 | + .param("p1", "v1") |
| 209 | + .errors(List.of(new HttpProblem.Error("err1", "err1 detail"))) |
| 210 | + .build(); |
| 211 | + |
| 212 | + // Use our custom dummy exception to pass the Type constraints |
| 213 | + MappableException mappableException = new MappableException(problem); |
| 214 | + |
| 215 | + handler.apply(ctx, mappableException, StatusCode.BAD_REQUEST); |
| 216 | + |
| 217 | + verify(ctx) |
| 218 | + .send( |
| 219 | + argThat( |
| 220 | + (String s) -> |
| 221 | + s.contains("instance: /path") |
| 222 | + && s.contains("detail: Some details") |
| 223 | + && s.contains("parameters: {p1=v1}") |
| 224 | + // Match the class name since toString() prints the object reference |
| 225 | + && s.contains("errors: [io.jooby.problem.HttpProblem$Error@"))); |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + @DisplayName("Verify apply handles unexpected internal exceptions silently") |
| 230 | + void testApplyUnexpectedException() { |
| 231 | + when(ctx.accept(anyList())).thenThrow(new RuntimeException("Negotiation Crash")); |
| 232 | + |
| 233 | + assertDoesNotThrow(() -> handler.apply(ctx, new Exception("original"), StatusCode.BAD_REQUEST)); |
| 234 | + verify(log).error(contains("Unexpected error"), any(Throwable.class)); |
| 235 | + } |
| 236 | +} |
0 commit comments