/** * Returns a new instance with the union of the header expressions * from "this" and the "other" instance. */ @Override public HeadersRequestCondition combine(HeadersRequestCondition other) { Set<HeaderExpression> set = new LinkedHashSet<>(this.expressions); set.addAll(other.expressions); return new HeadersRequestCondition(set); }
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 headerEquals() { assertEquals(new HeadersRequestCondition("foo"), new HeadersRequestCondition("foo")); assertEquals(new HeadersRequestCondition("foo"), new HeadersRequestCondition("FOO")); assertNotEquals(new HeadersRequestCondition("foo"), new HeadersRequestCondition("bar")); assertEquals(new HeadersRequestCondition("foo=bar"), new HeadersRequestCondition("foo=bar")); assertEquals(new HeadersRequestCondition("foo=bar"), new HeadersRequestCondition("FOO=bar")); }
@Test public void combine() { HeadersRequestCondition condition1 = new HeadersRequestCondition("foo=bar"); HeadersRequestCondition condition2 = new HeadersRequestCondition("foo=baz"); HeadersRequestCondition result = condition1.combine(condition2); Collection<?> conditions = result.getContent(); assertEquals(2, conditions.size()); }
@Before public void setup() throws Exception { this.param1 = new ParamsRequestCondition("param1"); this.param2 = new ParamsRequestCondition("param2"); this.param3 = this.param1.combine(this.param2); this.header1 = new HeadersRequestCondition("header1"); this.header2 = new HeadersRequestCondition("header2"); this.header3 = this.header1.combine(this.header2); }
@Test public void compareToWithNegatedMatch() { ServerWebExchange exchange = MockServerWebExchange.from(get("/")); HeadersRequestCondition condition1 = new HeadersRequestCondition("foo!=a"); HeadersRequestCondition condition2 = new HeadersRequestCondition("foo"); assertEquals("Negated match should not count as more specific", 0, condition1.compareTo(condition2, exchange)); }
@Test // SPR-16674 public void compareToWithMoreSpecificMatchByValue() { ServerWebExchange exchange = MockServerWebExchange.from(get("/")); HeadersRequestCondition condition1 = new HeadersRequestCondition("foo=a"); HeadersRequestCondition condition2 = new HeadersRequestCondition("foo"); int result = condition1.compareTo(condition2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); }
@Test(expected = ClassCastException.class) public void combineIncompatible() { RequestConditionHolder params = new RequestConditionHolder(new ParamsRequestCondition("name")); RequestConditionHolder headers = new RequestConditionHolder(new HeadersRequestCondition("name")); params.combine(headers); }
@Test(expected = ClassCastException.class) public void compareIncompatible() { RequestConditionHolder params = new RequestConditionHolder(new ParamsRequestCondition("name")); RequestConditionHolder headers = new RequestConditionHolder(new HeadersRequestCondition("name")); params.compareTo(headers, this.exchange); }
@Test public void compareTo() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/")); HeadersRequestCondition condition1 = new HeadersRequestCondition("foo", "bar", "baz"); HeadersRequestCondition condition2 = new HeadersRequestCondition("foo=a", "bar"); 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); }
@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 getMatchingCondition() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("foo", "bar")); HeadersRequestCondition condition = new HeadersRequestCondition("foo"); HeadersRequestCondition result = condition.getMatchingCondition(exchange); assertEquals(condition, result); condition = new HeadersRequestCondition("bar"); result = condition.getMatchingCondition(exchange); assertNull(result); }
@Test public void headerNotPresent() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/")); HeadersRequestCondition condition = new HeadersRequestCondition("!accept"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void headerValueMatch() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("foo", "bar")); HeadersRequestCondition condition = new HeadersRequestCondition("foo=bar"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void headerValueNoMatch() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("foo", "bazz")); HeadersRequestCondition condition = new HeadersRequestCondition("foo=bar"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void headerPresent() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "")); HeadersRequestCondition condition = new HeadersRequestCondition("accept"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void headerPresentNoMatch() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("bar", "")); HeadersRequestCondition condition = new HeadersRequestCondition("foo"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void headerCaseSensitiveValueMatch() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("foo", "bar")); HeadersRequestCondition condition = new HeadersRequestCondition("foo=Bar"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void headerValueNoMatchNegated() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("foo", "bar")); HeadersRequestCondition condition = new HeadersRequestCondition("foo!=bar"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void headerValueMatchNegated() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("foo", "baz")); HeadersRequestCondition condition = new HeadersRequestCondition("foo!=bar"); assertNotNull(condition.getMatchingCondition(exchange)); }