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

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

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

MockWebServer.<init>介绍

暂无

代码示例

代码示例来源: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 {
 MockWebServer server = new MockWebServer();
 server.start();
 server.enqueue(new MockResponse()

代码示例来源: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: 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\"}"));

代码示例来源: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() {
  this.server = new MockWebServer();
  this.webClient = WebClient
      .builder()
      .clientConnector(this.connector)
      .baseUrl(this.server.url("/").toString())
      .build();
}

代码示例来源:origin: SonarSource/sonarqube

@Before
public void setUp() throws Exception {
 server = new MockWebServer();
 server.start();
 serverUrl = server.url("").url().toString();
}

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

相关文章