001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.proxy;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.jmeter.samplers.SampleResult;
024: import org.apache.jmeter.protocol.http.sampler.HTTPNullSampler;
025: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
026:
027: public class TestProxyControl extends TestCase {
028: HTTPSamplerBase sampler;
029:
030: ProxyControl control;
031:
032: public TestProxyControl(String name) {
033: super (name);
034: }
035:
036: public void setUp() {
037: control = new ProxyControl();
038: control.addIncludedPattern(".*\\.jsp");
039: control.addExcludedPattern(".*apache.org.*");
040: sampler = new HTTPNullSampler();
041: }
042:
043: public void testFilter1() throws Exception {
044: sampler.setDomain("jakarta.org");
045: sampler.setPath("index.jsp");
046: assertTrue("Should find jakarta.org/index.jsp", control
047: .filterUrl(sampler));
048: }
049:
050: public void testFilter2() throws Exception {
051: sampler.setPath("index.jsp");
052: sampler.setDomain("www.apache.org");
053: assertFalse("Should not match www.apache.org", control
054: .filterUrl(sampler));
055: }
056:
057: public void testFilter3() throws Exception {
058: sampler.setPath("header.gif");
059: sampler.setDomain("jakarta.org");
060: assertFalse("Should not match header.gif", control
061: .filterUrl(sampler));
062: }
063:
064: public void testContentTypeNoFilters() throws Exception {
065: SampleResult result = new SampleResult();
066: // No filters
067: control.setContentTypeInclude(null);
068: control.setContentTypeExclude(null);
069:
070: result.setContentType(null);
071: assertTrue("Should allow if no content-type present", control
072: .filterContentType(result));
073: result.setContentType("text/html; charset=utf-8");
074: assertTrue("Should allow text/html", control
075: .filterContentType(result));
076: result.setContentType("image/png");
077: assertTrue("Should allow image/png", control
078: .filterContentType(result));
079:
080: // Empty filters
081: control.setContentTypeInclude("");
082: control.setContentTypeExclude("");
083:
084: result.setContentType(null);
085: assertTrue("Should allow if no content-type present", control
086: .filterContentType(result));
087: result.setContentType("text/html; charset=utf-8");
088: assertTrue("Should allow text/html", control
089: .filterContentType(result));
090: result.setContentType("image/png");
091: assertTrue("Should allow image/png", control
092: .filterContentType(result));
093:
094: // Non empty filters
095: control.setContentTypeInclude(" ");
096: control.setContentTypeExclude(" ");
097:
098: result.setContentType(null);
099: assertTrue("Should allow if no content-type present", control
100: .filterContentType(result));
101: result.setContentType("text/html; charset=utf-8");
102: assertFalse("Should not allow text/html", control
103: .filterContentType(result));
104: result.setContentType("image/png");
105: assertFalse("Should not allow image/png", control
106: .filterContentType(result));
107: }
108:
109: public void testContentTypeInclude() throws Exception {
110: SampleResult result = new SampleResult();
111: control.setContentTypeInclude("text/html|text/ascii");
112:
113: result.setContentType(null);
114: assertTrue("Should allow if no content-type present", control
115: .filterContentType(result));
116: result.setContentType("text/html; charset=utf-8");
117: assertTrue("Should allow text/html", control
118: .filterContentType(result));
119: result.setContentType("text/css");
120: assertFalse("Should not allow text/css", control
121: .filterContentType(result));
122: }
123:
124: public void testContentTypeExclude() throws Exception {
125: SampleResult result = new SampleResult();
126: control.setContentTypeExclude("text/css");
127:
128: result.setContentType(null);
129: assertTrue("Should allow if no content-type present", control
130: .filterContentType(result));
131: result.setContentType("text/html; charset=utf-8");
132: assertTrue("Should allow text/html", control
133: .filterContentType(result));
134: result.setContentType("text/css");
135: assertFalse("Should not allow text/css", control
136: .filterContentType(result));
137: }
138:
139: public void testContentTypeIncludeAndExclude() throws Exception {
140: SampleResult result = new SampleResult();
141: // Simple inclusion and exclusion filter
142: control.setContentTypeInclude("text/html|text/ascii");
143: control.setContentTypeExclude("text/css");
144:
145: result.setContentType(null);
146: assertTrue("Should allow if no content-type present", control
147: .filterContentType(result));
148: result.setContentType("text/html; charset=utf-8");
149: assertTrue("Should allow text/html", control
150: .filterContentType(result));
151: result.setContentType("text/css");
152: assertFalse("Should not allow text/css", control
153: .filterContentType(result));
154: result.setContentType("image/png");
155: assertFalse("Should not allow image/png", control
156: .filterContentType(result));
157:
158: // Allow all but images
159: control.setContentTypeInclude(null);
160: control.setContentTypeExclude("image/.*");
161:
162: result.setContentType(null);
163: assertTrue("Should allow if no content-type present", control
164: .filterContentType(result));
165: result.setContentType("text/html; charset=utf-8");
166: assertTrue("Should allow text/html", control
167: .filterContentType(result));
168: result.setContentType("text/css");
169: assertTrue("Should allow text/css", control
170: .filterContentType(result));
171: result.setContentType("image/png");
172: assertFalse("Should not allow image/png", control
173: .filterContentType(result));
174: }
175: }
|