Java Code Examples for okhttp3.mockwebserver.MockWebServer#url()

The following examples show how to use okhttp3.mockwebserver.MockWebServer#url() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebAPITest.java    From JavaSteam with MIT License 6 votes vote down vote up
@Before
public void setUp() throws IOException {
    lock = new CountDownLatch(1);
    server = new MockWebServer();

    server.enqueue(new MockResponse().setBody("" +
            "\"root\"" +
            "{" +
            "    \"name\" \"stringvalue\"" +
            "}"));

    server.start();

    baseUrl = server.url("/");

    config = SteamConfiguration.create(new Consumer<ISteamConfigurationBuilder>() {
        @Override
        public void accept(ISteamConfigurationBuilder b) {
            b.withWebAPIBaseAddress(baseUrl.toString());
        }
    });
}
 
Example 2
Source File: SixpackTest.java    From sixpack-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testGetClientIdInterceptor() throws IOException {
    String clientId = "test-client-id";

    MockWebServer webServer = new MockWebServer();
    webServer.enqueue(new MockResponse());
    webServer.start();
    HttpUrl url = webServer.url("/");

    Interceptor clientIdInterceptor = Sixpack.getClientIdInterceptor(clientId);
    assertNotNull(clientIdInterceptor);

    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(clientIdInterceptor)
            .build();

    Response response = client.newCall(new Request.Builder().url(url).build()).execute();

    assertEquals(clientId, response.request().url().queryParameter("client_id"));
}
 
Example 3
Source File: FederationServerTest.java    From java-stellar-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testNameFederationSuccess() throws IOException {
  MockWebServer mockWebServer = new MockWebServer();
  mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(successResponse));
  mockWebServer.start();
  HttpUrl baseUrl = mockWebServer.url("");

  FederationServer server = new FederationServer(
          baseUrl.toString(),
          "stellar.org"
  );

  FederationResponse response = server.resolveAddress("bob*stellar.org");
  assertEquals(response.getStellarAddress(), "bob*stellar.org");
  assertEquals(response.getAccountId(), "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY");
  assertNull(response.getMemoType());
  assertNull(response.getMemo());
}
 
Example 4
Source File: MapMatchingTracepointTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  server = new MockWebServer();
  server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() {
    @Override
    public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
      try {
        String response = loadJsonFixture(MAP_MATCHING_FIXTURE);
        return new MockResponse().setBody(response);
      } catch (IOException ioException) {
        throw new RuntimeException(ioException);
      }
    }
  });
  server.start();
  mockUrl = server.url("");

  coordinates = new ArrayList<>();
  coordinates.add(Point.fromLngLat(13.418946862220764, 52.50055852688439));
  coordinates.add(Point.fromLngLat(13.419011235237122, 52.50113000479732));
  coordinates.add(Point.fromLngLat(13.419756889343262, 52.50171780290061));
  coordinates.add(Point.fromLngLat(13.419885635375975, 52.50237416816131));
  coordinates.add(Point.fromLngLat(13.420631289482117, 52.50294888790448));
}
 
Example 5
Source File: FederationServerTest.java    From java-stellar-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testNameFederationNotFound() throws IOException {
  MockWebServer mockWebServer = new MockWebServer();
  mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody(notFoundResponse));
  mockWebServer.start();
  HttpUrl baseUrl = mockWebServer.url("");

  FederationServer server = new FederationServer(
          baseUrl.toString(),
          "stellar.org"
  );

  try {
    FederationResponse response = server.resolveAddress("bob*stellar.org");
    fail("Expected exception");
  } catch (NotFoundException e) {}
}
 
Example 6
Source File: MapboxSpeechTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  mockWebServer = new MockWebServer();

  mockWebServer.setDispatcher(new Dispatcher() {
    @Override
    public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
      okio.Buffer buffer = new okio.Buffer();
      try {
        buffer.writeAll(Okio.source(new File("src/test/resources/test_response.mp3")));
      } catch (IOException ioException) {
        throw new RuntimeException(ioException);
      }
      return new MockResponse().setBody(buffer);
    }
  });

  mockWebServer.start();
  mockUrl = mockWebServer.url("");

}
 
Example 7
Source File: ServerTest.java    From java-stellar-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubmitTransactionNetworkMisMatch() throws IOException, AccountRequiresMemoException {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(publicRootResponse));
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(successResponse));
    mockWebServer.start();
    HttpUrl baseUrl = mockWebServer.url("");
    Server server = new Server(baseUrl.toString());

    try {
        server.submitTransaction(this.buildTransaction(Network.TESTNET), true);
        fail("expected NetworkMismatchException exception");
    } catch (NetworkMismatchException e) {
        // expect exception
    }
}
 
Example 8
Source File: MapboxDirectionsRefreshTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  server = new MockWebServer();

  server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() {
    @Override
    public MockResponse dispatch(RecordedRequest request) throws InterruptedException {

      String resource = DIRECTIONS_REFRESH_V1_FIXTURE;

      try {
        String body = loadJsonFixture(resource);
        return new MockResponse().setBody(body);
      } catch (IOException ioException) {
        throw new RuntimeException(ioException);
      }
    }
  });
  server.start();
  mockUrl = server.url("");
}
 
Example 9
Source File: ServerTest.java    From java-stellar-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testCheckMemoRequiredWithSkipCheck() throws IOException, AccountRequiresMemoException {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.setDispatcher(buildTestCheckMemoRequiredMockDispatcher());
    mockWebServer.start();
    HttpUrl baseUrl = mockWebServer.url("");
    Server server = new Server(baseUrl.toString());

    KeyPair source = KeyPair.fromSecretSeed("SDQXFKA32UVQHUTLYJ42N56ZUEM5PNVVI4XE7EA5QFMLA2DHDCQX3GPY");
    Account account = new Account(source.getAccountId(), 1L);
    Transaction transaction = new Transaction.Builder(account, Network.PUBLIC)
            .addOperation(new PaymentOperation.Builder(DESTINATION_ACCOUNT_MEMO_REQUIRED_A, new AssetTypeNative(), "10").build())
            .addOperation(new PathPaymentStrictReceiveOperation.Builder(new AssetTypeNative(), "10", DESTINATION_ACCOUNT_NO_MEMO_REQUIRED, new AssetTypeCreditAlphaNum4("BTC", "GA7GYB3QGLTZNHNGXN3BMANS6TC7KJT3TCGTR763J4JOU4QHKL37RVV2"), "5").build())
            .addOperation(new PathPaymentStrictSendOperation.Builder(new AssetTypeNative(), "10", DESTINATION_ACCOUNT_NO_MEMO_REQUIRED, new AssetTypeCreditAlphaNum4("BTC", "GA7GYB3QGLTZNHNGXN3BMANS6TC7KJT3TCGTR763J4JOU4QHKL37RVV2"), "5").build())
            .addOperation(new AccountMergeOperation.Builder(DESTINATION_ACCOUNT_NO_MEMO_REQUIRED).build())
            .setTimeout(Transaction.Builder.TIMEOUT_INFINITE)
            .setBaseFee(100)
            .build();
    transaction.sign(source);
    server.submitTransaction(transaction, true);
    server.submitTransaction(feeBump(transaction), true);
}
 
Example 10
Source File: PublicUsage.java    From nodes with Apache License 2.0 6 votes vote down vote up
@Before
public void setupMockServer() throws Exception {
    server = new MockWebServer();
    server.start();
    server.enqueue(new MockResponse().setBody("{\n" +
            "  \"data\": {\n" +
            "    \"test\": {\n" +
            "      \"testString\": \"String\"\n" +
            "    }\n" +
            "  }," +
            "  \"errors\": [\n" +
            "    {\n" +
            "      \"message\": \"Cannot query field \\\"invalid\\\" on type \\\"TestTO\\\".\",\n" +
            "      \"locations\": [\n" +
            "        {\n" +
            "          \"line\": 1,\n" +
            "          \"column\": 1\n" +
            "        }\n" +
            "      ]\n" +
            "    }\n" +
            "  ]\n" +
            "}"
    ));
    mockServerUrl = server.url("/test");
}
 
Example 11
Source File: ExchangeRateTest.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    mockWebServer = new MockWebServer();
    mockWebServer.start();

    waiter = new Waiter();

    MockitoAnnotations.initMocks(this);

    exchangeApi = new ExchangeApiImpl(okHttpClient, mockWebServer.url("/"));
}
 
Example 12
Source File: XmrToApiQueryOrderTest.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    mockWebServer = new MockWebServer();
    mockWebServer.start();

    waiter = new Waiter();

    MockitoAnnotations.initMocks(this);

    xmrToApi = new XmrToApiImpl(okHttpClient, mockWebServer.url("/"));
}
 
Example 13
Source File: ZooniverseClientTest.java    From android-galaxyzoo with GNU General Public License v3.0 5 votes vote down vote up
private static ZooniverseClient createZooniverseClient(final MockWebServer server) {
    final HttpUrl mockUrl = server.url("/");

    final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    final Context context = instrumentation.getTargetContext();

    return new ZooniverseClient(context, mockUrl.toString());
}
 
Example 14
Source File: DirectionsWaypointTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Before
public void setUp() throws IOException {
  server = new MockWebServer();

  server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() {
    @Override
    public MockResponse dispatch(RecordedRequest request) throws InterruptedException {

      // Switch response on geometry parameter (only false supported, so nice and simple)
      String resource = DIRECTIONS_V5_FIXTURE;
      if (request.getPath().contains("geometries=polyline6")) {
        resource = DIRECTIONS_V5_PRECISION6_FIXTURE;
      }
      if (request.getPath().contains("driving-traffic")) {
        resource = DIRECTIONS_TRAFFIC_FIXTURE;
      }
      if (request.getPath().contains("-77.04430")) {
        resource = DIRECTIONS_ROTARY_FIXTURE;
      }
      if (request.getPath().contains("annotations")) {
        resource = DIRECTIONS_V5_ANNOTATIONS_FIXTURE;
      }

      try {
        String body = loadJsonFixture(resource);
        return new MockResponse().setBody(body);
      } catch (IOException ioException) {
        throw new RuntimeException(ioException);
      }
    }
  });
  server.start();
  mockUrl = server.url("");
}
 
Example 15
Source File: OptimizationResponseTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  final String json = loadJsonFixture(OPTIMIZATION_FIXTURE);
  object = new JsonParser().parse(json).getAsJsonObject();

  server = new MockWebServer();
  server.setDispatcher(new Dispatcher() {
    @Override
    public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
      return new MockResponse().setBody(json);
    }
  });
  server.start();
  mockUrl = server.url("");
}
 
Example 16
Source File: ServerTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testNextPage() throws IOException, URISyntaxException {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(operationsPageResponse));
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(operationsPageResponse));
    mockWebServer.start();
    HttpUrl baseUrl = mockWebServer.url("");
    Server server = new Server(baseUrl.toString());

    Page<OperationResponse> page = server.operations().execute();
    assertEquals(1, page.getRecords().size());
    assertEquals("dd9d10c80a344f4464df3ecaa63705a5ef4a0533ff2f2099d5ef371ab5e1c046", page.getRecords().get(0).getTransactionHash());
    Page<OperationResponse> nextPage = page.getNextPage(server.getHttpClient());
    assertEquals(1, page.getRecords().size());
}
 
Example 17
Source File: MapboxMapMatchingTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  server = new MockWebServer();
  server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() {
    @Override
    public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
        String resource = MAP_MATCHING_FIXTURE;
        if (request.getPath().contains("approaches")) {
          resource = MAP_MATCHING_APPROACHES;
        } else if (request.getPath().contains("waypoint_names")) {
          resource = MAP_MATCHING_WAYPOINT_NAMES_FIXTURE;
        } else if (request.getPath().contains("0,-40;0,-20")) { // no matching segment
          resource = MAP_MATCHING_ERROR_FIXTURE;
        }
          try {
        String response = loadJsonFixture(resource);
        return new MockResponse().setBody(response);
      } catch (IOException ioException) {
        throw new RuntimeException(ioException);
      }
    }
  });

  coordinates = new ArrayList<>();
  coordinates.add(Point.fromLngLat(13.418946862220764, 52.50055852688439));
  coordinates.add(Point.fromLngLat(13.419011235237122, 52.50113000479732));
  coordinates.add(Point.fromLngLat(13.419756889343262, 52.50171780290061));
  coordinates.add(Point.fromLngLat(13.419885635375975, 52.50237416816131));
  coordinates.add(Point.fromLngLat(13.420631289482117, 52.50294888790448));

  server.start();
  mockUrl = server.url("");
}
 
Example 18
Source File: MapboxMatrixTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Before
public void setUp() throws IOException {
  server = new MockWebServer();

  server.setDispatcher(new Dispatcher() {

    @Override
    public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
      try {
        String resource;
        if (request.getPath().contains("walking")) {
          resource = DIRECTIONS_MATRIX_WALKING_1X3_FIXTURE;

        } else if (request.getPath().contains("cycling")) {
          resource = DIRECTIONS_MATRIX_CYCLING_2X3_FIXTURE;

        } else { // driving
          resource = DIRECTIONS_MATRIX_DRIVING_3X3_FIXTURE;
        }
        String response = loadJsonFixture(resource);
        return new MockResponse().setBody(response);
      } catch (IOException ioException) {
        throw new RuntimeException(ioException);
      }
    }
  });
  server.start();
  mockUrl = server.url("");

  positions = new ArrayList<>();
  positions.add(Point.fromLngLat(-122.42, 37.78));
  positions.add(Point.fromLngLat(-122.45, 37.91));
  positions.add(Point.fromLngLat(-122.48, 37.73));
}
 
Example 19
Source File: FederationServerTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateForDomain() throws IOException, InterruptedException {
  MockWebServer mockWebServer = new MockWebServer();
  mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(stellarToml));
  mockWebServer.start();

  HttpUrl baseUrl = mockWebServer.url("");
  String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port());
  FederationServer server = FederationServer.createForDomain(domain);
  assertEquals(server.getServerUri().toString(), "https://api.stellar.org/federation");
  assertEquals(server.getDomain(), domain);

  RecordedRequest stellarTomlRequest = mockWebServer.takeRequest();
  assertEquals("http://"+domain+"/.well-known/stellar.toml", stellarTomlRequest.getRequestUrl().toString());
}
 
Example 20
Source File: MapboxDirectionsTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Before
public void setUp() throws IOException {
  server = new MockWebServer();

  server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() {
    @Override
    public MockResponse dispatch(RecordedRequest request) throws InterruptedException {

      // Switch response on geometry parameter (only false supported, so nice and simple)
      String resource = DIRECTIONS_V5_FIXTURE;
      if (request.getPath().contains("-77.04430")) {
        resource = DIRECTIONS_ROTARY_FIXTURE;
      } else if (request.getPath().contains("annotations")) {
        resource = DIRECTIONS_V5_ANNOTATIONS_FIXTURE;
      } else if (request.getPath().contains("waypoint_names")) {
        resource = DIRECTIONS_V5_WAYPOINT_NAMES_FIXTURE;
      } else if (request.getPath().contains("waypoint_targets")) {
          resource = DIRECTIONS_V5_WAYPOINT_TARGETS_FIXTURE;
      }else if (request.getPath().contains("approaches")) {
        resource = DIRECTIONS_V5_APPROACHES_REQUEST;
      } else if (request.getPath().contains("-151.2302")) {
        resource = DIRECTIONS_V5_NO_ROUTE;
      } else if (request.getPath().contains("-122.403561,37.777689")) {
        resource = DIRECTIONS_V5_BANNER_INSTRUCTIONS;
      } else if (request.getPath().contains("driving-traffic")) {
        resource = DIRECTIONS_TRAFFIC_FIXTURE;
      } else if (request.getPath().contains("geometries=polyline6")) {
        resource = DIRECTIONS_V5_PRECISION6_FIXTURE;
      } else if (request.getMethod().equals("POST")) {
        resource = DIRECTIONS_V5_POST;
      }

      try {
        String body = loadJsonFixture(resource);
        return new MockResponse().setBody(body);
      } catch (IOException ioException) {
        throw new RuntimeException(ioException);
      }
    }
  });
  server.start();
  mockUrl = server.url("");
}