001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import static org.easymock.EasyMock.contains;
018: import static org.easymock.EasyMock.eq;
019:
020: import javax.servlet.http.HttpServletResponse;
021:
022: import org.apache.tapestry.TapestryConstants;
023: import org.apache.tapestry.internal.test.InternalBaseTestCase;
024: import org.apache.tapestry.ioc.Resource;
025: import org.apache.tapestry.ioc.internal.util.ClasspathResource;
026: import org.apache.tapestry.services.ClasspathAssetAliasManager;
027: import org.apache.tapestry.services.Dispatcher;
028: import org.apache.tapestry.services.Request;
029: import org.apache.tapestry.services.Response;
030: import org.testng.annotations.Test;
031:
032: public class AssetDispatcherTest extends InternalBaseTestCase {
033: private static final String SMILEY_CLIENT_URL = "/assets/app1/pages/smiley.png";
034:
035: private static final String SMILEY_PATH = "org/apache/tapestry/integration/app1/pages/smiley.png";
036:
037: private static final Resource SMILEY = new ClasspathResource(
038: SMILEY_PATH);
039:
040: @Test
041: public void not_an_asset_request() throws Exception {
042: Request request = mockRequest();
043:
044: train_getPath(request, "/foo/bar/Baz.gif");
045:
046: replay();
047:
048: Dispatcher d = new AssetDispatcher(null, null, null);
049:
050: assertFalse(d.dispatch(request, null));
051:
052: verify();
053: }
054:
055: @Test
056: public void unprotected_asset() throws Exception {
057: Request request = mockRequest();
058: Response response = mockResponse();
059: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
060: ResourceCache cache = mockResourceCache();
061: ResourceStreamer streamer = mockResourceStreamer();
062:
063: train_getPath(request, SMILEY_CLIENT_URL);
064:
065: train_toResourcePath(aliasManager, SMILEY_CLIENT_URL,
066: SMILEY_PATH);
067:
068: train_requiresDigest(cache, SMILEY, false);
069:
070: train_getDateHeader(request,
071: AssetDispatcher.IF_MODIFIED_SINCE_HEADER, -1);
072:
073: streamer.streamResource(SMILEY);
074:
075: replay();
076:
077: Dispatcher d = new AssetDispatcher(streamer, aliasManager,
078: cache);
079:
080: assertTrue(d.dispatch(request, response));
081:
082: verify();
083: }
084:
085: @Test
086: public void protected_asset_without_an_extension() throws Exception {
087: Request request = mockRequest();
088: Response response = mockResponse();
089: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
090: ResourceCache cache = mockResourceCache();
091: ResourceStreamer streamer = mockResourceStreamer();
092:
093: String clientURL = "/assets/app1/pages/smiley_png";
094: String resourcePath = "org/apache/tapestry/integration/app1/pages/smiley_png";
095:
096: train_getPath(request, clientURL);
097:
098: train_toResourcePath(aliasManager, clientURL, resourcePath);
099:
100: train_requiresDigest(cache,
101: new ClasspathResource(resourcePath), true);
102:
103: response.sendError(eq(HttpServletResponse.SC_FORBIDDEN),
104: contains(resourcePath));
105:
106: replay();
107:
108: Dispatcher d = new AssetDispatcher(streamer, aliasManager,
109: cache);
110:
111: assertTrue(d.dispatch(request, response));
112:
113: verify();
114: }
115:
116: @Test
117: public void protected_asset_with_incorrect_digest_in_url()
118: throws Exception {
119: Request request = mockRequest();
120: Response response = mockResponse();
121: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
122: ResourceCache cache = mockResourceCache();
123: ResourceStreamer streamer = mockResourceStreamer();
124:
125: String clientURL = "/assets/app1/pages/smiley.WRONG.png";
126: String resourcePath = "org/apache/tapestry/integration/app1/pages/smiley.WRONG.png";
127:
128: train_getPath(request, clientURL);
129:
130: train_toResourcePath(aliasManager, clientURL, resourcePath);
131:
132: train_requiresDigest(cache,
133: new ClasspathResource(resourcePath), true);
134:
135: train_getDigest(cache, SMILEY, "RIGHT");
136:
137: response.sendError(eq(HttpServletResponse.SC_FORBIDDEN),
138: contains(SMILEY_PATH));
139:
140: replay();
141:
142: Dispatcher d = new AssetDispatcher(streamer, aliasManager,
143: cache);
144:
145: assertTrue(d.dispatch(request, response));
146:
147: verify();
148: }
149:
150: @Test
151: public void protected_asset_wth_correct_digest_in_url()
152: throws Exception {
153: Request request = mockRequest();
154: Response response = mockResponse();
155: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
156: ResourceCache cache = mockResourceCache();
157: ResourceStreamer streamer = mockResourceStreamer();
158:
159: String clientURL = TapestryConstants.ASSET_PATH_PREFIX
160: + "app1/pages/smiley.RIGHT.png";
161: String resourcePath = "org/apache/tapestry/integration/app1/pages/smiley.RIGHT.png";
162:
163: train_getPath(request, clientURL);
164:
165: train_toResourcePath(aliasManager, clientURL, resourcePath);
166:
167: train_requiresDigest(cache,
168: new ClasspathResource(resourcePath), true);
169:
170: train_getDigest(cache, SMILEY, "RIGHT");
171:
172: train_getDateHeader(request,
173: AssetDispatcher.IF_MODIFIED_SINCE_HEADER, -1);
174:
175: streamer.streamResource(SMILEY);
176:
177: replay();
178:
179: Dispatcher d = new AssetDispatcher(streamer, aliasManager,
180: cache);
181:
182: assertTrue(d.dispatch(request, response));
183:
184: verify();
185: }
186:
187: protected final void train_getDigest(ResourceCache cache,
188: Resource resource, String digest) {
189: expect(cache.getDigest(resource)).andReturn(digest)
190: .atLeastOnce();
191: }
192:
193: @Test
194: public void protected_asset_without_digest() throws Exception {
195: Request request = mockRequest();
196: Response response = mockResponse();
197: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
198: ResourceCache cache = mockResourceCache();
199: ResourceStreamer streamer = mockResourceStreamer();
200:
201: train_getPath(request, SMILEY_CLIENT_URL);
202:
203: train_toResourcePath(aliasManager, SMILEY_CLIENT_URL,
204: SMILEY_PATH);
205:
206: train_requiresDigest(cache, SMILEY, true);
207:
208: response.sendError(eq(HttpServletResponse.SC_FORBIDDEN),
209: contains(SMILEY_PATH));
210:
211: replay();
212:
213: Dispatcher d = new AssetDispatcher(streamer, aliasManager,
214: cache);
215:
216: assertTrue(d.dispatch(request, response));
217:
218: verify();
219: }
220:
221: @Test
222: public void client_cache_upto_date() throws Exception {
223: Request request = mockRequest();
224: Response response = mockResponse();
225: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
226: ResourceCache cache = mockResourceCache();
227: ResourceStreamer streamer = mockResourceStreamer();
228: long now = System.currentTimeMillis();
229:
230: train_getPath(request, SMILEY_CLIENT_URL);
231:
232: train_toResourcePath(aliasManager, SMILEY_CLIENT_URL,
233: SMILEY_PATH);
234:
235: train_requiresDigest(cache, SMILEY, false);
236:
237: train_getDateHeader(request,
238: AssetDispatcher.IF_MODIFIED_SINCE_HEADER, now);
239:
240: train_getTimeModified(cache, SMILEY, now - 1000);
241:
242: response.sendError(HttpServletResponse.SC_NOT_MODIFIED, "");
243:
244: replay();
245:
246: Dispatcher d = new AssetDispatcher(streamer, aliasManager,
247: cache);
248:
249: assertTrue(d.dispatch(request, response));
250:
251: verify();
252: }
253:
254: @Test
255: public void client_cache_out_of_date() throws Exception {
256: Request request = mockRequest();
257: Response response = mockResponse();
258: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
259: ResourceCache cache = mockResourceCache();
260: ResourceStreamer streamer = mockResourceStreamer();
261: long now = System.currentTimeMillis();
262:
263: train_getPath(request, SMILEY_CLIENT_URL);
264:
265: train_toResourcePath(aliasManager, SMILEY_CLIENT_URL,
266: SMILEY_PATH);
267:
268: train_requiresDigest(cache, SMILEY, false);
269:
270: train_getDateHeader(request,
271: AssetDispatcher.IF_MODIFIED_SINCE_HEADER, now - 1000);
272:
273: train_getTimeModified(cache, SMILEY, now + 1000);
274:
275: streamer.streamResource(SMILEY);
276:
277: replay();
278:
279: Dispatcher d = new AssetDispatcher(streamer, aliasManager,
280: cache);
281:
282: assertTrue(d.dispatch(request, response));
283:
284: verify();
285: }
286: }
|