|
|
@ -26,6 +26,7 @@ import static org.junit.Assert.assertThat; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
@ -48,35 +49,41 @@ public class OkHttpRibbonRequestTests { |
|
|
|
String uri = "http://example.com"; |
|
|
|
String uri = "http://example.com"; |
|
|
|
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); |
|
|
|
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); |
|
|
|
headers.add("my-header", "my-value"); |
|
|
|
headers.add("my-header", "my-value"); |
|
|
|
headers.add(HttpEncoding.CONTENT_LENGTH, "5192"); |
|
|
|
// headers.add(HttpEncoding.CONTENT_LENGTH, "5192");
|
|
|
|
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
|
|
|
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
|
|
|
params.add("myparam", "myparamval"); |
|
|
|
params.add("myparam", "myparamval"); |
|
|
|
RibbonCommandContext context = new RibbonCommandContext("example", "GET", uri, false, headers, params, null); |
|
|
|
RibbonCommandContext context = new RibbonCommandContext("example", "GET", uri, |
|
|
|
|
|
|
|
false, headers, params, null, new ArrayList<RibbonRequestCustomizer>()); |
|
|
|
OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context); |
|
|
|
OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context); |
|
|
|
|
|
|
|
|
|
|
|
Request request = httpRequest.toRequest(); |
|
|
|
Request request = httpRequest.toRequest(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat("body is not null", request.body(), is(nullValue())); |
|
|
|
assertThat("body is not null", request.body(), is(nullValue())); |
|
|
|
assertThat("uri is wrong", request.url().toString(), startsWith(uri)); |
|
|
|
assertThat("uri is wrong", request.url().toString(), startsWith(uri)); |
|
|
|
assertThat("my-header is wrong", request.header("my-header"), is(equalTo("my-value"))); |
|
|
|
assertThat("my-header is wrong", request.header("my-header"), |
|
|
|
assertThat("Content-Length is wrong", request.header(HttpEncoding.CONTENT_LENGTH), is(equalTo("5192"))); |
|
|
|
is(equalTo("my-value"))); |
|
|
|
assertThat("myparam is missing", request.url().queryParameter("myparam"), is(equalTo("myparamval"))); |
|
|
|
assertThat("myparam is missing", request.url().queryParameter("myparam"), |
|
|
|
|
|
|
|
is(equalTo("myparamval"))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
// this situation happens, see https://github.com/spring-cloud/spring-cloud-netflix/issues/1042#issuecomment-227723877
|
|
|
|
// this situation happens, see
|
|
|
|
|
|
|
|
// https://github.com/spring-cloud/spring-cloud-netflix/issues/1042#issuecomment-227723877
|
|
|
|
public void testEmptyEntityGet() throws Exception { |
|
|
|
public void testEmptyEntityGet() throws Exception { |
|
|
|
String entityValue = ""; |
|
|
|
String entityValue = ""; |
|
|
|
testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), false, "GET"); |
|
|
|
testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), false, |
|
|
|
|
|
|
|
"GET"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNonEmptyEntityPost() throws Exception { |
|
|
|
public void testNonEmptyEntityPost() throws Exception { |
|
|
|
String entityValue = "abcd"; |
|
|
|
String entityValue = "abcd"; |
|
|
|
testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), true, "POST"); |
|
|
|
testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), true, |
|
|
|
|
|
|
|
"POST"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader, String method) throws IOException { |
|
|
|
void testEntity(String entityValue, ByteArrayInputStream requestEntity, |
|
|
|
|
|
|
|
boolean addContentLengthHeader, String method) throws IOException { |
|
|
|
String lengthString = String.valueOf(entityValue.length()); |
|
|
|
String lengthString = String.valueOf(entityValue.length()); |
|
|
|
Long length = null; |
|
|
|
Long length = null; |
|
|
|
String uri = "http://example.com"; |
|
|
|
String uri = "http://example.com"; |
|
|
@ -97,8 +104,9 @@ public class OkHttpRibbonRequestTests { |
|
|
|
builder.addHeader("from-customizer", "foo"); |
|
|
|
builder.addHeader("from-customizer", "foo"); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
RibbonCommandContext context = new RibbonCommandContext("example", method, uri, false, |
|
|
|
RibbonCommandContext context = new RibbonCommandContext("example", method, uri, |
|
|
|
headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer)); |
|
|
|
false, headers, new LinkedMultiValueMap<String, String>(), requestEntity, |
|
|
|
|
|
|
|
Collections.singletonList(requestCustomizer)); |
|
|
|
context.setContentLength(length); |
|
|
|
context.setContentLength(length); |
|
|
|
OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context); |
|
|
|
OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context); |
|
|
|
|
|
|
|
|
|
|
@ -115,7 +123,8 @@ public class OkHttpRibbonRequestTests { |
|
|
|
if (!method.equalsIgnoreCase("get")) { |
|
|
|
if (!method.equalsIgnoreCase("get")) { |
|
|
|
assertThat("body is null", request.body(), is(notNullValue())); |
|
|
|
assertThat("body is null", request.body(), is(notNullValue())); |
|
|
|
RequestBody body = request.body(); |
|
|
|
RequestBody body = request.body(); |
|
|
|
assertThat("contentLength is wrong", body.contentLength(), is(equalTo((long) entityValue.length()))); |
|
|
|
assertThat("contentLength is wrong", body.contentLength(), |
|
|
|
|
|
|
|
is(equalTo((long) entityValue.length()))); |
|
|
|
Buffer content = new Buffer(); |
|
|
|
Buffer content = new Buffer(); |
|
|
|
body.writeTo(content); |
|
|
|
body.writeTo(content); |
|
|
|
String string = content.readByteString().utf8(); |
|
|
|
String string = content.readByteString().utf8(); |
|
|
@ -123,4 +132,3 @@ public class OkHttpRibbonRequestTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|