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

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

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

MockWebServer.url介绍

[英]Returns a URL for connecting to this server.
[中]返回用于连接到此服务器的URL。

代码示例

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

private HttpUrl redirectUrl() {
 return mockWebServer.url("/oauth/");
}

代码示例来源: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: prestodb/presto

private Connection createConnection()
    throws SQLException
{
  String url = format("jdbc:presto://%s", server.url("/").uri().getAuthority());
  return DriverManager.getConnection(url, "test", null);
}

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

public static void main(String... args) throws IOException {
  MockWebServer server = new MockWebServer();
  server.start();
  server.enqueue(new MockResponse().setBody("{\"name\": \"Jason\"}"));
  server.enqueue(new MockResponse().setBody("<user name=\"Eximel\"/>"));

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

  User user1 = service.exampleJson().execute().body();
  System.out.println("User 1: " + user1.name);

  User user2 = service.exampleXml().execute().body();
  System.out.println("User 2: " + user2.name);

  server.shutdown();
 }
}

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

.baseUrl(server.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.build();

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

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

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

  Repo retrofitRepo = new Repo("square", "retrofit");

  service.sendNormal(retrofitRepo).execute();
  RecordedRequest normalRequest = server.takeRequest();
  System.out.println(
    "Normal @Body Transfer-Encoding: " + normalRequest.getHeader("Transfer-Encoding"));

  service.sendChunked(retrofitRepo).execute();
  RecordedRequest chunkedRequest = server.takeRequest();
  System.out.println(
    "@Chunked @Body Transfer-Encoding: " + chunkedRequest.getHeader("Transfer-Encoding"));

  server.shutdown();
 }
}

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

public void run() throws Exception {
 String localhost = InetAddress.getByName("localhost").getCanonicalHostName();
 HeldCertificate localhostCertificate = new HeldCertificate.Builder()
   .addSubjectAlternativeName(localhost)
   .build();
 HandshakeCertificates serverCertificates = new HandshakeCertificates.Builder()
   .heldCertificate(localhostCertificate)
   .build();
 MockWebServer server = new MockWebServer();
 server.useHttps(serverCertificates.sslSocketFactory(), false);
 server.enqueue(new MockResponse());
 HandshakeCertificates clientCertificates = new HandshakeCertificates.Builder()
   .addTrustedCertificate(localhostCertificate.certificate())
   .build();
 OkHttpClient client = new OkHttpClient.Builder()
   .sslSocketFactory(clientCertificates.sslSocketFactory(), clientCertificates.trustManager())
   .build();
 Call call = client.newCall(new Request.Builder()
   .url(server.url("/"))
   .build());
 Response response = call.execute();
 System.out.println(response.handshake().tlsVersion());
}

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

private void pushPromises(Http2Stream stream, RecordedRequest request,
   List<PushPromise> promises) throws IOException {
  for (PushPromise pushPromise : promises) {
   List<Header> pushedHeaders = new ArrayList<>();
   pushedHeaders.add(new Header(Header.TARGET_AUTHORITY, url(pushPromise.path()).host()));
   pushedHeaders.add(new Header(Header.TARGET_METHOD, pushPromise.method()));
   pushedHeaders.add(new Header(Header.TARGET_PATH, pushPromise.path()));
   Headers pushPromiseHeaders = pushPromise.headers();
   for (int i = 0, size = pushPromiseHeaders.size(); i < size; i++) {
    pushedHeaders.add(new Header(pushPromiseHeaders.name(i), pushPromiseHeaders.value(i)));
   }
   String requestLine = pushPromise.method() + ' ' + pushPromise.path() + " HTTP/1.1";
   List<Integer> chunkSizes = Collections.emptyList(); // No chunked encoding for HTTP/2.
   requestQueue.add(new RecordedRequest(requestLine, pushPromise.headers(), chunkSizes, 0,
     new Buffer(), sequenceNumber.getAndIncrement(), socket));
   boolean hasBody = pushPromise.response().getBody() != null;
   Http2Stream pushedStream =
     stream.getConnection().pushStream(stream.getId(), pushedHeaders, hasBody);
   writeResponse(pushedStream, request, pushPromise.response());
  }
 }
}

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

GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
SimpleXmlConverterFactory simpleXmlConverterFactory = SimpleXmlConverterFactory.create();
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/"))
  .addConverterFactory(
    new AnnotatedConverterFactory.Builder().add(Moshi.class, moshiConverterFactory)

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

private DataFetcher<InputStream> getFetcher(Headers headers) {
 URL url = mockWebServer.url(DEFAULT_PATH).url();
 return new VolleyStreamFetcher(requestQueue, new GlideUrl(url.toString(), headers));
}

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

@Before
public void setup() {
  this.server = new MockWebServer();
  this.webClient = WebClient
      .builder()
      .clientConnector(this.connector)
      .baseUrl(this.server.url("/").toString())
      .build();
}

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

private HttpUrlFetcher getFetcher(Headers headers) {
  URL url = mockWebServer.url(DEFAULT_PATH).url();
  return new HttpUrlFetcher(new GlideUrl(url, headers), TIMEOUT_TIME_MS,
    HttpUrlFetcher.DEFAULT_CONNECTION_FACTORY);
 }
}

代码示例来源: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 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 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 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 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: spring-projects/spring-framework

@Before
public void setUp() {
  this.factory = new ReactorResourceFactory();
  this.factory.afterPropertiesSet();
  this.server = new MockWebServer();
  this.webClient = WebClient
      .builder()
      .clientConnector(initConnector())
      .baseUrl(this.server.url("/").toString())
      .build();
}

代码示例来源: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 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);
 verify(callback).onDataReady(streamCaptor.capture());
 TestUtil.assertStreamOf(expected, streamCaptor.getValue());
 assertThat(mockWebServer.takeRequest().getMethod()).isEqualTo("GET");
 assertThat(mockWebServer.takeRequest().getMethod()).isEqualTo("GET");
}

相关文章