private MockResponse createContentResponse(String contentType, byte[] content) { MockResponse response = new MockResponse(); response.setHeader("content-type", contentType); Buffer body = new Buffer(); body.write(content); response.setBody(body); return response; }
@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()))); }
@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(); }
@Test @SuppressWarnings("unchecked") public void testImport() throws Exception { server.enqueue(new MockResponse().setBody(BATCH_CALENDAR_RESPONSE)); server.enqueue(new MockResponse().setResponseCode(201).setBody(BATCH_EVENT_RESPONSE)); server.start();
@Test public void testExport() throws Exception { server.enqueue(new MockResponse().setBody(PHOTOS_RESPONSE)); server.enqueue(new MockResponse().setBody(FOLDER_RESPONSE));
server.enqueue(new MockResponse() .setBody("Success!"));
/** * Returns an early guess of the next response, used for policy on how an * incoming request should be received. The default implementation returns an * empty response. Mischievous implementations can return other values to test * HTTP edge cases, such as unhappy socket policies or throttled request * bodies. */ public MockResponse peek() { return new MockResponse().setSocketPolicy(SocketPolicy.KEEP_OPEN); } }
public void setFailFast(boolean failFast) { MockResponse failFastResponse = failFast ? new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) : null; setFailFast(failFastResponse); }
public void getNameReturnsName() { server().enqueue(new MockResponse().setBody("<html><body>" + "<form name='x'/>" + "</body></html>")); String actual = newBrowser().get(url(server())) .getForm("x") .getName(); assertThat("form name", actual, is("x")); }
@Test public void getRelWhenAnchorReturnsRelationship() { server().enqueue(new MockResponse().setBody("<html><body>" + "<a rel='x'/>" + "</body></html>")); String actual = newBrowser().get(url(server())) .getLink("x") .getRel(); assertThat("link rel", actual, is("x")); }
@Test public void getRelWhenLinkReturnsRelationship() { server().enqueue(new MockResponse().setBody("<html><body>" + "<link rel='x'/>" + "</body></html>")); String actual = newBrowser().get(url(server())) .getLink("x") .getRel(); assertThat("link rel", actual, is("x")); }
@Test public void fetchSettings() throws Exception { server.enqueue(new MockResponse()); Client.Connection connection = client.fetchSettings(); assertThat(connection.os).isNull(); assertThat(connection.is).isNotNull(); assertThat(connection.connection.getResponseCode()).isEqualTo(200); RecordedRequestAssert.assertThat(server.takeRequest()) .hasRequestLine("GET /v1/projects/foo/settings HTTP/1.1") .containsHeader("User-Agent", ConnectionFactory.USER_AGENT) .containsHeader("Content-Type", "application/json"); }
@Test(expected = ShapeShiftException.class) public void testGetMarketInfoFail() throws ShapeShiftException, IOException { server.enqueue(new MockResponse().setBody(MARKET_INFO_BTC_NBT_JSON)); // Incorrect pair shapeShift.getMarketInfo(BTC, LTC); }
@Test(expected = ShapeShiftException.class) public void testGetTxStatusFail() throws ShapeShiftException, AddressMalformedException, IOException { server.enqueue(new MockResponse().setBody(TX_STATUS_COMPLETE_JSON)); // Used an incorrect address, correct is 1NDQPAGamGePkSZXW2CYBzXJEefB7N4bTN shapeShift.getTxStatus(BTC.newAddress("18ETaXCYhJ8sxurh41vpKC3E6Tu7oJ94q8")); }
@Test(expected = ShapeShiftException.class) public void testNormalTransactionFail() throws ShapeShiftException, AddressMalformedException, IOException { server.enqueue(new MockResponse().setBody(NORMAL_TRANSACTION_JSON)); // Incorrect Dogecoin address, correct is DMHLQYG4j96V8cZX9WSuXxLs5RnZn6ibrV shapeShift.exchange(DOGE.newAddress("DSntbp199h851m3Y1g3ruYCQHzWYCZQmmA"), BTC.newAddress("1Nz4xHJjNCnZFPjRUq8CN4BZEXTgLZfeUW")); }
@Test public void followWhenInvalidUrlThrowsException() { server().enqueue(new MockResponse().setBody("<html><body>" + "<a rel='r' href='x:/a'>a</a>" + "</body></html>")); Link link = newBrowser().get(url(server())) .getLink("r"); thrown().expect(IllegalArgumentException.class); thrown().expectMessage("Invalid URL: x:/a"); link.follow(); }
@Test public void setControlValueWithUnknownNameThrowsException() { server().enqueue(new MockResponse().setBody("<html><body>" + "<form name='f'/>" + "</body></html>")); Form form = newBrowser().get(url(server())) .getForm("f"); thrown().expect(ControlNotFoundException.class); thrown().expectMessage("x"); form.setControlValue("x", "y"); }
@Test public void submitWhenNoSubmitButtonThrowsException() { server().enqueue(new MockResponse().setBody("<html><body>" + "<form name='f'/>" + "</body></html>")); Form form = newBrowser().get(url(server())) .getForm("f"); thrown().expect(IllegalStateException.class); thrown().expectMessage("Missing form submit button"); form.submit(); }
@Test(expected = ShapeShiftException.class) public void testFixedAmountTransactionFail() throws ShapeShiftException, AddressMalformedException, IOException { server.enqueue(new MockResponse().setBody(FIXED_AMOUNT_TRANSACTION_JSON)); // We withdraw Dogecoins to a Bitcoin address shapeShift.exchangeForAmount(DOGE.value("1000"), BTC.newAddress("18ETaXCYhJ8sxurh41vpKC3E6Tu7oJ94q8"), BTC.newAddress("1Nz4xHJjNCnZFPjRUq8CN4BZEXTgLZfeUW")); }
@Test(expected = ShapeShiftException.class) public void testFixedAmountTransactionFail2() throws ShapeShiftException, AddressMalformedException, IOException { server.enqueue(new MockResponse().setBody(FIXED_AMOUNT_TRANSACTION_JSON)); // Incorrect Dogecoin address, correct is DMHLQYG4j96V8cZX9WSuXxLs5RnZn6ibrV shapeShift.exchangeForAmount(DOGE.value("1000"), DOGE.newAddress("DSntbp199h851m3Y1g3ruYCQHzWYCZQmmA"), BTC.newAddress("1Nz4xHJjNCnZFPjRUq8CN4BZEXTgLZfeUW")); }