-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace Request#setHeaders with addHeader #30588
Merged
javanna
merged 15 commits into
elastic:master
from
javanna:enhancement/request_add_headers
May 22, 2018
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4d53709
Replace Request#setHeaders with addHeaders
javanna 7ce5692
remove empty line
javanna 608b139
fix more issues
javanna 299cf3b
Merge branch 'master' into enhancement/request_add_headers
javanna d953894
Remove varargs argument, replace addHeaders with addHeader
javanna f4116ad
fixes
javanna 5b91a56
remove unused imports
javanna c9a560a
addHeader(String,String)
javanna 6e456e9
fix tests
javanna e3c74c2
rename test method
javanna 5a395f9
Merge branch 'master' into enhancement/request_add_headers
javanna a2721b5
add javadocs for ReqHeader
javanna ed19946
fix compile error
javanna 61e4361
fix compile errors
javanna 08e1d41
fix failing test
javanna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,6 @@ | |
|
||
package org.elasticsearch.client; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.http.Header; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.entity.ByteArrayEntity; | ||
|
@@ -33,7 +28,13 @@ | |
import org.apache.http.nio.entity.NStringEntity; | ||
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
@@ -127,31 +128,26 @@ public void testSetJsonEntity() throws IOException { | |
assertEquals(json, new String(os.toByteArray(), ContentType.APPLICATION_JSON.getCharset())); | ||
} | ||
|
||
public void testSetHeaders() { | ||
public void testAddHeaders() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
final String method = randomFrom(new String[] {"GET", "PUT", "POST", "HEAD", "DELETE"}); | ||
final String endpoint = randomAsciiLettersOfLengthBetween(1, 10); | ||
Request request = new Request(method, endpoint); | ||
|
||
try { | ||
request.setHeaders((Header[]) null); | ||
fail("expected failure"); | ||
} catch (NullPointerException e) { | ||
assertEquals("headers cannot be null", e.getMessage()); | ||
} | ||
|
||
try { | ||
request.setHeaders(new Header [] {null}); | ||
request.addHeader(null); | ||
fail("expected failure"); | ||
} catch (NullPointerException e) { | ||
assertEquals("header cannot be null", e.getMessage()); | ||
} | ||
|
||
Header[] headers = new Header[between(0, 5)]; | ||
for (int i = 0; i < headers.length; i++) { | ||
headers[i] = new BasicHeader(randomAsciiAlphanumOfLength(3), randomAsciiAlphanumOfLength(3)); | ||
int numHeaders = between(0, 5); | ||
Set<Header> headers = new HashSet<>(); | ||
for (int i = 0; i < numHeaders; i++) { | ||
BasicHeader header = new BasicHeader(randomAsciiAlphanumOfLengthBetween(5, 10), randomAsciiAlphanumOfLength(3)); | ||
headers.add(header); | ||
request.addHeader(header); | ||
} | ||
request.setHeaders(headers); | ||
assertArrayEquals(headers, request.getHeaders()); | ||
assertEquals(headers, new HashSet<>(request.getHeaders())); | ||
} | ||
|
||
public void testEqualsAndHashCode() { | ||
|
@@ -168,7 +164,7 @@ public void testEqualsAndHashCode() { | |
assertNotEquals(mutant, request); | ||
} | ||
|
||
private Request randomRequest() { | ||
private static Request randomRequest() { | ||
Request request = new Request( | ||
randomFrom(new String[] {"GET", "PUT", "DELETE", "POST", "HEAD", "OPTIONS"}), | ||
randomAsciiAlphanumOfLength(5)); | ||
|
@@ -192,11 +188,9 @@ private Request randomRequest() { | |
|
||
if (randomBoolean()) { | ||
int headerCount = between(1, 5); | ||
Header[] headers = new Header[headerCount]; | ||
for (int i = 0; i < headerCount; i++) { | ||
headers[i] = new BasicHeader(randomAsciiAlphanumOfLength(3), randomAsciiAlphanumOfLength(3)); | ||
request.addHeader(new BasicHeader(randomAsciiAlphanumOfLength(3), randomAsciiAlphanumOfLength(3))); | ||
} | ||
request.setHeaders(headers); | ||
} | ||
|
||
if (randomBoolean()) { | ||
|
@@ -206,13 +200,13 @@ private Request randomRequest() { | |
return request; | ||
} | ||
|
||
private Request copy(Request request) { | ||
private static Request copy(Request request) { | ||
Request copy = new Request(request.getMethod(), request.getEndpoint()); | ||
copyMutables(request, copy); | ||
return copy; | ||
} | ||
|
||
private Request mutate(Request request) { | ||
private static Request mutate(Request request) { | ||
if (randomBoolean()) { | ||
// Mutate request or method but keep everything else constant | ||
Request mutant = randomBoolean() | ||
|
@@ -231,11 +225,7 @@ private Request mutate(Request request) { | |
mutant.setJsonEntity("mutant"); // randomRequest can't produce this value | ||
return mutant; | ||
case 2: | ||
if (mutant.getHeaders().length > 0) { | ||
mutant.setHeaders(new Header[0]); | ||
} else { | ||
mutant.setHeaders(new BasicHeader("extra", "m")); | ||
} | ||
mutant.addHeader(new BasicHeader("extra", "m")); | ||
return mutant; | ||
case 3: | ||
mutant.setHttpAsyncResponseConsumerFactory(new HeapBufferedResponseConsumerFactory(5)); | ||
|
@@ -245,12 +235,14 @@ private Request mutate(Request request) { | |
} | ||
} | ||
|
||
private void copyMutables(Request from, Request to) { | ||
private static void copyMutables(Request from, Request to) { | ||
for (Map.Entry<String, String> param : from.getParameters().entrySet()) { | ||
to.addParameter(param.getKey(), param.getValue()); | ||
} | ||
to.setEntity(from.getEntity()); | ||
to.setHeaders(from.getHeaders()); | ||
for (Header header : from.getHeaders()) { | ||
to.addHeader(header); | ||
} | ||
to.setHttpAsyncResponseConsumerFactory(from.getHttpAsyncResponseConsumerFactory()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
addHeader(String key, String value)
instead? I don't see us using anything butBasicHeader
anyway and I like exposing a little less of httpclient publicly. And I like not having to import another class everywhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea I think it's a good idea