public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondition patterns, @Nullable RequestMethodsRequestCondition methods, @Nullable ParamsRequestCondition params, @Nullable HeadersRequestCondition headers, @Nullable ConsumesRequestCondition consumes, @Nullable ProducesRequestCondition produces, @Nullable RequestCondition<?> custom) { this.name = (StringUtils.hasText(name) ? name : null); this.patternsCondition = (patterns != null ? patterns : new PatternsRequestCondition()); this.methodsCondition = (methods != null ? methods : new RequestMethodsRequestCondition()); this.paramsCondition = (params != null ? params : new ParamsRequestCondition()); this.headersCondition = (headers != null ? headers : new HeadersRequestCondition()); this.consumesCondition = (consumes != null ? consumes : new ConsumesRequestCondition()); this.producesCondition = (produces != null ? produces : new ProducesRequestCondition()); this.customConditionHolder = new RequestConditionHolder(custom); }
@Test public void combine() { ProducesRequestCondition condition1 = new ProducesRequestCondition("text/plain"); ProducesRequestCondition condition2 = new ProducesRequestCondition("application/xml"); ProducesRequestCondition result = condition1.combine(condition2); assertEquals(condition2, result); }
@Test public void combineWithDefault() { ProducesRequestCondition condition1 = new ProducesRequestCondition("text/plain"); ProducesRequestCondition condition2 = new ProducesRequestCondition(); ProducesRequestCondition result = condition1.combine(condition2); assertEquals(condition1, result); }
@Test public void instantiateWithProducesAndHeaderConditions() { String[] produces = new String[] {"text/plain"}; String[] headers = new String[]{"foo=bar", "accept=application/xml,application/pdf"}; ProducesRequestCondition condition = new ProducesRequestCondition(produces, headers); assertConditions(condition, "text/plain", "application/xml", "application/pdf"); }
@Test public void getProducibleMediaTypes() { ProducesRequestCondition condition = new ProducesRequestCondition("!application/xml"); assertEquals(Collections.emptySet(), condition.getProducibleMediaTypes()); }
@Override public RequestMappingInfo build() { RequestedContentTypeResolver contentTypeResolver = this.options.getContentTypeResolver(); PathPatternParser parser = (this.options.getPatternParser() != null ? this.options.getPatternParser() : new PathPatternParser()); PatternsRequestCondition patternsCondition = new PatternsRequestCondition(parse(this.paths, parser)); return new RequestMappingInfo(this.mappingName, patternsCondition, new RequestMethodsRequestCondition(this.methods), new ParamsRequestCondition(this.params), new HeadersRequestCondition(this.headers), new ConsumesRequestCondition(this.consumes, this.headers), new ProducesRequestCondition(this.produces, this.headers, contentTypeResolver), this.customCondition); }
@Test public void compareToMediaTypeAllWithParameter() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "*/*;q=0.9")); ProducesRequestCondition condition1 = new ProducesRequestCondition(); ProducesRequestCondition condition2 = new ProducesRequestCondition("application/json"); assertTrue(condition1.compareTo(condition2, exchange) < 0); assertTrue(condition2.compareTo(condition1, exchange) > 0); }
@Test public void getMatchingCondition() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain", "application/xml"); ProducesRequestCondition result = condition.getMatchingCondition(exchange); assertConditions(result, "text/plain"); condition = new ProducesRequestCondition("application/xml"); result = condition.getMatchingCondition(exchange); assertNull(result); }
@Test public void compareToWithSingleExpression() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition1 = new ProducesRequestCondition("text/plain"); ProducesRequestCondition condition2 = new ProducesRequestCondition("text/*"); int result = condition1.compareTo(condition2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); result = condition2.compareTo(condition1, exchange); assertTrue("Invalid comparison result: " + result, result > 0); }
@Test public void compareToEqualMatch() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/*")); ProducesRequestCondition condition1 = new ProducesRequestCondition("text/plain"); ProducesRequestCondition condition2 = new ProducesRequestCondition("text/xhtml"); int result = condition1.compareTo(condition2, exchange); assertTrue("Should have used MediaType.equals(Object) to break the match", result < 0); result = condition2.compareTo(condition1, exchange); assertTrue("Should have used MediaType.equals(Object) to break the match", result > 0); }
@Test public void compareToMultipleExpressions() { ProducesRequestCondition condition1 = new ProducesRequestCondition("*/*", "text/plain"); ProducesRequestCondition condition2 = new ProducesRequestCondition("text/*", "text/plain;q=0.7"); MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); int result = condition1.compareTo(condition2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); result = condition2.compareTo(condition1, exchange); assertTrue("Invalid comparison result: " + result, result > 0); }
@Test public void matchParseErrorWithNegation() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "bogus")); ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void matchWildcard() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("text/*"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void matchParseError() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "bogus")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test // SPR-17550 public void matchWithNegationAndMediaTypeAllWithQualityParameter() { ProducesRequestCondition condition = new ProducesRequestCondition("!application/json"); MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void matchSingle() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "application/xml")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void match() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void matchNegated() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void matchMultiple() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain", "application/xml"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void compareToMultipleExpressionsAndMultipleAcceptHeaderValues() { ProducesRequestCondition condition1 = new ProducesRequestCondition("text/*", "text/plain"); ProducesRequestCondition condition2 = new ProducesRequestCondition("application/*", "application/xml"); ServerWebExchange exchange = MockServerWebExchange.from( get("/").header("Accept", "text/plain", "application/xml")); int result = condition1.compareTo(condition2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); result = condition2.compareTo(condition1, exchange); assertTrue("Invalid comparison result: " + result, result > 0); exchange = MockServerWebExchange.from( get("/").header("Accept", "application/xml", "text/plain")); result = condition1.compareTo(condition2, exchange); assertTrue("Invalid comparison result: " + result, result > 0); result = condition2.compareTo(condition1, exchange); assertTrue("Invalid comparison result: " + result, result < 0); }