blob: 41f4b396b9508cc8224951c054a5c07f1dcd5d23 [file] [log] [blame]
Narayan Kamathcb318c62013-11-12 16:01:55 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package tests.api.internal.net.www.protocol.file;
18
Narayan Kamath1399e0b2013-11-13 13:05:58 +000019import junit.framework.TestCase;
Narayan Kamathcb318c62013-11-12 16:01:55 +000020import java.io.IOException;
21import java.net.URL;
22import java.net.URLConnection;
Narayan Kamathcb318c62013-11-12 16:01:55 +000023import libcore.net.url.FileURLConnection;
24
25/**
26 * Tests for <code>FileURLConnection</code> class constructors and methods.
27 */
28public class FileURLConnectionTest extends TestCase {
29
30 static String getContentType(String fileName) throws IOException {
Narayan Kamath1399e0b2013-11-13 13:05:58 +000031 String resourceName = "resources/" + fileName;
Narayan Kamathcb318c62013-11-12 16:01:55 +000032 URL url = ClassLoader.getSystemClassLoader().getResource(resourceName);
33 assertNotNull("Cannot find test resource " + resourceName, url);
34 return new FileURLConnection(url).getContentType();
35 }
36
37 public void testGetContentType() throws IOException {
38 // Regression for HARMONY-4699
39 assertEquals("application/rtf", getContentType("test.rtf"));
40 assertEquals("text/plain", getContentType("test.java"));
41 // RI would return "content/unknown"
42 assertEquals("application/msword", getContentType("test.doc"));
43 assertEquals("text/html", getContentType("test.htx"));
44 assertEquals("application/xml", getContentType("test.xml"));
45 assertEquals("text/plain", getContentType("."));
46 }
47
48 public void testGetInputStream() throws IOException {
49 // Regression for Harmony-5737
Narayan Kamath1399e0b2013-11-13 13:05:58 +000050 String resourceName = "resources/" + "test.rtf";
Narayan Kamathcb318c62013-11-12 16:01:55 +000051 URL url = ClassLoader.getSystemClassLoader().getResource(resourceName);
Narayan Kamath1399e0b2013-11-13 13:05:58 +000052 assertNotNull(url);
Narayan Kamathcb318c62013-11-12 16:01:55 +000053 URL anchorUrl = new URL(url, "#anchor");
54 assertNotNull("Cannot find test resource " + resourceName, anchorUrl);
55
56 FileURLConnection conn = new FileURLConnection(anchorUrl);
57 assertNotNull(conn.getInputStream());
58
59 // Regression for Harmony-5779
60 String localURLString = "file://localhost/" + url.getFile();
61 URL localURL = new URL(localURLString);
62 conn = new FileURLConnection(localURL);
63 assertNotNull(conn.getInputStream());
64 assertEquals("file", conn.getURL().getProtocol());
65 }
66
67 public void testHeaderFunctions() throws IOException {
Narayan Kamath1399e0b2013-11-13 13:05:58 +000068 String resourceName = "resources/test.rtf"; //folder name
Narayan Kamathcb318c62013-11-12 16:01:55 +000069 URL url = ClassLoader.getSystemClassLoader().getResource(resourceName);
70 FileURLConnection conn = new FileURLConnection(url);
71 assertNotNull(conn.getInputStream());
72 assertEquals(conn.getContentType(), conn.getHeaderField("content-type"));
73
Narayan Kamath1399e0b2013-11-13 13:05:58 +000074 resourceName = "resources/" + "test.rtf";
Narayan Kamathcb318c62013-11-12 16:01:55 +000075 url = ClassLoader.getSystemClassLoader().getResource(resourceName);
76 conn = new FileURLConnection(url);
77 assertNotNull(conn.getInputStream());
78 assertEquals(conn.getContentType(), conn.getHeaderField("content-type"));
79 assertEquals(Integer.toString(conn.getContentLength()), conn.getHeaderField("content-length"));
80 assertEquals(conn.getHeaderField(0), conn.getHeaderField("content-type"));
81 assertEquals(conn.getHeaderField(1), conn.getHeaderField("content-length"));
82 assertEquals(conn.getHeaderField(2), conn.getHeaderField("last-modified"));
83 assertEquals("last-modified", conn.getHeaderFieldKey(2));
84 assertEquals("content-length", conn.getHeaderFieldKey(1));
85 assertEquals("content-type", conn.getHeaderFieldKey(0));
86 }
87
88 public void testHeader_BoundaryCheck() throws IOException {
Narayan Kamath1399e0b2013-11-13 13:05:58 +000089 String resourceName = "resources/test.rtf";
Narayan Kamathcb318c62013-11-12 16:01:55 +000090 URL url = ClassLoader.getSystemClassLoader().getResource(resourceName);
91 URLConnection urlConnection = url.openConnection();
92 assertNull(urlConnection.getHeaderField(Integer.MIN_VALUE));
93 assertNull(urlConnection.getHeaderField(Integer.MAX_VALUE));
94 assertNull(urlConnection.getHeaderFieldKey(Integer.MIN_VALUE));
95 assertNull(urlConnection.getHeaderFieldKey(Integer.MAX_VALUE));
96 assertNull(urlConnection.getHeaderField(null));
97 }
98}