当前位置: 首页>>代码示例>>Java>>正文


Java MockHttpTransport类代码示例

本文整理汇总了Java中com.google.api.client.testing.http.MockHttpTransport的典型用法代码示例。如果您正苦于以下问题:Java MockHttpTransport类的具体用法?Java MockHttpTransport怎么用?Java MockHttpTransport使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MockHttpTransport类属于com.google.api.client.testing.http包,在下文中一共展示了MockHttpTransport类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testSetCustomAttributes

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Test
public void testSetCustomAttributes() throws Exception {
  MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
  response.setContent(TestUtils.loadResource("createUser.json"));
  MockHttpTransport transport = new MockHttpTransport.Builder()
      .setLowLevelHttpResponse(response)
      .build();
  FirebaseUserManager userManager = new FirebaseUserManager(gson, transport, credentials);
  TestResponseInterceptor interceptor = new TestResponseInterceptor();
  userManager.setInterceptor(interceptor);
  // should not throw
  ImmutableMap<String, Object> claims = ImmutableMap.<String, Object>of(
      "admin", true, "package", "gold");
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  userManager.updateUser(new UpdateRequest("testuser")
      .setCustomClaims(claims), jsonFactory);
  checkRequestHeaders(interceptor);

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals("testuser", parsed.get("localId"));
  assertEquals(jsonFactory.toString(claims), parsed.get("customAttributes"));
}
 
开发者ID:firebase,项目名称:firebase-admin-java,代码行数:25,代码来源:FirebaseUserManagerTest.java

示例2: testGetUserMalformedJsonError

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Test
public void testGetUserMalformedJsonError() throws Exception {
  MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
  response.setContent("{\"not\" json}");
  MockHttpTransport transport = new MockHttpTransport.Builder()
      .setLowLevelHttpResponse(response)
      .build();
  FirebaseUserManager userManager = new FirebaseUserManager(gson, transport, credentials);
  try {
    userManager.getUserById("testuser");
    fail("No error thrown for JSON error");
  }  catch (FirebaseAuthException e) {
    assertTrue(e.getCause() instanceof IOException);
    assertEquals(FirebaseUserManager.INTERNAL_ERROR, e.getErrorCode());
  }
}
 
开发者ID:firebase,项目名称:firebase-admin-java,代码行数:17,代码来源:FirebaseUserManagerTest.java

示例3: testGetUserUnexpectedHttpError

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Test
public void testGetUserUnexpectedHttpError() throws Exception {
  MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
  response.setContent("{\"not\" json}");
  response.setStatusCode(500);
  MockHttpTransport transport = new MockHttpTransport.Builder()
      .setLowLevelHttpResponse(response)
      .build();
  FirebaseUserManager userManager = new FirebaseUserManager(gson, transport, credentials);
  try {
    userManager.getUserById("testuser");
    fail("No error thrown for JSON error");
  }  catch (FirebaseAuthException e) {
    assertTrue(e.getCause() instanceof HttpResponseException);
    assertEquals("Unexpected HTTP response with status: 500; body: {\"not\" json}",
        e.getMessage());
    assertEquals(FirebaseUserManager.INTERNAL_ERROR, e.getErrorCode());
  }
}
 
开发者ID:firebase,项目名称:firebase-admin-java,代码行数:20,代码来源:FirebaseUserManagerTest.java

示例4: configureMock

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
protected static HttpTransport configureMock() {
    return new MockHttpTransport() {
        @Override
        public LowLevelHttpRequest buildRequest(String method, final String url) throws IOException {
            return new MockLowLevelHttpRequest() {
                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    response.setContentType(Json.MEDIA_TYPE);
                    if (url.startsWith(GCE_METADATA_URL)) {
                        logger.info("--> Simulate GCE Auth/Metadata response for [{}]", url);
                        response.setContent(readGoogleInternalJsonResponse(url));
                    } else {
                        logger.info("--> Simulate GCE API response for [{}]", url);
                        response.setContent(readGoogleApiJsonResponse(url));
                    }

                    return response;
                }
            };
        }
    };
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:25,代码来源:GceMockUtils.java

示例5: mockClient

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
/**
 * Return a Client that is mocked to return the given HTTP response.
 *
 * @param httpResponse The full HTTP response data
 * @return The Client instance
 */
public Client mockClient(final String httpResponse) {
  Client client = new Client();

  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();
          return mockResponse(response, httpResponse);
        }
      };
    }
  };

  client.setTransport(transport);

  return client;
}
 
开发者ID:dnsimple,项目名称:dnsimple-java,代码行数:27,代码来源:DnsimpleTestBase.java

示例6: constructHttpRequest

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
private HttpRequest constructHttpRequest(final String content) throws IOException {
  HttpTransport transport = new MockHttpTransport() {
    @Override
    public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
      return new MockLowLevelHttpRequest() {
        @Override
        public LowLevelHttpResponse execute() throws IOException {
          MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
          result.setContentType("application/json");
          result.setContent(content);
          return result;
        }
      };
    }
  };
  return transport.createRequestFactory().buildGetRequest(new GenericUrl("https://google.com"))
      .setParser(new JsonObjectParser(new JacksonFactory()));
}
 
开发者ID:cloudendpoints,项目名称:endpoints-java,代码行数:19,代码来源:GoogleAuthTest.java

示例7: normal

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Test
public void normal() throws IOException {
  Collection<SinkRecord> sinkRecords = new ArrayList<>();
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp", "sourcetype", "txt", "index", "main"));

  final LowLevelHttpRequest httpRequest = mock(LowLevelHttpRequest.class, CALLS_REAL_METHODS);
  LowLevelHttpResponse httpResponse = getResponse(200);
  when(httpRequest.execute()).thenReturn(httpResponse);

  this.task.transport = new MockHttpTransport() {
    @Override
    public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
      return httpRequest;
    }
  };

  this.task.httpRequestFactory = this.task.transport.createRequestFactory(this.task.httpRequestInitializer);
  this.task.put(sinkRecords);
}
 
开发者ID:jcustenborder,项目名称:kafka-connect-splunk,代码行数:22,代码来源:SplunkHttpSinkTaskTest.java

示例8: connectionRefused

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Test
public void connectionRefused() throws IOException {
  Collection<SinkRecord> sinkRecords = new ArrayList<>();
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp", "sourcetype", "txt", "index", "main"));

  final LowLevelHttpRequest httpRequest = mock(LowLevelHttpRequest.class, CALLS_REAL_METHODS);
  when(httpRequest.execute()).thenThrow(ConnectException.class);
  this.task.transport = new MockHttpTransport() {
    @Override
    public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
      return httpRequest;
    }
  };

  this.task.httpRequestFactory = this.task.transport.createRequestFactory(this.task.httpRequestInitializer);
  assertThrows(RetriableException.class, () -> this.task.put(sinkRecords));
}
 
开发者ID:jcustenborder,项目名称:kafka-connect-splunk,代码行数:20,代码来源:SplunkHttpSinkTaskTest.java

示例9: contentLengthTooLarge

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Test
public void contentLengthTooLarge() throws IOException {
  Collection<SinkRecord> sinkRecords = new ArrayList<>();
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp", "sourcetype", "txt", "index", "main"));

  final LowLevelHttpRequest httpRequest = mock(LowLevelHttpRequest.class, CALLS_REAL_METHODS);
  LowLevelHttpResponse httpResponse = getResponse(417);
  when(httpResponse.getContentType()).thenReturn("text/html");
  when(httpRequest.execute()).thenReturn(httpResponse);

  this.task.transport = new MockHttpTransport() {
    @Override
    public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
      return httpRequest;
    }
  };

  this.task.httpRequestFactory = this.task.transport.createRequestFactory(this.task.httpRequestInitializer);
  assertThrows(org.apache.kafka.connect.errors.ConnectException.class, () -> this.task.put(sinkRecords));
}
 
开发者ID:jcustenborder,项目名称:kafka-connect-splunk,代码行数:23,代码来源:SplunkHttpSinkTaskTest.java

示例10: invalidToken

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Test
public void invalidToken() throws IOException {
  Collection<SinkRecord> sinkRecords = new ArrayList<>();
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp", "sourcetype", "txt", "index", "main"));

  final LowLevelHttpRequest httpRequest = mock(LowLevelHttpRequest.class, CALLS_REAL_METHODS);
  LowLevelHttpResponse httpResponse = getResponse(403);
  when(httpRequest.execute()).thenReturn(httpResponse);

  this.task.transport = new MockHttpTransport() {
    @Override
    public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
      return httpRequest;
    }
  };

  this.task.httpRequestFactory = this.task.transport.createRequestFactory(this.task.httpRequestInitializer);
  assertThrows(org.apache.kafka.connect.errors.ConnectException.class, () -> this.task.put(sinkRecords));
}
 
开发者ID:jcustenborder,项目名称:kafka-connect-splunk,代码行数:22,代码来源:SplunkHttpSinkTaskTest.java

示例11: invalidIndex

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Test
public void invalidIndex() throws IOException {
  Collection<SinkRecord> sinkRecords = new ArrayList<>();
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp"));
  SinkRecordContentTest.addRecord(sinkRecords, ImmutableMap.of("host", "hostname.example.com", "time", new Date(1472256858924L), "source", "testapp", "sourcetype", "txt", "index", "main"));

  final LowLevelHttpRequest httpRequest = mock(LowLevelHttpRequest.class, CALLS_REAL_METHODS);
  LowLevelHttpResponse httpResponse = getResponse(400);
  when(httpRequest.execute()).thenReturn(httpResponse);

  this.task.transport = new MockHttpTransport() {
    @Override
    public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
      return httpRequest;
    }
  };

  this.task.httpRequestFactory = this.task.transport.createRequestFactory(this.task.httpRequestInitializer);
  assertThrows(org.apache.kafka.connect.errors.ConnectException.class, () -> this.task.put(sinkRecords));
}
 
开发者ID:jcustenborder,项目名称:kafka-connect-splunk,代码行数:22,代码来源:SplunkHttpSinkTaskTest.java

示例12: buildHttpResponse

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的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();
}
 
开发者ID:apache,项目名称:beam,代码行数:28,代码来源:UploadIdResponseInterceptorTest.java

示例13: testFileSizeWhenFileNotFoundNonBatch

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Test
public void testFileSizeWhenFileNotFoundNonBatch() throws Exception {
  MockLowLevelHttpResponse notFoundResponse = new MockLowLevelHttpResponse();
  notFoundResponse.setContent("");
  notFoundResponse.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);

  MockHttpTransport mockTransport =
          new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build();

  GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
  GcsUtil gcsUtil = pipelineOptions.getGcsUtil();

  gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), null));

  thrown.expect(FileNotFoundException.class);
  gcsUtil.fileSize(GcsPath.fromComponents("testbucket", "testobject"));
}
 
开发者ID:apache,项目名称:beam,代码行数:18,代码来源:GcsUtilTest.java

示例14: setUp

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);

  // A mock transport that lets us mock the API responses.
  MockHttpTransport transport =
      new MockHttpTransport.Builder()
          .setLowLevelHttpRequest(
              new MockLowLevelHttpRequest() {
                @Override
                public LowLevelHttpResponse execute() throws IOException {
                  return response;
                }
              })
          .build();

  // A sample BigQuery API client that uses default JsonFactory and RetryHttpInitializer.
  bigquery =
      new Bigquery.Builder(
              transport, Transport.getJsonFactory(), new RetryHttpRequestInitializer())
          .build();
}
 
开发者ID:apache,项目名称:beam,代码行数:23,代码来源:BigQueryServicesImplTest.java

示例15: createMockTransport

import com.google.api.client.testing.http.MockHttpTransport; //导入依赖的package包/类
private MockHttpTransport createMockTransport (final ByteSource iirdeaResponse) {
  return new MockHttpTransport() {
    @Override
    public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
      mockRequest = new MockLowLevelHttpRequest() {
        @Override
        public LowLevelHttpResponse execute() throws IOException {
          MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
          response.setStatusCode(200);
          response.setContentType(PLAIN_TEXT_UTF_8.toString());
          response.setContent(iirdeaResponse.read());
          return response;
        }
      };
      mockRequest.setUrl(url);
      return mockRequest;
    }
  };
}
 
开发者ID:google,项目名称:nomulus,代码行数:20,代码来源:IcannHttpReporterTest.java


注:本文中的com.google.api.client.testing.http.MockHttpTransport类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。