001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.web.servlet.htmlunit; 018 019import java.net.URL; 020import java.util.Arrays; 021import java.util.HashSet; 022import java.util.Set; 023 024import com.gargoylesoftware.htmlunit.WebRequest; 025 026/** 027 * A {@link WebRequestMatcher} that allows matching on the host and optionally 028 * the port of {@code WebRequest#getUrl()}. 029 * 030 * <p>For example, the following would match any request to the host 031 * {@code "code.jquery.com"} without regard for the port. 032 * 033 * <pre class="code">WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com");</pre> 034 * 035 * <p>Multiple hosts can also be passed in. For example, the following would 036 * match any request to the host {@code "code.jquery.com"} or the host 037 * {@code "cdn.com"} without regard for the port. 038 * 039 * <pre class="code">WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com", "cdn.com");</pre> 040 * 041 * <p>Alternatively, one can also specify the port. For example, the following would match 042 * any request to the host {@code "code.jquery.com"} with the port of {@code 80}. 043 * 044 * <pre class="code">WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com:80");</pre> 045 * 046 * <p>The above {@code cdnMatcher} would match {@code "http://code.jquery.com/jquery.js"} 047 * which has a default port of {@code 80} and {@code "http://code.jquery.com:80/jquery.js"}. 048 * However, it would not match {@code "https://code.jquery.com/jquery.js"} 049 * which has a default port of {@code 443}. 050 * 051 * @author Rob Winch 052 * @author Sam Brannen 053 * @since 4.2 054 * @see UrlRegexRequestMatcher 055 * @see org.springframework.test.web.servlet.htmlunit.DelegatingWebConnection 056 */ 057public final class HostRequestMatcher implements WebRequestMatcher { 058 059 private final Set<String> hosts = new HashSet<String>(); 060 061 062 /** 063 * Create a new {@code HostRequestMatcher} for the given hosts — 064 * for example: {@code "localhost"}, {@code "example.com:443"}, etc. 065 * @param hosts the hosts to match on 066 */ 067 public HostRequestMatcher(String... hosts) { 068 this.hosts.addAll(Arrays.asList(hosts)); 069 } 070 071 072 @Override 073 public boolean matches(WebRequest request) { 074 URL url = request.getUrl(); 075 String host = url.getHost(); 076 077 if (this.hosts.contains(host)) { 078 return true; 079 } 080 081 int port = url.getPort(); 082 if (port == -1) { 083 port = url.getDefaultPort(); 084 } 085 String hostAndPort = host + ":" + port; 086 087 return this.hosts.contains(hostAndPort); 088 } 089 090}