use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest in project vespa by vespa-engine.
the class StateRestAPITest method testClusterState.
@Test
public void testClusterState() throws Exception {
setupDummyStateApi();
HttpResult result = execute(new HttpRequest().setPath("/cluster/v2/foo"));
assertEquals(result.toString(true), 200, result.getHttpReturnCode());
assertEquals(result.toString(true), "application/json", result.getHeader("Content-Type"));
String expected = "{\"node\": {\n" + " \"1\": {\"link\": \"\\/cluster\\/v2\\/foo\\/1\"},\n" + " \"3\": {\"link\": \"\\/cluster\\/v2\\/foo\\/3\"}\n" + "}}";
assertEquals(expected, ((JSONObject) result.getContent()).toString(2));
}
use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest in project vespa by vespa-engine.
the class StateRestAPITest method testRuntimeException.
@Test
public void testRuntimeException() throws Exception {
setupDummyStateApi();
stateApi.induceException(new RuntimeException("Moahaha"));
HttpResult result = execute(new HttpRequest().setPath("/cluster/v2"));
assertEquals(result.toString(true), 500, result.getHttpReturnCode());
assertEquals(result.toString(true), "Failed to process request", result.getHttpReturnCodeDescription());
assertEquals(result.toString(true), "application/json", result.getHeader("Content-Type"));
String expected = "{\"message\":\"java.lang.RuntimeException: Moahaha\"}";
assertEquals(expected, result.getContent().toString());
}
use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest in project vespa by vespa-engine.
the class ApacheHttpInstance method execute.
/**
* This function is not threadsafe.
*/
public HttpResult execute(HttpRequest r) {
HttpRequest.HttpOp op = r.getHttpOperation();
if (op == null) {
if (r.getPostContent() != null) {
log.fine("Request " + r + " has no HTTP function specified. Assuming POST as post content is set.");
op = HttpRequest.HttpOp.POST;
} else {
log.fine("Request " + r + " has no HTTP function specified. Assuming GET as post content is set.");
op = HttpRequest.HttpOp.GET;
}
}
if (r.getPostContent() != null && !(op.equals(HttpRequest.HttpOp.POST) || op.equals(HttpRequest.HttpOp.PUT))) {
throw new IllegalStateException("A " + op + " operation can't have content");
}
try {
HttpHost target = new HttpHost(r.getHost(), r.getPort(), "http");
org.apache.http.HttpRequest req = null;
String path = r.getPath();
int uriOption = 0;
for (HttpRequest.KeyValuePair option : r.getUrlOptions()) {
path += (++uriOption == 1 ? '?' : '&');
path += option.getKey() + '=' + option.getValue();
}
switch(op) {
case POST:
HttpPost post = new HttpPost(path);
if (r.getPostContent() != null) {
post.setEntity(new StringEntity(r.getPostContent().toString()));
}
req = post;
break;
case GET:
req = new HttpGet(path);
break;
case PUT:
HttpPut put = new HttpPut(path);
put.setEntity(new StringEntity(r.getPostContent().toString()));
req = put;
break;
case DELETE:
req = new HttpDelete(path);
break;
}
for (HttpRequest.KeyValuePair header : r.getHeaders()) {
req.addHeader(header.getKey(), header.getValue());
}
HttpResponse rsp = client.execute(target, req);
HttpEntity entity = rsp.getEntity();
HttpResult result = new HttpResult();
result.setHttpCode(rsp.getStatusLine().getStatusCode(), rsp.getStatusLine().getReasonPhrase());
if (entity != null) {
result.setContent(EntityUtils.toString(entity));
}
for (Header header : rsp.getAllHeaders()) {
result.addHeader(header.getName(), header.getValue());
}
return result;
} catch (Exception e) {
HttpResult result = new HttpResult();
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
writer.flush();
result.setHttpCode(500, "Got exception " + writer.toString() + " when sending message.");
return result;
}
}
use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest in project vespa by vespa-engine.
the class StateRestAPITest method testTopLevelList.
@Test
public void testTopLevelList() throws Exception {
setupDummyStateApi();
HttpResult result = execute(new HttpRequest().setPath("/cluster/v2"));
assertEquals(result.toString(true), 200, result.getHttpReturnCode());
assertEquals(result.toString(true), "application/json", result.getHeader("Content-Type"));
String expected = "{\"cluster\": {\n" + " \"foo\": {\"link\": \"\\/cluster\\/v2\\/foo\"},\n" + " \"bar\": {\"link\": \"\\/cluster\\/v2\\/bar\"}\n" + "}}";
assertEquals(expected, ((JSONObject) result.getContent()).toString(2));
}
use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest in project vespa by vespa-engine.
the class StateRestAPITest method testNodeState.
@Test
public void testNodeState() throws Exception {
setupDummyStateApi();
HttpResult result = execute(new HttpRequest().setPath("/cluster/v2/foo/3"));
assertEquals(result.toString(true), 200, result.getHttpReturnCode());
assertEquals(result.toString(true), "application/json", result.getHeader("Content-Type"));
String expected = "{\n" + " \"attributes\": {\"group\": \"mygroup\"},\n" + " \"state\": {\"current\": {\n" + " \"state\": \"up\",\n" + " \"reason\": \"\"\n" + " }},\n" + " \"metrics\": {\"doc-count\": 8}\n" + "}";
assertEquals(expected, ((JSONObject) result.getContent()).toString(2));
}
Aggregations