use of org.springframework.mock.web.server.MockServerWebExchange in project spring-boot by spring-projects.
the class MetricsWebFilterTests method createTimedHandlerMethodExchange.
private MockServerWebExchange createTimedHandlerMethodExchange(String methodName) {
MockServerWebExchange exchange = createExchange("/projects/spring-boot", "/projects/{project}");
exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, new HandlerMethod(this, ReflectionUtils.findMethod(Handlers.class, methodName)));
return exchange;
}
use of org.springframework.mock.web.server.MockServerWebExchange in project spring-boot by spring-projects.
the class MetricsWebFilterTests method filterAddsExtraTagsAndException.
@Test
void filterAddsExtraTagsAndException() {
MockServerWebExchange exchange = createTimedHandlerMethodExchange("timedExtraTags");
this.webFilter.filter(exchange, (serverWebExchange) -> Mono.error(new IllegalStateException("test error"))).onErrorResume((ex) -> {
exchange.getResponse().setRawStatusCode(500);
return exchange.getResponse().setComplete();
}).block(Duration.ofSeconds(30));
assertMetricsContainsTag("uri", "/projects/{project}");
assertMetricsContainsTag("status", "500");
assertMetricsContainsTag("exception", "IllegalStateException");
assertMetricsContainsTag("tag1", "value1");
assertMetricsContainsTag("tag2", "value2");
}
use of org.springframework.mock.web.server.MockServerWebExchange in project spring-cloud-gateway by spring-cloud.
the class WeightCalculatorWebFilterTests method testChooseRouteWithRandom.
@Test
public void testChooseRouteWithRandom() {
WeightCalculatorWebFilter filter = new WeightCalculatorWebFilter();
filter.addWeightConfig(new WeightConfig("groupa", "route1", 1));
filter.addWeightConfig(new WeightConfig("groupa", "route2", 3));
filter.addWeightConfig(new WeightConfig("groupa", "route3", 6));
Random random = mock(Random.class);
when(random.nextDouble()).thenReturn(0.05).thenReturn(0.2).thenReturn(0.6);
filter.setRandom(random);
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost").build());
WebFilterChain filterChain = mock(WebFilterChain.class);
filter.filter(exchange, filterChain);
Map<String, String> weights = WeightCalculatorWebFilter.getWeights(exchange);
assertThat(weights).containsEntry("groupa", "route1");
filter.filter(exchange, filterChain);
weights = WeightCalculatorWebFilter.getWeights(exchange);
assertThat(weights).containsEntry("groupa", "route2");
filter.filter(exchange, filterChain);
weights = WeightCalculatorWebFilter.getWeights(exchange);
assertThat(weights).containsEntry("groupa", "route3");
}
use of org.springframework.mock.web.server.MockServerWebExchange in project spring-cloud-gateway by spring-cloud.
the class CloudFoundryRouteServicePredicateFactoryTest method itReturnsTrueWithAllHeadersPresent.
@Test
public void itReturnsTrueWithAllHeadersPresent() {
MockServerHttpRequest request = MockServerHttpRequest.get("someurl").header(CloudFoundryRouteServicePredicateFactory.X_CF_FORWARDED_URL, "url").header(CloudFoundryRouteServicePredicateFactory.X_CF_PROXY_METADATA, "metadata").header(CloudFoundryRouteServicePredicateFactory.X_CF_PROXY_SIGNATURE, "signature").build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
assertThat(predicate.test(exchange)).isTrue();
}
use of org.springframework.mock.web.server.MockServerWebExchange in project spring-cloud-gateway by spring-cloud.
the class CloudFoundryRouteServicePredicateFactoryTest method itReturnsFalseWithAHeadersMissing.
@Test
public void itReturnsFalseWithAHeadersMissing() {
MockServerHttpRequest request = MockServerHttpRequest.get("someurl").header(CloudFoundryRouteServicePredicateFactory.X_CF_FORWARDED_URL, "url").header(CloudFoundryRouteServicePredicateFactory.X_CF_PROXY_METADATA, "metadata").build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
assertThat(predicate.test(exchange)).isFalse();
}
Aggregations