< prev index next >
1 /*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
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 8087112
27 * @library /lib/testlibrary/
28 * @build jdk.testlibrary.SimpleSSLContext ProxyServer
29 * @build TestKit
30 * @compile ../../../com/sun/net/httpserver/LogFilter.java
31 * @compile ../../../com/sun/net/httpserver/FileServerHandler.java
32 * @run main/othervm APIErrors
33 */
34 //package javaapplication16;
35
36 import com.sun.net.httpserver.*;
37 import java.io.IOException;
38 import java.net.*;
39 import java.net.http.*;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.concurrent.*;
43 import java.util.function.Supplier;
44
45 /**
46 * Does stupid things with API, to check appropriate errors/exceptions thrown
47 */
48 public class APIErrors {
49
50 static HttpServer s1 = null;
51 static ExecutorService executor = null;
52 static int port;
53 static HttpClient client;
54 static String httproot, fileuri, fileroot;
55 static List<HttpClient> clients = new LinkedList<>();
56
57 public static void main(String[] args) throws Exception {
58 initServer();
59 fileroot = System.getProperty("test.src") + "/docs";
60
61 client = HttpClient.create().build();
62
63 clients.add(HttpClient.getDefault());
64
65 try {
66 test1();
67 test2();
68 test3();
69 } finally {
70 s1.stop(0);
71 executor.shutdownNow();
72 for (HttpClient client : clients)
73 client.executorService().shutdownNow();
74 }
75 }
76
77 static void checkNonNull(Supplier<?> r) {
78 if (r.get() == null)
79 throw new RuntimeException("Unexpected null return:");
80 }
81
82 static void assertTrue(Supplier<Boolean> r) {
83 if (r.get() == false)
84 throw new RuntimeException("Assertion failure:");
85 }
86
87 // HttpClient.Builder
88 static void test1() throws Exception {
89 System.out.println("Test 1");
90 HttpClient.Builder cb = HttpClient.create();
91 InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 5000);
92 TestKit.assertThrows(IllegalArgumentException.class, () -> cb.priority(-1));
93 TestKit.assertThrows(IllegalArgumentException.class, () -> cb.priority(500));
94 TestKit.assertNotThrows(() -> cb.priority(1));
95 TestKit.assertNotThrows(() -> cb.priority(255));
96 TestKit.assertNotThrows(() -> {
97 clients.add(cb.build());
98 clients.add(cb.build());
99 });
100 }
101
102 static void test2() throws Exception {
103 System.out.println("Test 2");
104 HttpClient.Builder cb = HttpClient.create();
105 InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 5000);
106 cb.proxy(ProxySelector.of(addr));
107 HttpClient c = cb.build();
108 clients.add(c);
109 checkNonNull(()-> {return c.executorService();});
110 assertTrue(()-> {return c.followRedirects() == HttpClient.Redirect.NEVER;});
111 assertTrue(()-> {return !c.authenticator().isPresent();});
112 }
113
114 static URI accessibleURI() {
115 return URI.create(fileuri);
116 }
117
118 static HttpRequest request() {
119 return HttpRequest.create(accessibleURI())
120 .GET();
121 }
122
123 static void test3() throws Exception {
124 System.out.println("Test 3");
125 TestKit.assertThrows(IllegalStateException.class, ()-> {
126 try {
127 HttpRequest r1 = request();
128 HttpResponse resp = r1.response();
129 HttpResponse resp1 = r1.response();
130 } catch (IOException |InterruptedException e) {
131 throw new RuntimeException(e);
132 }
133 });
134
135 TestKit.assertThrows(IllegalStateException.class, ()-> {
136 try {
137 HttpRequest r1 = request();
138 HttpResponse resp = r1.response();
139 HttpResponse resp1 = r1.responseAsync().get();
140 } catch (IOException |InterruptedException | ExecutionException e) {
141 throw new RuntimeException(e);
142 }
143 });
144 TestKit.assertThrows(IllegalStateException.class, ()-> {
145 try {
146 HttpRequest r1 = request();
147 HttpResponse resp1 = r1.responseAsync().get();
148 HttpResponse resp = r1.response();
149 } catch (IOException |InterruptedException | ExecutionException e) {
150 throw new RuntimeException(e);
151 }
152 });
153 }
154
155 static class Auth extends java.net.Authenticator {
156 int count = 0;
157 @Override
158 protected PasswordAuthentication getPasswordAuthentication() {
159 if (count++ == 0) {
160 return new PasswordAuthentication("user", "passwd".toCharArray());
161 } else {
162 return new PasswordAuthentication("user", "goober".toCharArray());
163 }
164 }
165 int count() {
166 return count;
167 }
168 }
169
170 public static void initServer() throws Exception {
171 String root = System.getProperty ("test.src")+ "/docs";
172 InetSocketAddress addr = new InetSocketAddress (0);
173 s1 = HttpServer.create (addr, 0);
174 if (s1 instanceof HttpsServer) {
175 throw new RuntimeException ("should not be httpsserver");
176 }
177 HttpHandler h = new FileServerHandler(root);
178
179 HttpContext c1 = s1.createContext("/files", h);
180
181 executor = Executors.newCachedThreadPool();
182 s1.setExecutor (executor);
183 s1.start();
184
185 port = s1.getAddress().getPort();
186 System.out.println("HTTP server port = " + port);
187 httproot = "http://127.0.0.1:" + port + "/files/";
188 fileuri = httproot + "foo.txt";
189 }
190 }
< prev index next >