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


Java MockClientHttpResponse类代码示例

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


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

示例1: interceptNeverRetry

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void interceptNeverRetry() throws Throwable {
    HttpRequest request = mock(HttpRequest.class);
    when(request.getURI()).thenReturn(new URI("http://foo"));
    ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[]{}, HttpStatus.OK);
    LoadBalancedRetryPolicyFactory lbRetryPolicyFactory = mock(LoadBalancedRetryPolicyFactory.class);
    when(lbRetryPolicyFactory.create(eq("foo"), any(ServiceInstanceChooser.class))).thenReturn(null);
    ServiceInstance serviceInstance = mock(ServiceInstance.class);
    when(client.choose(eq("foo"))).thenReturn(serviceInstance);
    when(client.execute(eq("foo"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenReturn(clientHttpResponse);
    when(this.lbRequestFactory.createRequest(any(), any(), any())).thenReturn(mock(LoadBalancerRequest.class));
    lbProperties.setEnabled(true);
    RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory,
            lbRequestFactory, backOffPolicyFactory, retryListenerFactory);
    byte[] body = new byte[]{};
    ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
    interceptor.intercept(request, body, execution);
    verify(lbRequestFactory).createRequest(request, body, execution);
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-commons,代码行数:20,代码来源:RetryLoadBalancerInterceptorTest.java

示例2: run

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Override
public void run() {
    MockClientHttpRequest request = new MockClientHttpRequest();
    MockClientHttpResponse response = new MockClientHttpResponse(new byte[0], HttpStatus.OK);
    ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);

    response.getHeaders().add("X-RateLimit-Remaining", "50");
    response.getHeaders().add("X-RateLimit-Reset", String.valueOf((System.currentTimeMillis() / 1000) + 1));

    try {
        when(execution.execute(request, new byte[0])).thenReturn(response);

        this.interceptor.intercept(request, new byte[0], execution);
    } catch (IOException e) {
    } finally {
        this.latch.countDown();
    }
}
 
开发者ID:pivotalsoftware,项目名称:github-cla-integration,代码行数:19,代码来源:RateLimitingClientHttpRequestInterceptorTest.java

示例3: interceptSuccess

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
  public void interceptSuccess() throws Throwable {
      HttpRequest request = mock(HttpRequest.class);
      when(request.getURI()).thenReturn(new URI("http://foo"));
      ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[]{}, HttpStatus.OK);
      LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
      LoadBalancedRetryPolicyFactory lbRetryPolicyFactory = mock(LoadBalancedRetryPolicyFactory.class);
      when(lbRetryPolicyFactory.create(eq("foo"), any(ServiceInstanceChooser.class))).thenReturn(policy);
      ServiceInstance serviceInstance = mock(ServiceInstance.class);
      when(client.choose(eq("foo"))).thenReturn(serviceInstance);
      when(client.execute(eq("foo"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenReturn(clientHttpResponse);
when(this.lbRequestFactory.createRequest(any(), any(), any())).thenReturn(mock(LoadBalancerRequest.class));
      lbProperties.setEnabled(true);
      RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory,
              lbRequestFactory, backOffPolicyFactory, retryListenerFactory);
      byte[] body = new byte[]{};
      ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
      ClientHttpResponse rsp = interceptor.intercept(request, body, execution);
      assertThat(rsp, is(clientHttpResponse));
      verify(lbRequestFactory).createRequest(request, body, execution);
  }
 
开发者ID:spring-cloud,项目名称:spring-cloud-commons,代码行数:22,代码来源:RetryLoadBalancerInterceptorTest.java

示例4: interceptFailedRetry

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test(expected = IOException.class)
  public void interceptFailedRetry() throws Exception {
      HttpRequest request = mock(HttpRequest.class);
      when(request.getURI()).thenReturn(new URI("http://foo"));
      ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[]{}, HttpStatus.OK);
      LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
      when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class))).thenReturn(false);
      LoadBalancedRetryPolicyFactory lbRetryPolicyFactory = mock(LoadBalancedRetryPolicyFactory.class);
      when(lbRetryPolicyFactory.create(eq("foo"), any(ServiceInstanceChooser.class))).thenReturn(policy);
      ServiceInstance serviceInstance = mock(ServiceInstance.class);
      when(client.choose(eq("foo"))).thenReturn(serviceInstance);
      when(client.execute(eq("foo"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenThrow(new IOException()).thenReturn(clientHttpResponse);
when(this.lbRequestFactory.createRequest(any(), any(), any())).thenReturn(mock(LoadBalancerRequest.class));
      lbProperties.setEnabled(true);
      RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory,
              lbRequestFactory, backOffPolicyFactory, retryListenerFactory);
      byte[] body = new byte[]{};
      ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
      interceptor.intercept(request, body, execution);
      verify(lbRequestFactory).createRequest(request, body, execution);
  }
 
开发者ID:spring-cloud,项目名称:spring-cloud-commons,代码行数:22,代码来源:RetryLoadBalancerInterceptorTest.java

示例5: retryListenerTestNoRetry

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test(expected = TerminatedRetryException.class)
public void retryListenerTestNoRetry() throws Throwable {
    HttpRequest request = mock(HttpRequest.class);
    when(request.getURI()).thenReturn(new URI("http://noRetry"));
    ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[]{}, HttpStatus.OK);
    LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
    LoadBalancedRetryPolicyFactory lbRetryPolicyFactory = mock(LoadBalancedRetryPolicyFactory.class);
    when(lbRetryPolicyFactory.create(eq("noRetry"), any(ServiceInstanceChooser.class))).thenReturn(policy);
    LoadBalancedBackOffPolicyFactory backOffPolicyFactory = mock(LoadBalancedBackOffPolicyFactory.class);
    MyBackOffPolicy backOffPolicy = new MyBackOffPolicy();
    when(backOffPolicyFactory.createBackOffPolicy(eq("noRetry"))).thenReturn(backOffPolicy);
    ServiceInstance serviceInstance = mock(ServiceInstance.class);
    lbProperties.setEnabled(true);
    MyRetryListenersNotRetry retryListeners = new MyRetryListenersNotRetry();
    RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, lbRequestFactory,
        backOffPolicyFactory, retryListeners);
    byte[] body = new byte[]{};
    ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
    interceptor.intercept(request, body, execution);
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-commons,代码行数:21,代码来源:RetryLoadBalancerInterceptorTest.java

示例6: block

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void block() throws InterruptedException, IOException {
    CountDownLatch latch = new CountDownLatch(1);

    MockClientHttpRequest request = new MockClientHttpRequest();
    MockClientHttpResponse response = new MockClientHttpResponse(new byte[0], HttpStatus.OK);
    ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);

    request.setMethod(HttpMethod.GET);
    request.setURI(URI.create("http://localhost"));

    when(execution.execute(request, new byte[0])).thenReturn(response);

    new Thread(new Trigger(this.interceptor, latch)).start();
    latch.await();

    this.interceptor.intercept(request, new byte[0], execution);
}
 
开发者ID:pivotalsoftware,项目名称:github-cla-integration,代码行数:19,代码来源:RateLimitingClientHttpRequestInterceptorTest.java

示例7: customize

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Override
public void customize(RestTemplate template) {
	template.getInterceptors().add((request, body, execution) -> {
		String payload = "{\"value\":\"FOO\"}";
		MockClientHttpResponse response = new MockClientHttpResponse(
				payload.getBytes(), HttpStatus.OK);
		response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
		return response;
	});
}
 
开发者ID:spring-projects,项目名称:spring-security-oauth2-boot,代码行数:11,代码来源:ResourceServerTokenServicesConfigurationTests.java

示例8: canBufferResponses

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void canBufferResponses() throws IOException
{
	MockClientHttpRequest request = new MockClientHttpRequest();
	MockClientHttpResponse response = new MockClientHttpResponse(singleUseStream("hello".getBytes()), OK);
	request.setResponse(response);
	when(requestFactory.createRequest(URI.create("http://example.com"), GET)).thenReturn(request);
	
	loggingCustomizer.customize(restTemplate);
	
	ClientHttpResponse actualResponse = restTemplate.getRequestFactory()
		.createRequest(URI.create("http://example.com"), GET)
		.execute();
	assertThat(copyToByteArray(actualResponse.getBody()), equalTo("hello".getBytes()));
}
 
开发者ID:markhobson,项目名称:spring-rest-template-logger,代码行数:16,代码来源:LoggingCustomizerTest.java

示例9: canLogRequestUsingDefaultCharset

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void canLogRequestUsingDefaultCharset() throws IOException, URISyntaxException
{
	MockClientHttpRequest request = new MockClientHttpRequest(POST, new URI("/hello"));
	byte[] body = "world £".getBytes(ISO_8859_1);
	when(execution.execute(request, body)).thenReturn(new MockClientHttpResponse(new byte[0], OK));
	
	loggingInterceptor.intercept(request, body, execution);
	
	verify(log).debug("Request: POST /hello world £");
}
 
开发者ID:markhobson,项目名称:spring-rest-template-logger,代码行数:12,代码来源:LoggingInterceptorTest.java

示例10: canLogRequestUsingCharset

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void canLogRequestUsingCharset() throws IOException, URISyntaxException
{
	MockClientHttpRequest request = new MockClientHttpRequest(POST, new URI("/hello"));
	request.getHeaders().setContentType(parseMediaType("text/plain;charset=UTF-8"));
	byte[] body = "world £".getBytes(UTF_8);
	when(execution.execute(request, body)).thenReturn(new MockClientHttpResponse(new byte[0], OK));
	
	loggingInterceptor.intercept(request, body, execution);
	
	verify(log).debug("Request: POST /hello world £");
}
 
开发者ID:markhobson,项目名称:spring-rest-template-logger,代码行数:13,代码来源:LoggingInterceptorTest.java

示例11: canLogResponseUsingDefaultCharset

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void canLogResponseUsingDefaultCharset() throws IOException, URISyntaxException
{
	MockClientHttpRequest request = new MockClientHttpRequest();
	byte[] body = "hello £".getBytes(ISO_8859_1);
	when(execution.execute(request, new byte[0])).thenReturn(new MockClientHttpResponse(body, OK));
	
	loggingInterceptor.intercept(request, new byte[0], execution);
	
	verify(log).debug("Response: 200 hello £");
}
 
开发者ID:markhobson,项目名称:spring-rest-template-logger,代码行数:12,代码来源:LoggingInterceptorTest.java

示例12: canLogResponseUsingCharset

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void canLogResponseUsingCharset() throws IOException, URISyntaxException
{
	MockClientHttpRequest request = new MockClientHttpRequest();
	byte[] body = "hello £".getBytes(UTF_8);
	MockClientHttpResponse response = new MockClientHttpResponse(body, OK);
	response.getHeaders().setContentType(parseMediaType("text/plain;charset=UTF-8"));
	when(execution.execute(request, new byte[0])).thenReturn(response);
	
	loggingInterceptor.intercept(request, new byte[0], execution);
	
	verify(log).debug("Response: 200 hello £");
}
 
开发者ID:markhobson,项目名称:spring-rest-template-logger,代码行数:14,代码来源:LoggingInterceptorTest.java

示例13: testInject

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void testInject() {
    String url = "http://localhost:4000/foo";
    mockServer.expect(MockRestRequestMatchers.requestTo(url))
            .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
            .andRespond(new ResponseCreator() {
                @Override
                public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
                    MockClientHttpResponse response =
                            new MockClientHttpResponse(new byte[1], HttpStatus.OK);

                    response.getHeaders().add("traceId", request.getHeaders()
                            .getFirst("traceId"));
                    response.getHeaders().add("spanId", request.getHeaders()
                            .getFirst("spanId"));
                    return response;
                }
            });

    ResponseEntity<String> responseEntity = client.getForEntity(url, String.class);

    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    Assert.assertEquals(1, mockSpans.size());
    Assert.assertEquals(mockSpans.get(0).context().traceId(),
            Long.parseLong(responseEntity.getHeaders().getFirst("traceId")));
    Assert.assertEquals(mockSpans.get(0).context().spanId(),
            Long.parseLong(responseEntity.getHeaders().getFirst("spanId")));
}
 
开发者ID:opentracing-contrib,项目名称:java-spring-web,代码行数:29,代码来源:AbstractTracingClientTest.java

示例14: success

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void success() throws Exception {
	MockClientHttpResponse response = (MockClientHttpResponse) MockRestResponseCreators.withSuccess().createResponse(null);

	assertEquals(HttpStatus.OK, response.getStatusCode());
	assertTrue(response.getHeaders().isEmpty());
	assertNull(response.getBody());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:9,代码来源:ResponseCreatorsTests.java

示例15: successWithContent

import org.springframework.mock.http.client.MockClientHttpResponse; //导入依赖的package包/类
@Test
public void successWithContent() throws Exception {
	DefaultResponseCreator responseCreator = MockRestResponseCreators.withSuccess("foo", MediaType.TEXT_PLAIN);
	MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

	assertEquals(HttpStatus.OK, response.getStatusCode());
	assertEquals(MediaType.TEXT_PLAIN, response.getHeaders().getContentType());
	assertArrayEquals("foo".getBytes(), FileCopyUtils.copyToByteArray(response.getBody()));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:10,代码来源:ResponseCreatorsTests.java


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