@Override public void addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry registry) { registry.addInterceptor(new RequestNameHandlerInterceptorAdapter()); } }
@Test public void testAdapterReturnTrueWhenExceptionIsThrown() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { throw new Exception("FATAL!"); } }).when(request).getMethod(); boolean result = interceptorAdapter.preHandle(request, null, handlerMethod); Assert.assertTrue("Adapter should return true.", result); }
@Test public void testInterceptorRegistryHasRequiredAnnotations() { List<Annotation> annotations = Arrays.asList(InterceptorRegistry.class.getAnnotations()); Assert.assertNotNull(getAnnotationByType(annotations, Configuration.class)); }
/** * This method is being invoked just before the request is forwarded the the relevant controller. * It should always return true, so the request will be processed in any case of failure. */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { try { RequestTelemetryContext context = ThreadContext.getRequestTelemetryContext(); if (context == null) { return true; } String requestName = generateRequestName(request, handler); if (requestName == null) { return true; } context.getHttpRequestTelemetry().setName(requestName); } catch (Exception e) { InternalLogger.INSTANCE.error( "Failed to invoke interceptor '%s' with exception: %s.", this.getClass().getSimpleName(), e.toString()); } return true; }
@Override public void addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry registry) { registry.addInterceptor(new RequestNameHandlerInterceptorAdapter()); } }
/** * This method is being invoked just before the request is forwarded the the relevant controller. * It should always return true, so the request will be processed in any case of failure. */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { try { RequestTelemetryContext context = ThreadContext.getRequestTelemetryContext(); if (context == null) { return true; } String requestName = generateRequestName(request, handler); if (requestName == null) { return true; } context.getHttpRequestTelemetry().setName(requestName); } catch (Exception e) { InternalLogger.INSTANCE.error( "Failed to invoke interceptor '%s' with exception: %s.", this.getClass().getSimpleName(), e.toString()); } return true; }
@Test public void testAdapterReturnTrueWhenContextNull() throws Exception { ThreadContext.setRequestTelemetryContext(null); boolean result = interceptorAdapter.preHandle(null, null, handlerMethod); Assert.assertTrue("Adapter should return true.", result); } }
@Test public void testAdapterSetRequestNameCorrectly() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn(HttpMethods.GET); interceptorAdapter.preHandle(request, null, handlerMethod); String requestName = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().getName(); String expectedRequestName = String.format("%s %s/%s", HttpMethods.GET, DEFAULT_CONTROLLER_NAME, DEFAULT_ACTION_NAME); Assert.assertEquals(expectedRequestName, requestName); }