@Override public Matcher requestMatcher() { return Matcher.match(MATCHING_PATH, HttpMethod.GET); } }
protected String getMatchingHttpMethodsAsCombinedString(Endpoint<?> endpoint) { if (endpoint.requestMatcher().isMatchAllMethods()) return "ALL"; return StringUtils.join(endpoint.requestMatcher().matchingMethods(), ","); }
); for (Endpoint<?> endpoint : endpoints) { String matchingMethods = endpoint.requestMatcher().isMatchAllMethods() ? "ALL" : StringUtils.join(endpoint.requestMatcher().matchingMethods(), ","); logger.info( "Registering endpoint that matches methods and paths: {}\t{}", matchingMethods, endpoint.requestMatcher().matchingPathTemplates() );
@Test(expected = MethodNotAllowed405Exception.class) public void findSingleEndpointForExecution_throws_MethodNotAllowed405Exception_if_path_matches_but_method_does_not() { // given doReturn(Optional.of(defaultPath)).when(matcherMock).matchesPath(any(RequestInfo.class)); doReturn(false).when(matcherMock).matchesMethod(any(RequestInfo.class)); // expect handlerSpy.findSingleEndpointForExecution(requestInfoMock); }
protected String getTimerAndMeterMapKeyForEndpoint(Endpoint<?> endpoint) { String methodsString = getMatchingHttpMethodsAsCombinedString(endpoint); //TODO: this might be odd for multi-path endpoints return methodsString + "-" + endpoint.requestMatcher().matchingPathTemplates(); }
new DummyEndpoint(Matcher.match("/foo")), new DummyEndpoint(Matcher.match("/bar", HttpMethod.POST, HttpMethod.PUT)), new DummyEndpoint(Matcher.multiMatch(Arrays.asList("/multiFoo", "/multiBar"))), new DummyEndpoint(Matcher.multiMatch(Arrays.asList("/multiBaz", "/multiBat"), HttpMethod.PATCH, HttpMethod.OPTIONS)) );
@Test(expected = PathNotFound404Exception.class) public void findSingleEndpointForExecution_throws_PathNotFound404Exception_if_no_matching_path() { // given doReturn(Optional.empty()).when(matcherMock).matchesPath(any(RequestInfo.class)); // expect handlerSpy.findSingleEndpointForExecution(requestInfoMock); }
); for (Endpoint<?> endpoint : endpoints) { String matchingMethods = endpoint.requestMatcher().isMatchAllMethods() ? "ALL" : StringUtils.join(endpoint.requestMatcher().matchingMethods(), ","); logger.info( "Registering endpoint that matches methods and paths: {}\t{}", matchingMethods, endpoint.requestMatcher().matchingPathTemplates() );
@Test(expected = MultipleMatchingEndpointsException.class) public void findSingleEndpointForExecution_throws_MultipleMatchingEndpointsException_if_multiple_endpoints_fully_match() { // given doReturn(Optional.of(defaultPath)).when(matcherMock).matchesPath(any(RequestInfo.class)); doReturn(true).when(matcherMock).matchesMethod(any(RequestInfo.class)); Endpoint<?> alsoMatchingEndpointMock = mock(Endpoint.class); doReturn(matcherMock).when(alsoMatchingEndpointMock).requestMatcher(); endpoints.add(alsoMatchingEndpointMock); // when handlerSpy.findSingleEndpointForExecution(requestInfoMock); }
@Test public void doChannelRead_calls_findSingleEndpointForExecution_then_sets_path_params_and_endpoint_on_state_then_returns_CONTINUE_if_msg_is_HttpRequest() { // given doReturn(Arrays.asList(defaultPath)).when(matcherMock).matchingPathTemplates(); HttpRequest msg = mock(HttpRequest.class); // when PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg); // then verify(handlerSpy).findSingleEndpointForExecution(requestInfoMock); verify(requestInfoMock).setPathParamsBasedOnPathTemplate(defaultPath); verify(stateMock).setEndpointForExecution(endpointMock, defaultPath); assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); }
new DummyEndpoint(Matcher.match("/foo")), new DummyEndpoint(Matcher.match("/bar", HttpMethod.POST, HttpMethod.PUT)), new DummyEndpoint(Matcher.multiMatch(Arrays.asList("/multiFoo", "/multiBar"))), new DummyEndpoint(Matcher.multiMatch(Arrays.asList("/multiBaz", "/multiBat"), HttpMethod.PATCH, HttpMethod.OPTIONS)) );
doReturn(HttpVersion.HTTP_1_1).when(msgMock).protocolVersion(); RequestInfo<?> requestInfoAlreadyOnState = (requestInfoAlreadySetOnState) ? requestInfoMock : null; doReturn(Optional.of(uri)).when(matcherMock).matchesPath(any(RequestInfo.class)); doReturn(requestInfoAlreadyOnState).when(stateMock).getRequestInfo();
@Override public Matcher requestMatcher() { return Matcher.match(MATCHING_PATH); } }
@Test public void findSingleEndpointForExecution_returns_matching_endpoint() { // given doReturn(Optional.of(defaultPath)).when(matcherMock).matchesPath(any(RequestInfo.class)); doReturn(true).when(matcherMock).matchesMethod(any(RequestInfo.class)); // when Pair<Endpoint<?>, String> result = handlerSpy.findSingleEndpointForExecution(requestInfoMock); // then assertThat(result.getKey()).isSameAs(endpointMock); assertThat(result.getValue()).isSameAs(defaultPath); }
@Test public void doChannelRead_does_nothing_if_msg_is_not_HttpRequest() { // given String pathTemplate = "/some/path/with/{id}"; Collection<String> pathTemplates = new ArrayList<String>() {{ add(pathTemplate); }}; doReturn(pathTemplates).when(matcherMock).matchingPathTemplates(); HttpObject msg = mock(HttpObject.class); // when PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg); // then verify(handlerSpy).doChannelRead(ctxMock, msg); verifyNoMoreInteractions(handlerSpy); verifyNoMoreInteractions(requestInfoMock); verifyNoMoreInteractions(stateMock); assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); }
protected String getMatchingHttpMethodsAsCombinedString(Endpoint<?> endpoint) { if (endpoint.requestMatcher().isMatchAllMethods()) return "ALL"; return StringUtils.join(endpoint.requestMatcher().matchingMethods(), ","); }
@Override public Matcher requestMatcher() { return Matcher.match(MATCHING_PATH); } }
Optional<String> pattern = endpoint.requestMatcher().matchesPath(requestInfo); if (pattern.isPresent()) { hasPathMatch = true; if (endpoint.requestMatcher().matchesMethod(requestInfo)) { fullyMatchingEndpoints.add(endpoint); matchingPattern = pattern.get();
/** * Adds metrics related to the given ServerConfig - usually gauges so you can inspect how the ServerConfig was setup. * Usually not needed - better to log this info on startup. */ protected void addServerConfigMetrics(ServerConfig config) { // add server config gauges metricsCollector.registerNamedMetric(serverConfigMetricNamingStrategy.nameFor(BOSS_THREADS), (Gauge<Integer>)config::numBossThreads); metricsCollector.registerNamedMetric(serverConfigMetricNamingStrategy.nameFor(WORKER_THREADS), (Gauge<Integer>)config::numWorkerThreads); metricsCollector.registerNamedMetric(serverConfigMetricNamingStrategy.nameFor(MAX_REQUEST_SIZE_IN_BYTES), (Gauge<Integer>)config::maxRequestSizeInBytes); List<String> endpointsList = config.appEndpoints() .stream() .map( endpoint -> endpoint.getClass().getName() + "-" + getMatchingHttpMethodsAsCombinedString(endpoint) + "-" + endpoint.requestMatcher().matchingPathTemplates() ) .collect(Collectors.toList()); metricsCollector.registerNamedMetric(serverConfigMetricNamingStrategy.nameFor(ENDPOINTS), (Gauge<List<String>>)() -> endpointsList); }
@Override public Matcher requestMatcher() { return Matcher.match(MATCHING_PATH, HttpMethod.GET); } }