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

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

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

MockWebServer.start介绍

[英]Equivalent to start(0).
[中]相当于开始(0)。

代码示例

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

/** Equivalent to {@code start(0)}. */
public void start() throws IOException {
 start(0);
}

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

/**
 * Starts the server on the given address and port.
 *
 * @param inetAddress the address to create the server socket on
 * @param port the port to listen to, or 0 for any available port. Automated tests should always
 * use port 0 to avoid flakiness when a specific port is unavailable.
 */
public void start(InetAddress inetAddress, int port) throws IOException {
 start(new InetSocketAddress(inetAddress, port));
}

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

/**
 * Starts the server on the loopback interface for the given port.
 *
 * @param port the port to listen to, or 0 for any available port. Automated tests should always
 * use port 0 to avoid flakiness when a specific port is unavailable.
 */
public void start(int port) throws IOException {
 start(InetAddress.getByName("localhost"), port);
}

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

@Override protected synchronized void before() {
 if (started) return;
 try {
  start();
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

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

public void start() throws Exception {
 if (mockWebServer != null) throw new IllegalStateException();
 mockWebServer = new MockWebServer();
 mockWebServer.setDispatcher(this);
 mockWebServer.start(slackApi.port);
}

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

public void run() throws IOException {
 MockWebServer server = new MockWebServer();
 server.useHttps(sslContext.getSocketFactory(), false);
 server.setDispatcher(this);
 server.start(port);
}

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

public void run() throws Exception {
 File socketFile = new File("/tmp/ClientAndServer.sock");
 socketFile.delete(); // Clean up from previous runs.
 MockWebServer server = new MockWebServer();
 server.setServerSocketFactory(new UnixDomainServerSocketFactory(socketFile));
 server.setProtocols(Collections.singletonList(Protocol.H2_PRIOR_KNOWLEDGE));
 server.enqueue(new MockResponse().setBody("hello"));
 server.start();
 OkHttpClient client = new OkHttpClient.Builder()
   .socketFactory(new UnixDomainSocketFactory(socketFile))
   .protocols(Collections.singletonList(Protocol.H2_PRIOR_KNOWLEDGE))
   .build();
 Request request = new Request.Builder()
   .url("http://publicobject.com/helloworld.txt")
   .build();
 try (Response response = client.newCall(request).execute()) {
  System.out.println(response.body().string());
 }
 server.shutdown();
 socketFile.delete();
}

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

@BeforeMethod
public void setup()
    throws IOException
{
  server = new MockWebServer();
  server.start();
}

代码示例来源:origin: prestodb/presto

@BeforeMethod
public void setup()
    throws IOException
{
  server = new MockWebServer();
  server.start();
}

代码示例来源:origin: prestodb/presto

@BeforeMethod
public void setup()
    throws IOException
{
  server = new MockWebServer();
  server.start();
}

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

public static void main(String... args) throws IOException {
 server.start();
 server.enqueue(new MockResponse()
   .setResponseCode(404)

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

@Before
public void setUp() throws IOException {
 MockitoAnnotations.initMocks(this);
 waitForResponseLatch = new CountDownLatch(1);
 doAnswer(new CountDown()).when(callback).onDataReady(any(InputStream.class));
 doAnswer(new CountDown()).when(callback).onLoadFailed(any(Exception.class));
 requestQueue = Volley.newRequestQueue(RuntimeEnvironment.application);
 mockWebServer = new MockWebServer();
 mockWebServer.start();
 streamCaptor = ArgumentCaptor.forClass(InputStream.class);
}

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

public static void main(String... args) throws IOException {
 MockWebServer server = new MockWebServer();
 server.start();
 server.enqueue(new MockResponse().setBody("{\"name\": \"Moshi\"}"));
 server.enqueue(new MockResponse().setBody("{\"name\": \"Gson\"}"));

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

@Before
public void setUp() throws IOException {
 MockitoAnnotations.initMocks(this);
 defaultFollowRedirects = HttpURLConnection.getFollowRedirects();
 HttpURLConnection.setFollowRedirects(false);
 mockWebServer = new MockWebServer();
 mockWebServer.start();
 streamCaptor = ArgumentCaptor.forClass(InputStream.class);
}

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

@Before
public void setUp() throws Exception {
  this.server = new MockWebServer();
  this.server.setDispatcher(new TestDispatcher());
  this.server.start();
  this.port = this.server.getPort();
  this.baseUrl = "http://localhost:" + this.port;
}

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

@Before
public void setUp() throws Exception {
  this.server = new MockWebServer();
  this.server.setDispatcher(new TestDispatcher());
  this.server.start();
  this.port = this.server.getPort();
  this.baseUrl = "http://localhost:" + this.port;
}

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

@Test
public void testSetsReadTimeout() throws Exception {
 MockWebServer tempWebServer = new MockWebServer();
 tempWebServer.enqueue(
   new MockResponse().setBody("test").throttleBody(1, TIMEOUT_TIME_MS, TimeUnit.MILLISECONDS));
 tempWebServer.start();
 try {
  getFetcher().loadData(Priority.HIGH, callback);
 } finally {
  tempWebServer.shutdown();
  // shutdown() called before any enqueue() blocks until it times out.
  mockWebServer.enqueue(new MockResponse().setResponseCode(200));
 }
 verify(callback).onLoadFailed(isA(IOException.class));
}

相关文章