2 * Copyright (C) 2010 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.i18n.addressinput;
19 import com.android.i18n.addressinput.JsonpRequestBuilder.AsyncCallback;
20 import com.android.i18n.addressinput.testing.AsyncTestCase;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.ServerSocket;
26 import java.net.Socket;
27 import java.net.SocketTimeoutException;
29 public class JsonpRequestBuilderTest extends AsyncTestCase {
30 private JsonpRequestBuilder builder;
34 builder = new JsonpRequestBuilder();
37 public void testRequestObject() throws Exception {
38 delayTestFinish(4000);
39 builder.setTimeout(2000);
41 String url = HttpServer.execute(1000, "{\"id\": \"data\"}");
43 builder.requestObject(url, new AsyncCallback<JsoMap>() {
45 public void onFailure(Throwable caught) {
46 fail(caught.toString());
50 public void onSuccess(JsoMap result) {
51 assertNotNull(result);
52 assertEquals("data", result.get("id"));
58 public void testSetTimeout() throws Exception {
59 delayTestFinish(4000);
60 builder.setTimeout(1000);
62 String url = HttpServer.execute(2000, "Fubar");
64 builder.requestObject(url, new AsyncCallback<JsoMap>() {
66 public void onFailure(Throwable caught) {
67 assertNotNull(caught);
68 assertTrue(caught instanceof SocketTimeoutException);
73 public void onSuccess(JsoMap result) {
74 fail("The request should have timed out.");
79 public void testUrlEncoding() throws Exception {
80 delayTestFinish(4000);
81 builder.setTimeout(2000);
83 String urlBase = HttpServer.execute(1000, "{\"id\": \"data\"}");
84 String url = urlBase + "address/data/VN/B\u1EAFc K\u1EA1n";
86 builder.requestObject(url, new AsyncCallback<JsoMap>() {
88 public void onFailure(Throwable caught) {
89 fail(caught.toString());
93 public void onSuccess(JsoMap result) {
94 assertNotNull(result);
95 assertEquals("data", result.get("id"));
102 * Simple implementation of an HTTP server.
104 private static class HttpServer extends Thread {
106 * Start an HTTP server that will serve one request and then terminate.
108 * @param timeoutMillis
109 * Wait this long before answering a request.
111 * Reply to any request with this response.
112 * @return The URL to the server.
113 * @throws IOException
115 public static String execute(long timeoutMillis, String response) throws IOException {
116 HttpServer server = new HttpServer(timeoutMillis, response);
118 return "http://localhost:" + server.serverSocket.getLocalPort() + "/";
124 Socket clientSocket = serverSocket.accept();
126 synchronized (this) {
129 } catch (InterruptedException e) {
130 throw new RuntimeException(e);
132 InputStream inputStream = clientSocket.getInputStream();
133 inputStream.read(new byte[1024]); // Discard input.
134 OutputStream outputStream = clientSocket.getOutputStream();
135 outputStream.write(response);
136 outputStream.close();
138 clientSocket.close();
139 serverSocket.close();
140 } catch (IOException e) {
141 throw new RuntimeException(e);
145 private HttpServer(long waitMillis, String response) throws IOException {
146 this.waitMillis = waitMillis;
147 this.response = (header + response).getBytes();
148 serverSocket = new ServerSocket(0);
151 private long waitMillis;
152 private byte[] response;
153 private ServerSocket serverSocket;
155 private static final String header = "HTTP/1.0 200 OK\nContent-Type: text/plain\n\n";