< prev index next >

test/java/net/HttpURLConnection/UnmodifiableMaps.java

Print this page




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 7128648

  27  * @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map
  28  */
  29 
  30 import java.io.IOException;
  31 import java.net.InetAddress;
  32 import java.net.InetSocketAddress;
  33 import java.net.URI;
  34 import java.net.HttpURLConnection;
  35 import java.util.Collection;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import java.util.Map;
  39 import com.sun.net.httpserver.HttpExchange;
  40 import com.sun.net.httpserver.HttpHandler;
  41 import com.sun.net.httpserver.HttpServer;
  42 import com.sun.net.httpserver.Headers;

  43 
  44 public class UnmodifiableMaps {
  45 
  46     void test(String[] args) throws Exception {
  47         HttpServer server = startHttpServer();
  48         try {
  49             InetSocketAddress address = server.getAddress();
  50             URI uri = new URI("http://" + InetAddress.getLocalHost().getHostAddress()
  51                               + ":" + address.getPort() + "/foo");
  52             doClient(uri);
  53         } finally {
  54             server.stop(0);
  55         }
  56     }
  57 
  58     void doClient(URI uri) throws Exception {
  59         HttpURLConnection uc = (HttpURLConnection) uri.toURL().openConnection();
  60 
  61         // Test1: getRequestProperties is unmodifiable
  62         System.out.println("Check getRequestProperties");
  63         checkUnmodifiable(uc.getRequestProperties());
  64         uc.addRequestProperty("X", "V");
  65         uc.addRequestProperty("X1", "V1");
  66         checkUnmodifiable(uc.getRequestProperties());
  67 
  68         int resp = uc.getResponseCode();
  69         check(resp == 200,
  70               "Unexpected response code. Expected 200, got " + resp);
  71 
  72         // Test2: getHeaderFields is unmodifiable
  73         System.out.println("Check getHeaderFields");
  74         checkUnmodifiable(uc.getHeaderFields());
  75         // If the implementation does caching, check again.
  76         checkUnmodifiable(uc.getHeaderFields());
  77     }
  78 
  79     // HTTP Server




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 7128648
  27  * @modules jdk.httpserver
  28  * @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map
  29  */
  30 
  31 import java.io.IOException;

  32 import java.net.InetSocketAddress;
  33 import java.net.URI;
  34 import java.net.HttpURLConnection;
  35 import java.util.Collection;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import java.util.Map;
  39 import com.sun.net.httpserver.HttpExchange;
  40 import com.sun.net.httpserver.HttpHandler;
  41 import com.sun.net.httpserver.HttpServer;
  42 import com.sun.net.httpserver.Headers;
  43 import static java.net.Proxy.NO_PROXY;
  44 
  45 public class UnmodifiableMaps {
  46 
  47     void test(String[] args) throws Exception {
  48         HttpServer server = startHttpServer();
  49         try {
  50             InetSocketAddress address = server.getAddress();
  51             URI uri = new URI("http://localhost:" + address.getPort() + "/foo");

  52             doClient(uri);
  53         } finally {
  54             server.stop(0);
  55         }
  56     }
  57 
  58     void doClient(URI uri) throws Exception {
  59         HttpURLConnection uc = (HttpURLConnection) uri.toURL().openConnection(NO_PROXY);
  60 
  61         // Test1: getRequestProperties is unmodifiable
  62         System.out.println("Check getRequestProperties");
  63         checkUnmodifiable(uc.getRequestProperties());
  64         uc.addRequestProperty("X", "V");
  65         uc.addRequestProperty("X1", "V1");
  66         checkUnmodifiable(uc.getRequestProperties());
  67 
  68         int resp = uc.getResponseCode();
  69         check(resp == 200,
  70               "Unexpected response code. Expected 200, got " + resp);
  71 
  72         // Test2: getHeaderFields is unmodifiable
  73         System.out.println("Check getHeaderFields");
  74         checkUnmodifiable(uc.getHeaderFields());
  75         // If the implementation does caching, check again.
  76         checkUnmodifiable(uc.getHeaderFields());
  77     }
  78 
  79     // HTTP Server


< prev index next >