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

x33g5p2x  于2022-01-25 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(125)

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

MockWebServer.start介绍

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

代码示例

代码示例来源:origin: facebook/stetho

@Test
public void testWithResponseCompression() throws IOException {
 ByteArrayOutputStream capturedOutput = hookAlmostRealInterpretResponseStream(mMockEventReporter);
 byte[] uncompressedData = repeat(".", 1024).getBytes();
 byte[] compressedData = compress(uncompressedData);
 MockWebServer server = new MockWebServer();
 server.start();
 server.enqueue(new MockResponse()
   .setBody(new Buffer().write(compressedData))
   .addHeader("Content-Encoding: gzip"));
 Request request = new Request.Builder()
   .url(server.url("/"))
   .build();
 Response response = mClientWithInterceptor.newCall(request).execute();
 // Verify that the final output and the caller both saw the uncompressed stream.
 assertArrayEquals(uncompressedData, response.body().bytes());
 assertArrayEquals(uncompressedData, capturedOutput.toByteArray());
 // And verify that the StethoInterceptor was able to see both.
 Mockito.verify(mMockEventReporter)
   .dataReceived(
     anyString(),
     eq(compressedData.length),
     eq(uncompressedData.length));
 server.shutdown();
}

代码示例来源:origin: google/data-transfer-project

@Test
public void testExport() throws Exception {
 server.enqueue(new MockResponse().setBody(CALENDARS_RESPONSE));
 server.enqueue(new MockResponse().setBody(CALENDAR1_EVENTS_RESPONSE));
 server.enqueue(new MockResponse().setBody(CALENDAR2_EVENTS_RESPONSE));
 server.start();
 HttpUrl baseUrl = server.url("");
 MicrosoftCalendarExporter exporter =
   new MicrosoftCalendarExporter(baseUrl.toString(), client, mapper, transformerService);
 ExportResult<CalendarContainerResource> resource = exporter
   .export(UUID.randomUUID(), token, Optional.empty());
 CalendarContainerResource calendarResource = resource.getExportedData();
 Assert.assertEquals(2, calendarResource.getCalendars().size());
 Assert.assertFalse(
   calendarResource
     .getCalendars()
     .stream()
     .anyMatch(c -> "Calendar1".equals(c.getId()) && "Calendar2".equals(c.getId())));
 Assert.assertEquals(2, calendarResource.getEvents().size());
 Assert.assertFalse(
   calendarResource
     .getEvents()
     .stream()
     .anyMatch(
       e ->
         "Test Appointment 1".equals(e.getTitle())
           && "Test Appointment 2".equals(e.getTitle())));
}

代码示例来源:origin: google/data-transfer-project

server.enqueue(new MockResponse().setBody(BATCH_CALENDAR_RESPONSE));
server.enqueue(new MockResponse().setResponseCode(201).setBody(BATCH_EVENT_RESPONSE));
server.start();

代码示例来源:origin: facebook/stetho

server.start();
server.enqueue(new MockResponse()
  .setBody("Success!"));

代码示例来源:origin: google/data-transfer-project

server.enqueue(content2Response);
server.start();

代码示例来源:origin: com.squareup.okhttp/mockwebserver

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

代码示例来源:origin: com.squareup.okhttp/mockwebserver

private synchronized void maybeStart() {
 if (started) return;
 try {
  start();
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: com.squareup.okhttp/mockwebserver

/**
 * 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: com.squareup.okhttp/mockwebserver

/**
 * 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: cdancy/jenkins-rest

/**
 * Create a MockWebServer with an initial bread-crumb response.
 *
 * @return instance of MockWebServer
 * @throws IOException
 *             if unable to start/play server
 */
public static MockWebServer mockWebServer() throws IOException {
  final MockWebServer server = new MockWebServer();
  server.start();
  return server;
}

代码示例来源:origin: cdancy/bitbucket-rest

/**
 * Create a MockWebServer.
 *
 * @return instance of MockWebServer
 * @throws IOException
 *             if unable to start/play server
 */
public static MockWebServer mockWebServer() throws IOException {
  final MockWebServer server = new MockWebServer();
  server.start();
  return server;
}

代码示例来源:origin: Karumi/MarvelApiClientAndroid

@Before public void setUp() throws Exception {
 System.setProperty("user.timezone", ANY_TIME_ZONE);
 MockitoAnnotations.initMocks(this);
 this.server = new MockWebServer();
 this.server.start();
}

代码示例来源:origin: cfg4j/cfg4j

private void runMockServer() throws IOException {
  server = new MockWebServer();
  server.setDispatcher(dispatcher);
  server.start(0);
 }
}

代码示例来源:origin: cfg4j/cfg4j

private void runMockServer() throws IOException {
  server = new MockWebServer();
  server.setDispatcher(dispatcher);
  server.start(0);
 }
}

代码示例来源:origin: Coinomi/coinomi-android

@Before
public void setup() throws IOException {
  server = new MockWebServer();
  server.start();
  shapeShift = new ShapeShift();
  shapeShift.baseUrl = server.getUrl("/").toString();
  shapeShift.client.setConnectionSpecs(Collections.singletonList(ConnectionSpec.CLEARTEXT));
}

代码示例来源:origin: openwalletGH/openwallet-android

@Before
public void setup() throws IOException {
  server = new MockWebServer();
  server.start();
  shapeShift = new ShapeShift();
  shapeShift.baseUrl = server.getUrl("/").toString();
  shapeShift.client.setConnectionSpecs(Collections.singletonList(ConnectionSpec.CLEARTEXT));
}

代码示例来源:origin: plivo/plivo-java

@Before
public void setUp() throws Exception {
 server = new MockWebServer();
 server.start();
 PlivoClient.BASE_URL = server.url("/").toString();
 Plivo.init(authId, authToken);
 Plivo.getClient().setTesting(true);
}

代码示例来源:origin: team-supercharge/rxsnappy

@Override
protected void setUp() throws Exception {
  super.setUp();
  RxSnappy.init(getContext());
  mockWebServer = new MockWebServer();
  mockWebServer.start(9812);
  OkHttpClient okHttpClient = new OkHttpClient();
  RestAdapter restAdapter = new RestAdapter.Builder()
      .setClient(new OkClient(okHttpClient))
      .setEndpoint(mockWebServer.getUrl("/").toString())
      .build();
  testRestAdapter = restAdapter.create(TestRestAdapter.class);
  rxSnappyClient = new RxSnappyClient();
}

代码示例来源:origin: appnexus/mobile-sdk-android

@Before
public void setup() {
  Robolectric.getBackgroundThreadScheduler().reset();
  Robolectric.getForegroundThreadScheduler().reset();
  ShadowLog.stream = System.out;
  activity = Robolectric.buildActivity(MockMainActivity.class).create().start().resume().visible().get();
  shadowOf(activity).grantPermissions("android.permission.INTERNET");
  server= new MockWebServer();
  try {
    server.start();
    HttpUrl url= server.url("/");
    UTConstants.REQUEST_BASE_URL_UT = url.toString();
    System.out.println(UTConstants.REQUEST_BASE_URL_UT);
    ShadowSettings.setTestURL(url.toString());
  } catch (IOException e) {
    System.out.print("IOException");
  }
  bgScheduler = Robolectric.getBackgroundThreadScheduler();
  uiScheduler = Robolectric.getForegroundThreadScheduler();
  Robolectric.flushBackgroundThreadScheduler();
  Robolectric.flushForegroundThreadScheduler();
  bgScheduler.pause();
  uiScheduler.pause();
}

代码示例来源:origin: appnexus/mobile-sdk-android

@Before
public void setup() {
  Robolectric.getBackgroundThreadScheduler().reset();
  Robolectric.getForegroundThreadScheduler().reset();
  ShadowLog.stream = System.out;
  activity = Robolectric.buildActivity(MockMainActivity.class).create().start().resume().visible().get();
  shadowOf(activity).grantPermissions("android.permission.INTERNET");
  server= new MockWebServer();
  try {
    server.start();
    HttpUrl url= server.url("/");
    UTConstants.REQUEST_BASE_URL_UT = url.toString();
    System.out.println(UTConstants.REQUEST_BASE_URL_UT);
    ShadowSettings.setTestURL(url.toString());
    TestResponsesUT.setTestURL(url.toString());
  } catch (IOException e) {
    System.out.print("IOException");
  }
  bgScheduler = Robolectric.getBackgroundThreadScheduler();
  uiScheduler = Robolectric.getForegroundThreadScheduler();
  Robolectric.flushBackgroundThreadScheduler();
  Robolectric.flushForegroundThreadScheduler();
  bgScheduler.pause();
  uiScheduler.pause();
}

相关文章