本文整理汇总了Java中com.google.api.client.testing.http.HttpTesting类的典型用法代码示例。如果您正苦于以下问题:Java HttpTesting类的具体用法?Java HttpTesting怎么用?Java HttpTesting使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpTesting类属于com.google.api.client.testing.http包,在下文中一共展示了HttpTesting类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildHttpResponse
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
/**
* Builds a HttpResponse with the given string response.
*
* @param header header value to provide or null if none.
* @param uploadId upload id to provide in the url upload id param or null if none.
* @param uploadType upload type to provide in url upload type param or null if none.
* @return HttpResponse with the given parameters
* @throws IOException
*/
private HttpResponse buildHttpResponse(String header, String uploadId, String uploadType)
throws IOException {
MockHttpTransport.Builder builder = new MockHttpTransport.Builder();
MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse();
builder.setLowLevelHttpResponse(resp);
resp.setStatusCode(200);
GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
if (header != null) {
resp.addHeader("X-GUploader-UploadID", header);
}
if (uploadId != null) {
url.put("upload_id", uploadId);
}
if (uploadType != null) {
url.put("uploadType", uploadType);
}
return builder.build().createRequestFactory().buildGetRequest(url).execute();
}
示例2: makeResponseException
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
/** Returns a valid GoogleJsonResponseException for the given status code and error message. */
private GoogleJsonResponseException makeResponseException(
final int statusCode,
final String message) throws Exception {
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(statusCode);
response.setContentType(Json.MEDIA_TYPE);
response.setContent(String.format(
"{\"error\":{\"code\":%d,\"message\":\"%s\",\"domain\":\"global\","
+ "\"reason\":\"duplicate\"}}",
statusCode,
message));
return response;
}};
}};
HttpRequest request = transport.createRequestFactory()
.buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL)
.setThrowExceptionOnExecuteError(false);
return GoogleJsonResponseException.from(new JacksonFactory(), request.execute());
}
示例3: testHandleResponse
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testHandleResponse() {
HttpRequest req;
try {
req = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
req.setNumberOfRetries(5);
LabelResponse labelResponse = new LabelResponse();
Meta meta = new Meta();
meta.setCode(200);
meta.setRetryable(true);
labelResponse.setMeta(meta);
Boolean retry = responseHandler.handleResponse(req, labelResponse, true);
assertTrue(retry);
assertEquals(new Long(1000), sleeper.getDelay());
//responseHandler.handleResponse(req, labelResponse, true);
} catch (IOException e) {
}
}
示例4: testExponentialDelay
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testExponentialDelay() {
HttpRequest req;
try {
req = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
req.setNumberOfRetries(5);
LabelResponse labelResponse = new LabelResponse();
Meta meta = new Meta();
meta.setCode(200);
meta.setRetryable(true);
labelResponse.setMeta(meta);
Boolean retry = null;
Long delay = new Long(1000);
for(int i = 0; i < 5; i++) {
retry = responseHandler.handleResponse(req, labelResponse, true);
assertEquals(delay, sleeper.getDelay());
delay *= 2;
}
assertTrue(retry);
//responseHandler.handleResponse(req, labelResponse, true);
} catch (IOException e) {
}
}
示例5: testRetryStopOnSixth
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testRetryStopOnSixth() {
HttpRequest req;
try {
req = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
req.setNumberOfRetries(5);
LabelResponse labelResponse = new LabelResponse();
Meta meta = new Meta();
meta.setCode(200);
meta.setRetryable(true);
labelResponse.setMeta(meta);
Boolean retry = null;
for(int i = 0; i < 6; i++) {
retry = responseHandler.handleResponse(req, labelResponse, true);
}
assertFalse(retry);
//responseHandler.handleResponse(req, labelResponse, true);
} catch (IOException e) {
}
}
示例6: testNotRetryable
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testNotRetryable() {
HttpRequest req;
try {
req = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
req.setNumberOfRetries(5);
LabelResponse labelResponse = new LabelResponse();
Meta meta = new Meta();
meta.setCode(200);
meta.setRetryable(false);
labelResponse.setMeta(meta);
Boolean retry = null;
retry = responseHandler.handleResponse(req, labelResponse, true);
assertFalse(retry);
//responseHandler.handleResponse(req, labelResponse, true);
} catch (IOException e) {
}
}
示例7: testRetryableNullInResponse
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testRetryableNullInResponse() {
HttpRequest req;
try {
req = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
req.setNumberOfRetries(5);
LabelResponse labelResponse = new LabelResponse();
Meta meta = new Meta();
meta.setCode(200);
meta.setRetryable(null);
labelResponse.setMeta(meta);
Boolean retry = null;
retry = responseHandler.handleResponse(req, labelResponse, true);
assertFalse(retry);
//responseHandler.handleResponse(req, labelResponse, true);
} catch (IOException e) {
}
}
示例8: testDoNotRetry
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testDoNotRetry() {
HttpRequest req;
try {
req = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
req.setNumberOfRetries(5);
LabelResponse labelResponse = new LabelResponse();
Meta meta = new Meta();
meta.setCode(200);
meta.setRetryable(true);
labelResponse.setMeta(meta);
Boolean retry = null;
retry = responseHandler.handleResponse(req, labelResponse, false);
assertFalse(retry);
//responseHandler.handleResponse(req, labelResponse, true);
} catch (IOException e) {
}
}
示例9: testPostIOError
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testPostIOError() throws InterruptedException, ExecutionException, TimeoutException {
Jups jups = new Jups(HttpTesting.SIMPLE_URL, "APP_ID", "SECRET");
jups.mHttpTransport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
throw new IOException("test");
}
};
}
};
String result = jups.send(new PushNotification("MASTER_VARIANT_ID", "VARIANT_ID", "ALERT_MESSAGE", "SOUND"), "TEST_UUID");
assertEquals("Push notification should have failed", result, "failed");
}
示例10: testSuccessResult
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testSuccessResult() throws InterruptedException, ExecutionException, TimeoutException {
Afm afm = Afm.get(HttpTesting.SIMPLE_URL);
JsonMockHttpTransport mockTransport = new JsonMockHttpTransport(succeeded_task_json);
afm.mHttpTransport = mockTransport;
FutureCallback result = new FutureCallback();
afm.postTask(new Task("TASK_UUID", "TEST_UUID", "wait", 1), result);
Task resultTask = result.task.get(5000, TimeUnit.MILLISECONDS);
assertNotNull("Task should have been returned", resultTask);
assertTrue("Task should have succeeded", result.succeeded);
assertNull("Result should not contain error.", resultTask.task_error);
assertEquals("Afm should have only made 2 requests.", 2, mockTransport.getRequestsCount());
checkTaskBasics(resultTask);
assertNotNull("Task should have result data", resultTask.task_result);
assertEquals("result value should match", new BigDecimal(2), resultTask.task_result.get("waited"));
assertEquals("int result value should have zero", 0, ((BigDecimal) resultTask.task_result.get("waited")).scale());
assertEquals("result value should match", new BigDecimal(2.5), resultTask.task_result.get("waited-double"));
assertNotEquals("double return value should not have zero scale", 0, ((BigDecimal) resultTask.task_result.get("waited-double")).scale());
assertEquals("result value should match", "string-value", resultTask.task_result.get("string-value"));
}
示例11: testFailedResult
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testFailedResult() throws InterruptedException, ExecutionException, TimeoutException {
Afm afm = Afm.get(HttpTesting.SIMPLE_URL);
JsonMockHttpTransport mockTransport = new JsonMockHttpTransport(failed_task_json);
afm.mHttpTransport = mockTransport;
FutureCallback result = new FutureCallback();
afm.postTask(new Task("TASK_UUID", "TEST_UUID", "wait", 1), result);
Task resultTask = result.task.get(5000, TimeUnit.MILLISECONDS);
assertFalse("Task should have failed", result.succeeded);
assertNotNull("Task should have been returned.", resultTask);
assertNotNull("Error should be set.", resultTask.task_error);
assertEquals("Error message should match definition.", resultTask.task_error, "error description");
assertEquals("Afm should have only made 2 requests.", 2, mockTransport.getRequestsCount());
checkTaskBasics(resultTask);
}
示例12: testResultTimeout
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test(expected = TimeoutException.class)
public void testResultTimeout() throws InterruptedException, ExecutionException, TimeoutException {
Afm afm = Afm.get(HttpTesting.SIMPLE_URL);
JsonMockHttpTransport mockTransport = new JsonMockHttpTransport(waiting_task_json);
afm.mHttpTransport = mockTransport;
FutureCallback result = new FutureCallback();
afm.postTask(new Task("TASK_UUID", "TEST_UUID", "wait", 1), result);
try {
result.task.get(5000, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
assertTrue("Afm should have made multiple requests to poll results.", mockTransport.getRequestsCount() > 2);
throw e;
}
}
示例13: testPostIOError
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
@Test
public void testPostIOError() throws InterruptedException, ExecutionException, TimeoutException {
Afm afm = Afm.get(HttpTesting.SIMPLE_URL);
afm.mHttpTransport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
throw new IOException("test");
}
};
}
};
FutureCallback result = new FutureCallback();
afm.postTask(new Task("TASK_UUID", "TEST_UUID", "wait", 1), result);
Task resultTask = result.task.get(5000, TimeUnit.MILLISECONDS);
assertFalse("Task should have failed", result.succeeded);
assertNotNull("Task should have been returned.", resultTask);
assertNotNull("Error should be set.", resultTask.task_error);
}
示例14: makeHttpException
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
HttpResponseException makeHttpException(int status) throws IOException {
MockHttpTransport.Builder builder = new MockHttpTransport.Builder();
MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse();
resp.setStatusCode(status);
builder.setLowLevelHttpResponse(resp);
try {
HttpResponse res =
builder.build()
.createRequestFactory()
.buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL)
.execute();
return new HttpResponseException(res);
} catch (HttpResponseException exception) {
return exception; // Throws the exception we want anyway, so just return it.
}
}
示例15: createFakeResponse
import com.google.api.client.testing.http.HttpTesting; //导入依赖的package包/类
private HttpResponse createFakeResponse(final String responseHeader, final String responseValue,
final InputStream content) throws IOException {
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
MockLowLevelHttpRequest req = new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
return new MockLowLevelHttpResponse()
.addHeader(responseHeader, responseValue)
.setContent(content);
}
};
return req;
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
return request.execute();
}