1 | // Copyright (C) 2009 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.lexer; |
16 | |
17 | import com.google.caja.util.CajaTestCase; |
18 | |
19 | import java.io.ByteArrayInputStream; |
20 | import java.io.InputStream; |
21 | import java.net.URI; |
22 | import java.net.URL; |
23 | import java.net.URLConnection; |
24 | |
25 | /** |
26 | * @author ihab.awad@gmail.com (Ihab Awad) |
27 | */ |
28 | public class FetchedDataTest extends CajaTestCase { |
29 | private static class TestURLConnection extends URLConnection { |
30 | private String data; |
31 | private String contentType; |
32 | private boolean connected; |
33 | |
34 | public TestURLConnection(URL url, String data, String contentType) { |
35 | super(url); |
36 | this.data = data; |
37 | this.contentType = contentType; |
38 | } |
39 | |
40 | @Override |
41 | public void connect() { |
42 | assertFalse(connected); |
43 | connected = true; |
44 | } |
45 | |
46 | @Override |
47 | public String getContentType() { |
48 | return contentType; |
49 | } |
50 | |
51 | @Override |
52 | public InputStream getInputStream() { |
53 | assertTrue(connected); |
54 | return new ByteArrayInputStream(data.getBytes()); |
55 | } |
56 | } |
57 | |
58 | private URL testUrl; |
59 | |
60 | @Override |
61 | public void setUp() throws Exception { |
62 | super.setUp(); |
63 | testUrl = URI.create("http://www.example.com/").toURL(); |
64 | } |
65 | |
66 | public final void testSimpleContent() throws Exception { |
67 | FetchedData fd = FetchedData.fromConnection( |
68 | new TestURLConnection(testUrl, "abcdef", "text/html")); |
69 | assertEquals("text/html", fd.getContentType()); |
70 | assertEquals("abcdef", new String(fd.getByteContent())); |
71 | } |
72 | |
73 | private void assertCharSet( |
74 | String expectedCharSet, String urlConnectionContentType) |
75 | throws Exception { |
76 | String testContent = "abcdef"; |
77 | FetchedData fd = FetchedData.fromConnection( |
78 | new TestURLConnection(testUrl, testContent, urlConnectionContentType)); |
79 | assertEquals(testContent, new String(fd.getByteContent())); |
80 | assertEquals(urlConnectionContentType, fd.getContentType()); |
81 | assertEquals(expectedCharSet, fd.getCharSet()); |
82 | } |
83 | |
84 | public final void testCharSetParsing() throws Exception { |
85 | assertCharSet( |
86 | "iso-8859-1", |
87 | "text/html;charset=iso-8859-1"); |
88 | assertCharSet( |
89 | "iso-8859-1", |
90 | " text/html; boo=baz; charset=iso-8859-1 "); |
91 | assertCharSet( |
92 | "iso-8859-1", |
93 | "text/html;boo=\"baz\";charset=\"iso\\-8859\\-1\";bar=\"b\\\"ip\""); |
94 | assertCharSet( |
95 | "", |
96 | "text/html;boo=baz; charset = ;bar=bip"); |
97 | assertCharSet( |
98 | "", |
99 | "text/html;boo=baz;charset;bar=bip"); |
100 | assertCharSet( |
101 | "", |
102 | ""); |
103 | assertCharSet( |
104 | "", |
105 | null); |
106 | } |
107 | } |