@Test(expected = IllegalArgumentException.class) public void combineDifferentLength() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1); CompositeRequestCondition cond2 = new CompositeRequestCondition(this.param1, this.header1); cond1.combine(cond2); }
@Test public void combine() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1, this.header1); CompositeRequestCondition cond2 = new CompositeRequestCondition(this.param2, this.header2); CompositeRequestCondition cond3 = new CompositeRequestCondition(this.param3, this.header3); assertEquals(cond3, cond1.combine(cond2)); }
@Test(expected = IllegalArgumentException.class) public void compareDifferentLength() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1); CompositeRequestCondition cond2 = new CompositeRequestCondition(this.param1, this.header1); cond1.compareTo(cond2, MockServerWebExchange.from(MockServerHttpRequest.get("/"))); }
/** * Delegate to <em>all</em> contained conditions to match the request and return the * resulting "matching" condition instances. * <p>An empty {@code CompositeRequestCondition} matches to all requests. */ @Override public CompositeRequestCondition getMatchingCondition(ServerWebExchange exchange) { if (isEmpty()) { return this; } RequestConditionHolder[] matchingConditions = new RequestConditionHolder[getLength()]; for (int i = 0; i < getLength(); i++) { matchingConditions[i] = this.requestConditions[i].getMatchingCondition(exchange); if (matchingConditions[i] == null) { return null; } } return new CompositeRequestCondition(matchingConditions); }
@Test public void combineEmpty() { CompositeRequestCondition empty = new CompositeRequestCondition(); CompositeRequestCondition notEmpty = new CompositeRequestCondition(this.param1); assertSame(empty, empty.combine(empty)); assertSame(notEmpty, notEmpty.combine(empty)); assertSame(notEmpty, empty.combine(notEmpty)); }
@Test public void compare() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1); CompositeRequestCondition cond3 = new CompositeRequestCondition(this.param3); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); assertEquals(1, cond1.compareTo(cond3, exchange)); assertEquals(-1, cond3.compareTo(cond1, exchange)); }
@Test public void match() { MockServerHttpRequest request = MockServerHttpRequest.get("/path?param1=paramValue1").build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); RequestCondition<?> condition1 = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST); RequestCondition<?> condition2 = new RequestMethodsRequestCondition(RequestMethod.GET); CompositeRequestCondition composite1 = new CompositeRequestCondition(this.param1, condition1); CompositeRequestCondition composite2 = new CompositeRequestCondition(this.param1, condition2); assertEquals(composite2, composite1.getMatchingCondition(exchange)); }
@Test public void compareEmpty() { CompositeRequestCondition empty = new CompositeRequestCondition(); CompositeRequestCondition notEmpty = new CompositeRequestCondition(this.param1); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); assertEquals(0, empty.compareTo(empty, exchange)); assertEquals(-1, notEmpty.compareTo(empty, exchange)); assertEquals(1, empty.compareTo(notEmpty, exchange)); }
@Test public void matchEmpty() { CompositeRequestCondition empty = new CompositeRequestCondition(); assertSame(empty, empty.getMatchingCondition(MockServerWebExchange.from(MockServerHttpRequest.get("/")))); }
/** * If one instance is empty, return the other. * If both instances have conditions, combine the individual conditions * after ensuring they are of the same type and number. */ @Override public CompositeRequestCondition combine(CompositeRequestCondition other) { if (isEmpty() && other.isEmpty()) { return this; } else if (other.isEmpty()) { return this; } else if (isEmpty()) { return other; } else { assertNumberOfConditions(other); RequestConditionHolder[] combinedConditions = new RequestConditionHolder[getLength()]; for (int i = 0; i < getLength(); i++) { combinedConditions[i] = this.requestConditions[i].combine(other.requestConditions[i]); } return new CompositeRequestCondition(combinedConditions); } }
@Test public void noMatch() { CompositeRequestCondition cond = new CompositeRequestCondition(this.param1); assertNull(cond.getMatchingCondition(MockServerWebExchange.from(MockServerHttpRequest.get("/")))); }
/** * Delegate to <em>all</em> contained conditions to match the request and return the * resulting "matching" condition instances. * <p>An empty {@code CompositeRequestCondition} matches to all requests. */ @Override public CompositeRequestCondition getMatchingCondition(ServerWebExchange exchange) { if (isEmpty()) { return this; } RequestConditionHolder[] matchingConditions = new RequestConditionHolder[getLength()]; for (int i = 0; i < getLength(); i++) { matchingConditions[i] = this.requestConditions[i].getMatchingCondition(exchange); if (matchingConditions[i] == null) { return null; } } return new CompositeRequestCondition(matchingConditions); }
/** * If one instance is empty, return the other. * If both instances have conditions, combine the individual conditions * after ensuring they are of the same type and number. */ @Override public CompositeRequestCondition combine(CompositeRequestCondition other) { if (isEmpty() && other.isEmpty()) { return this; } else if (other.isEmpty()) { return this; } else if (isEmpty()) { return other; } else { assertNumberOfConditions(other); RequestConditionHolder[] combinedConditions = new RequestConditionHolder[getLength()]; for (int i = 0; i < getLength(); i++) { combinedConditions[i] = this.requestConditions[i].combine(other.requestConditions[i]); } return new CompositeRequestCondition(combinedConditions); } }