1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.communication.http;
import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation;
import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl;
import junit.framework.TestCase;
import org.codehaus.jettison.json.JSONObject;
public class JsonAsyncHttpClientTest extends TestCase {
public void testJSONInJSONOut() throws Exception {
DummyAsyncHttpClient dummy = new DummyAsyncHttpClient(
new HttpResult().setContent(new JSONObject().put("bar", 42)));
JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy);
client.addJsonContentType(true);
client.verifyRequestContentAsJson(true);
HttpRequest r = new HttpRequest();
r.setPostContent(new JSONObject().put("foo", 34));
AsyncOperation<JsonHttpResult> result = client.execute(r);
assertEquals(new JSONObject().put("bar", 42).toString(), result.getResult().getJson().toString());
assertTrue(result.isSuccess());
result.toString();
client.close();
}
public void testStringInJSONOut() throws Exception {
DummyAsyncHttpClient dummy = new DummyAsyncHttpClient(
new HttpResult().setContent(new JSONObject().put("bar", 42).toString()));
JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy);
HttpRequest r = new HttpRequest();
r.setPostContent(new JSONObject().put("foo", 34).toString());
AsyncOperation<JsonHttpResult> result = client.execute(r);
assertEquals(new JSONObject().put("bar", 42).toString(), result.getResult().getJson().toString());
}
public void testIllegalJsonIn() throws Exception {
DummyAsyncHttpClient dummy = new DummyAsyncHttpClient(
new HttpResult().setContent(new JSONObject().put("bar", 42)));
JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy);
try {
HttpRequest r = new HttpRequest();
r.setPostContent("my illegal json");
client.execute(r);
assertTrue(false);
} catch (Exception e) {
}
}
public void testIllegalJSONOut() throws Exception {
DummyAsyncHttpClient dummy = new DummyAsyncHttpClient(
new HttpResult().setContent("my illegal json"));
JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy);
HttpRequest r = new HttpRequest();
r.setPostContent(new JSONObject().put("foo", 34).toString());
AsyncOperation<JsonHttpResult> result = client.execute(r);
assertEquals("{\"error\":\"Invalid JSON in output: A JSONObject text must begin with '{' at character 1 of my illegal json\",\"output\":\"my illegal json\"}", result.getResult().getJson().toString());
}
public void testEmptyReply() throws Exception {
class Client implements AsyncHttpClient<HttpResult> {
AsyncOperationImpl<HttpResult> lastOp;
@Override
public AsyncOperation<HttpResult> execute(HttpRequest r) {
return lastOp = new AsyncOperationImpl<>(r.toString());
}
@Override
public void close() {
}
};
Client client = new Client();
JsonAsyncHttpClient jsonClient = new JsonAsyncHttpClient(client);
AsyncOperation<JsonHttpResult> op = jsonClient.execute(new HttpRequest());
client.lastOp.setResult(null);
assertNull(op.getResult());
}
public void testNotVerifyingJson() throws Exception {
DummyAsyncHttpClient dummy = new DummyAsyncHttpClient(
new HttpResult().setContent(new JSONObject().put("bar", 42)));
JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy);
client.addJsonContentType(true);
client.verifyRequestContentAsJson(false);
HttpRequest r = new HttpRequest();
r.setPostContent(new JSONObject().put("foo", 34));
AsyncOperation<JsonHttpResult> result = client.execute(r);
assertEquals(new JSONObject().put("bar", 42).toString(), result.getResult().getJson().toString());
assertTrue(result.isSuccess());
result.toString();
client.close();
}
}
|