public LowLevelHttpRequest buildRequest(String name, String url) {
actualCalls++;
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
response.addHeader("Content-Type", "multipart/mixed; boundary=" + RESPONSE_BOUNDARY);
String contentType =
testBinary ? "application/x-protobuf" : "application/json; charset=UTF-8";
byte[] content1 = testBinary ? MockData.Class1.newBuilder()
.setId(TEST_ID)
.setKind(TEST_KIND)
.build().toByteArray()
: utf8Encode("{\n \"id\": \"" + TEST_ID + "\",\n \"kind\": \""
+ TEST_KIND.replace("\n", "\\n") + "\"\n}");
byte[] content2 = testBinary ? MockData.Class2.newBuilder()
.setName(TEST_NAME)
.setNumber(TEST_NUM)
.build().toByteArray()
: utf8Encode("{\"name\": \"" + TEST_NAME + "\", \"number\": \"" + TEST_NUM + "\"}");
byte[] errorContent = testBinary ? ErrorOutput.ErrorBody.newBuilder()
.setError(ErrorOutput.ErrorProto.newBuilder()
.setCode(ERROR_CODE)
.setMessage(ERROR_MSG)
.addErrors(ErrorOutput.IndividualError.newBuilder()
.setDomain(ERROR_DOMAIN)
.setReason(ERROR_REASON)
.setMessage(ERROR_MSG))
).build().toByteArray()
: utf8Encode("{\"error\": { \"errors\": [{\"domain\": \"" + ERROR_DOMAIN + "\","
+ "\"reason\": \"" + ERROR_REASON + "\", \"message\": \"" + ERROR_MSG + "\"}],"
+ "\"code\": " + ERROR_CODE + ", \"message\": \"" + ERROR_MSG + "\"}}");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Writer responseContent = new OutputStreamWriter(outputStream, "ISO-8859-1");
if (returnSuccessAuthenticatedContent || (testExponentialBackOff && actualCalls > 1)
|| (testRedirect && actualCalls > 1)) {
if (returnSuccessAuthenticatedContent || actualCalls == callsBeforeSuccess) {
responseContent.append("--" + RESPONSE_BOUNDARY + "\n")
.append("Content-Type: application/http\n")
.append("Content-Transfer-Encoding: binary\n")
.append("Content-ID: response-1\n\n").append("HTTP/1.1 200 OK\n")
.append("Content-Type: " + contentType + "\n");
if (!testMissingLength) {
responseContent.append("Content-Length: " + content2.length + "\n");
}
responseContent.append("\n");
responseContent.flush();
outputStream.write(content2);
responseContent.append("\n--" + RESPONSE_BOUNDARY + "--\n\n");
} else {
responseContent.append("--" + RESPONSE_BOUNDARY + "\n")
.append("Content-Type: application/http\n")
.append("Content-Transfer-Encoding: binary\n")
.append("Content-ID: response-1\n\n")
.append("HTTP/1.1 " + ERROR_CODE + " Not Found\n")
.append("Content-Type: " + contentType + "\n");
if (!testMissingLength) {
responseContent.append("Content-Length: " + errorContent.length + "\n");
}
responseContent.append("\n");
responseContent.flush();
outputStream.write(errorContent);
responseContent.append("\n--" + RESPONSE_BOUNDARY + "--\n\n");
}
} else if (returnErrorAuthenticatedContent) {
responseContent.append("Content-Type: application/http\n")
.append("Content-Transfer-Encoding: binary\n").append("Content-ID: response-1\n\n");
responseContent.append("HTTP/1.1 " + ERROR_CODE + " Not Found\n")
.append("Content-Type: " + contentType + "\n");
if (!testMissingLength) {
responseContent.append("Content-Length: " + errorContent.length + "\n");
}
responseContent.append("\n");
responseContent.flush();
outputStream.write(errorContent);
responseContent.append("\n--" + RESPONSE_BOUNDARY + "--\n\n");
} else {
responseContent.append("--" + RESPONSE_BOUNDARY + "\n")
.append("Content-Type: application/http\n")
.append("Content-Transfer-Encoding: binary\n").append("Content-ID: response-1\n\n")
.append("HTTP/1.1 200 OK\n")
.append("Content-Type: " + contentType + "\n");
if (!testMissingLength) {
responseContent.append("Content-Length: " + content1.length + "\n");
}
responseContent.append("\n");
responseContent.flush();
outputStream.write(content1);
responseContent
.append("\n--" + RESPONSE_BOUNDARY + "\n")
.append("Content-Type: application/http\n")
.append("Content-Transfer-Encoding: binary\n")
.append("Content-ID: response-2\n\n");
if (testServerError) {
responseContent.append("HTTP/1.1 " + ERROR_CODE + " Not Found\n")
.append("Content-Type: " + contentType + "\n");
if (!testMissingLength) {
responseContent.append("Content-Length: " + errorContent.length + "\n");
}
responseContent.append("\n");
responseContent.flush();
outputStream.write(errorContent);
responseContent.append("\n--" + RESPONSE_BOUNDARY + "--\n\n");
} else if (testAuthenticationError) {
responseContent.append("HTTP/1.1 401 Unauthorized\n")
.append("Content-Type: application/json; charset=UTF-8\n\n")
.append("--" + RESPONSE_BOUNDARY + "--\n\n");
} else if (testRedirect && actualCalls == 1) {
responseContent.append("HTTP/1.1 301 MovedPermanently\n")
.append("Content-Type: " + contentType + "\n")
.append("Location: http://redirect/location\n\n")
.append("--" + RESPONSE_BOUNDARY + "--\n\n");
} else {
responseContent.append("HTTP/1.1 200 OK\n")
.append("Content-Type: " + contentType + "\n");
if (!testMissingLength) {
responseContent.append("Content-Length: " + content2.length + "\n");
}
responseContent.append("\n");
responseContent.flush();
outputStream.write(content2);
responseContent.append("\n--" + RESPONSE_BOUNDARY + "--\n\n");
}
}
responseContent.flush();
response.setContent(outputStream.toByteArray());
return response;
}
// Short-hand to encode a String as a UTF-8 byte array
private byte[] utf8Encode(String string) {