Watcher: Fix proxy xcontent serialization (elastic/elasticsearch#3364)

Calling to xcontent in a HttpRequest, with a proxy enabled, lead to
serialization exceptions, resulting in failing to write the watch
history.

Closes elastic/elasticsearch#3334

Original commit: elastic/x-pack-elasticsearch@a04dff686c
This commit is contained in:
Alexander Reelsen 2016-09-07 16:29:15 +02:00 committed by GitHub
parent a296e31a7c
commit 0f571685b9
2 changed files with 57 additions and 14 deletions

View File

@ -30,6 +30,7 @@ import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableMap;
@ -166,7 +167,7 @@ public class HttpRequest implements ToXContent {
HttpRequest.Field.READ_TIMEOUT_HUMAN.getPreferredName(), readTimeout);
}
if (proxy != null) {
builder.field(Field.PROXY.getPreferredName(), proxy);
proxy.toXContent(builder, params);
}
return builder.endObject();
}
@ -195,19 +196,7 @@ public class HttpRequest implements ToXContent {
@Override
public int hashCode() {
int result = host.hashCode();
result = 31 * result + port;
result = 31 * result + scheme.hashCode();
result = 31 * result + method.hashCode();
result = 31 * result + (path != null ? path.hashCode() : 0);
result = 31 * result + params.hashCode();
result = 31 * result + headers.hashCode();
result = 31 * result + (auth != null ? auth.hashCode() : 0);
result = 31 * result + (connectionTimeout != null ? connectionTimeout.hashCode() : 0);
result = 31 * result + (readTimeout != null ? readTimeout.hashCode() : 0);
result = 31 * result + (body != null ? body.hashCode() : 0);
result = 31 * result + (proxy != null ? proxy.hashCode() : 0);
return result;
return Objects.hash(host, port, scheme, method, path, params, headers, auth, connectionTimeout, readTimeout, body, proxy);
}
@Override

View File

@ -5,7 +5,9 @@
*/
package org.elasticsearch.xpack.common.http;
import com.carrotsearch.randomizedtesting.annotations.Repeat;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -14,6 +16,10 @@ import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.common.http.HttpRequest;
import org.elasticsearch.xpack.common.http.Scheme;
import org.elasticsearch.xpack.common.http.auth.HttpAuthRegistry;
import org.elasticsearch.xpack.common.http.auth.basic.BasicAuth;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.containsString;
@ -65,6 +71,54 @@ public class HttpRequestTests extends ESTestCase {
}
}
public void testXContentSerialization() throws Exception {
final HttpRequest.Builder builder;
if (randomBoolean()) {
builder = HttpRequest.builder();
builder.fromUrl("http://localhost:9200/generic/createevent");
} else {
builder = HttpRequest.builder("localhost", 9200);
if (randomBoolean()) {
builder.scheme(randomFrom(Scheme.values()));
if (usually()) {
builder.path(randomAsciiOfLength(50));
}
}
}
if (usually()) {
builder.method(randomFrom(HttpMethod.values()));
}
if (randomBoolean()) {
builder.setParam(randomAsciiOfLength(10), randomAsciiOfLength(10));
if (randomBoolean()) {
builder.setParam(randomAsciiOfLength(10), randomAsciiOfLength(10));
}
}
if (randomBoolean()) {
builder.setHeader(randomAsciiOfLength(10), randomAsciiOfLength(10));
if (randomBoolean()) {
builder.setHeader(randomAsciiOfLength(10), randomAsciiOfLength(10));
}
}
if (randomBoolean()) {
builder.auth(new BasicAuth(randomAsciiOfLength(10), randomAsciiOfLength(20).toCharArray()));
}
if (randomBoolean()) {
builder.body(randomAsciiOfLength(200));
}
if (randomBoolean()) {
builder.connectionTimeout(TimeValue.parseTimeValue(randomTimeValue(), "my.setting"));
}
if (randomBoolean()) {
builder.readTimeout(TimeValue.parseTimeValue(randomTimeValue(), "my.setting"));
}
if (randomBoolean()) {
builder.proxy(new HttpProxy(randomAsciiOfLength(10), randomIntBetween(1024, 65000)));
}
builder.build().toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS);
}
private void assertThatManualBuilderEqualsParsingFromUrl(String url, HttpRequest.Builder builder) throws Exception {
XContentBuilder urlContentBuilder = jsonBuilder().startObject().field("url", url).endObject();
XContentParser urlContentParser = JsonXContent.jsonXContent.createParser(urlContentBuilder.bytes());