okhttp3.mockwebserver.MockWebServer.enqueue()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(238)

本文整理了Java中okhttp3.mockwebserver.MockWebServer.enqueue()方法的一些代码示例,展示了MockWebServer.enqueue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MockWebServer.enqueue()方法的具体详情如下:
包路径:okhttp3.mockwebserver.MockWebServer
类名称:MockWebServer
方法名:enqueue

MockWebServer.enqueue介绍

[英]Scripts response to be returned to a request made in sequence. The first request is served by the first enqueued response; the second request by the second enqueued response; and so on.
[中]脚本响应将返回到按顺序发出的请求。第一个请求由第一个排队响应服务;由第二排队响应发出的第二请求;等等

代码示例

代码示例来源:origin: spring-projects/spring-framework

private void prepareResponse(Consumer<MockResponse> consumer) {
  MockResponse response = new MockResponse();
  consumer.accept(response);
  this.server.enqueue(response);
}

代码示例来源:origin: square/retrofit

public static void main(String... args) throws IOException, InterruptedException {
  MockWebServer server = new MockWebServer();
  server.start();
  server.enqueue(new MockResponse());

  Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(server.url("/"))
    .addConverterFactory(new JsonStringConverterFactory(GsonConverterFactory.create()))
    .build();
  Service service = retrofit.create(Service.class);

  Call<ResponseBody> call = service.example(new Filter("123"));
  Response<ResponseBody> response = call.execute();
  // TODO handle user response...

  // Print the request path that the server saw to show the JSON query param:
  RecordedRequest recordedRequest = server.takeRequest();
  System.out.println(recordedRequest.getPath());

  server.shutdown();
 }
}

代码示例来源:origin: bumptech/glide

@Test
public void testHandlesRedirect302s() throws Exception {
 String expected = "fakedata";
 mockWebServer.enqueue(new MockResponse().setResponseCode(302)
   .setHeader("Location", mockWebServer.url("/redirect").toString()));
 mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(expected));
 getFetcher().loadData(Priority.LOW, callback);
 waitForResponseLatch.await();
 verify(callback).onDataReady(streamCaptor.capture());
 assertStreamOf(expected, streamCaptor.getValue());
}

代码示例来源:origin: bumptech/glide

@Test
public void testHandlesRedirect301s() throws Exception {
 String expected = "fakedata";
 mockWebServer.enqueue(new MockResponse().setResponseCode(301)
   .setHeader("Location", mockWebServer.url("/redirect").toString()));
 mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(expected));
 getFetcher().loadData(Priority.LOW, callback);
 waitForResponseLatch.await();
 verify(callback).onDataReady(streamCaptor.capture());
 assertStreamOf(expected, streamCaptor.getValue());
}

代码示例来源:origin: bumptech/glide

@Test
public void testCallsLoadFailedIfStatusCodeIsNegativeOne() throws Exception {
 mockWebServer.enqueue(new MockResponse().setResponseCode(-1));
 getFetcher().loadData(Priority.LOW, callback);
 waitForResponseLatch.await();
 verify(callback).onLoadFailed(isA(VolleyError.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testCallsLoadFailedIfRedirectLocationIsEmpty() throws Exception {
 for (int i = 0; i < 2; i++) {
  mockWebServer.enqueue(new MockResponse().setResponseCode(301));
 }
 getFetcher().loadData(Priority.NORMAL, callback);
 waitForResponseLatch.await();
 verify(callback).onLoadFailed(isA(VolleyError.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testFailsOnRedirectLoops() throws Exception {
 mockWebServer.enqueue(new MockResponse().setResponseCode(301)
   .setHeader("Location", mockWebServer.url("/redirect").toString()));
 mockWebServer.enqueue(new MockResponse().setResponseCode(301)
   .setHeader("Location", mockWebServer.url("/redirect").toString()));
 getFetcher().loadData(Priority.IMMEDIATE, callback);
 verify(callback).onLoadFailed(isA(IOException.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testHandlesRedirect301s() throws Exception {
 String expected = "fakedata";
 mockWebServer.enqueue(new MockResponse().setResponseCode(301)
   .setHeader("Location", mockWebServer.url("/redirect").toString()));
 mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(expected));
 getFetcher().loadData(Priority.LOW, callback);
 verify(callback).onDataReady(streamCaptor.capture());
 TestUtil.assertStreamOf(expected, streamCaptor.getValue());
 assertThat(mockWebServer.takeRequest().getMethod()).isEqualTo("GET");
 assertThat(mockWebServer.takeRequest().getMethod()).isEqualTo("GET");
}

代码示例来源:origin: bumptech/glide

@Test
public void testCallsLoadFailedAfterTooManyRedirects() throws Exception {
 for (int i = 0; i < 20; i++) {
  mockWebServer.enqueue(new MockResponse().setResponseCode(301)
    .setHeader("Location", mockWebServer.url("/redirect" + i).toString()));
 }
 getFetcher().loadData(Priority.NORMAL, callback);
 waitForResponseLatch.await();
 verify(callback).onLoadFailed(isA(VolleyError.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testReturnsInputStreamOnStatusOk() throws Exception {
 String expected = "fakedata";
 mockWebServer.enqueue(new MockResponse().setBody(expected).setResponseCode(200));
 HttpUrlFetcher fetcher = getFetcher();
 fetcher.loadData(Priority.HIGH, callback);
 verify(callback).onDataReady(streamCaptor.capture());
 TestUtil.assertStreamOf(expected, streamCaptor.getValue());
 assertThat(mockWebServer.takeRequest().getMethod()).isEqualTo("GET");
}

代码示例来源:origin: bumptech/glide

@Test
public void testCallsLoadFailedIfStatusCodeIs400() throws Exception {
 mockWebServer.enqueue(new MockResponse().setResponseCode(400).setBody("error"));
 getFetcher().loadData(Priority.LOW, callback);
 waitForResponseLatch.await();
 verify(callback).onLoadFailed(isA(VolleyError.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testCallsLoadFailedIfStatusCodeIs500() throws Exception {
 mockWebServer.enqueue(new MockResponse().setResponseCode(500).setBody("error"));
 getFetcher().loadData(Priority.NORMAL, callback);
 waitForResponseLatch.await();
 verify(callback).onLoadFailed(isA(VolleyError.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testFailsIfStatusCodeIs500() throws Exception {
 mockWebServer.enqueue(new MockResponse().setResponseCode(500));
 getFetcher().loadData(Priority.NORMAL, callback);
 verify(callback).onLoadFailed(isA(IOException.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testFailsIfStatusCodeIs400() throws Exception {
 mockWebServer.enqueue(new MockResponse().setResponseCode(400));
 getFetcher().loadData(Priority.LOW, callback);
 verify(callback).onLoadFailed(isA(IOException.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testFailsIfStatusCodeIsNegativeOne() throws Exception {
 mockWebServer.enqueue(new MockResponse().setResponseCode(-1));
 getFetcher().loadData(Priority.LOW, callback);
 verify(callback).onLoadFailed(isA(IOException.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testFailsAfterTooManyRedirects() throws Exception {
 for (int i = 0; i < 10; i++) {
  mockWebServer.enqueue(new MockResponse().setResponseCode(301)
    .setHeader("Location", mockWebServer.url("/redirect" + i).toString()));
 }
 getFetcher().loadData(Priority.NORMAL, callback);
 verify(callback).onLoadFailed(isA(IOException.class));
}

代码示例来源:origin: bumptech/glide

@Test
public void testFailsIfRedirectLocationIsPresentAndEmpty() throws Exception {
 mockWebServer.enqueue(new MockResponse().setResponseCode(301).setHeader("Location", ""));
 getFetcher().loadData(Priority.NORMAL, callback);
 verify(callback).onLoadFailed(isA(IOException.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test // SPR-17482
public void bodyToMonoVoidWithoutContentType() {
  this.server.enqueue(new MockResponse()
      .setResponseCode(HttpStatus.ACCEPTED.value())
      .setChunkedBody("{\"foo\" : \"123\",  \"baz\" : \"456\", \"baz\" : \"456\"}", 5));
  Mono<Map<String, String>> mono = this.webClient.get()
      .uri("/sample").accept(MediaType.APPLICATION_JSON)
      .retrieve()
      .bodyToMono(new ParameterizedTypeReference<Map<String, String>>() {});
  StepVerifier.create(mono).expectError(UnsupportedMediaTypeException.class).verify(Duration.ofSeconds(3));
  assertEquals(1, this.server.getRequestCount());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void bodyToMonoVoid() {
  this.server.enqueue(new MockResponse()
      .setResponseCode(201)
      .setHeader("Content-Type", "application/json")
      .setChunkedBody("{\"foo\" : {\"bar\" : \"123\", \"baz\" : \"456\"}}", 5));
  Mono<Void> mono = this.webClient.get()
      .uri("/json").accept(MediaType.APPLICATION_JSON)
      .retrieve()
      .bodyToMono(Void.class);
  StepVerifier.create(mono).expectComplete().verify(Duration.ofSeconds(3));
  assertEquals(1, this.server.getRequestCount());
}

代码示例来源:origin: spring-projects/spring-framework

private void testOnStatus(Throwable expected,
    Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
  HttpStatus errorStatus = HttpStatus.BAD_GATEWAY;
  this.server.enqueue(new MockResponse()
      .setResponseCode(errorStatus.value())
      .setHeader("Content-Type", "application/json")
      .setChunkedBody("{\"error\" : {\"status\" : 502, \"message\" : \"Bad gateway.\"}}", 5));
  Mono<String> mono = this.webClient.get()
      .uri("/json").accept(MediaType.APPLICATION_JSON)
      .retrieve()
      .onStatus(status -> status.equals(errorStatus), exceptionFunction)
      .bodyToMono(String.class);
  StepVerifier.create(mono).expectErrorSatisfies(actual -> assertSame(expected, actual)).verify(DELAY);
  assertEquals(1, this.server.getRequestCount());
}

相关文章