1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.http;
20
21 import org.apache.hadoop.net.NetUtils;
22 import org.apache.hadoop.security.authorize.AccessControlList;
23 import org.junit.Assert;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.http.HttpServer.Builder;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.URI;
31 import java.net.URL;
32 import java.net.MalformedURLException;
33
34
35
36
37
38 public class HttpServerFunctionalTest extends Assert {
39
40 public static final String TEST_BUILD_WEBAPPS = "test.build.webapps";
41
42 private static final String BUILD_WEBAPPS_DIR = "build/test/webapps";
43
44
45 private static final String TEST = "test";
46
47
48
49
50
51
52
53
54
55
56 public static HttpServer createTestServer() throws IOException {
57 prepareTestWebapp();
58 return createServer(TEST);
59 }
60
61
62
63
64
65
66
67
68
69
70 public static HttpServer createTestServer(Configuration conf)
71 throws IOException {
72 prepareTestWebapp();
73 return createServer(TEST, conf);
74 }
75
76 public static HttpServer createTestServer(Configuration conf, AccessControlList adminsAcl)
77 throws IOException {
78 prepareTestWebapp();
79 return createServer(TEST, conf, adminsAcl);
80 }
81
82
83
84
85
86
87
88
89
90
91 public static HttpServer createTestServer(Configuration conf,
92 String[] pathSpecs) throws IOException {
93 prepareTestWebapp();
94 return createServer(TEST, conf, pathSpecs);
95 }
96
97
98
99
100
101
102 protected static void prepareTestWebapp() {
103 String webapps = System.getProperty(TEST_BUILD_WEBAPPS, BUILD_WEBAPPS_DIR);
104 File testWebappDir = new File(webapps +
105 File.separatorChar + TEST);
106 try {
107 if (!testWebappDir.exists()) {
108 fail("Test webapp dir " + testWebappDir.getCanonicalPath() + " missing");
109 }
110 }
111 catch (IOException e) {
112 }
113 }
114
115
116
117
118
119
120
121
122 public static HttpServer createServer(String host, int port)
123 throws IOException {
124 prepareTestWebapp();
125 return new HttpServer.Builder().setName(TEST)
126 .addEndpoint(URI.create("http://" + host + ":" + port))
127 .setFindPort(true).build();
128 }
129
130
131
132
133
134
135
136 public static HttpServer createServer(String webapp) throws IOException {
137 return localServerBuilder(webapp).setFindPort(true).build();
138 }
139
140
141
142
143
144
145
146 public static HttpServer createServer(String webapp, Configuration conf)
147 throws IOException {
148 return localServerBuilder(webapp).setFindPort(true).setConf(conf).build();
149 }
150
151 public static HttpServer createServer(String webapp, Configuration conf, AccessControlList adminsAcl)
152 throws IOException {
153 return localServerBuilder(webapp).setFindPort(true).setConf(conf).setACL(adminsAcl).build();
154 }
155
156 private static Builder localServerBuilder(String webapp) {
157 return new HttpServer.Builder().setName(webapp).addEndpoint(
158 URI.create("http://localhost:0"));
159 }
160
161
162
163
164
165
166
167
168
169 public static HttpServer createServer(String webapp, Configuration conf,
170 String[] pathSpecs) throws IOException {
171 return localServerBuilder(webapp).setFindPort(true).setConf(conf).setPathSpec(pathSpecs).build();
172 }
173
174
175
176
177
178
179
180
181
182 public static HttpServer createAndStartTestServer() throws IOException {
183 HttpServer server = createTestServer();
184 server.start();
185 return server;
186 }
187
188
189
190
191
192
193 public static void stop(HttpServer server) throws Exception {
194 if (server != null) {
195 server.stop();
196 }
197 }
198
199
200
201
202
203
204
205 public static URL getServerURL(HttpServer server)
206 throws MalformedURLException {
207 assertNotNull("No server", server);
208 return new URL("http://"
209 + NetUtils.getHostPortString(server.getConnectorAddress(0)));
210 }
211
212
213
214
215
216
217
218 protected static String readOutput(URL url) throws IOException {
219 StringBuilder out = new StringBuilder();
220 InputStream in = url.openConnection().getInputStream();
221 byte[] buffer = new byte[64 * 1024];
222 int len = in.read(buffer);
223 while (len > 0) {
224 out.append(new String(buffer, 0, len));
225 len = in.read(buffer);
226 }
227 return out.toString();
228 }
229 }