hookAlmostRealRequestWillBeSent(mMockEventReporter); MockWebServer server = new MockWebServer(); server.start(); server.enqueue(new MockResponse() .setBody("Success!")); compress(decompressed)); Request request = new Request.Builder() .url(server.getUrl("/")) .addHeader("Content-Encoding", "gzip") .post(compressedBody) eq(compressed.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(); HttpUrl baseUrl = server.url(""); MicrosoftCalendarImporter importer = new MicrosoftCalendarImporter( RecordedRequest calendarBatch = server.takeRequest(); Map<String, Object> calendarBody = (Map<String, Object>) mapper.readValue(calendarBatch.getBody().readUtf8(), Map.class); RecordedRequest eventBatch = server.takeRequest(); Map<String, Object> eventRequests = (Map<String, Object>) mapper.readValue(eventBatch.getBody().readUtf8(), Map.class);
@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 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()))); }
public void testDeleteReturns404() throws Exception { server.enqueue(response404()); final VirtualMachineApi vmAPI = api.getVirtualMachineApi("groupname"); URI uri = vmAPI.delete("windowsmachine"); assertEquals(server.getRequestCount(), 1); assertNull(uri); assertSent(server, "DELETE", "/subscriptions/SUBSCRIPTIONID/resourceGroups/groupname/providers/Microsoft.Compute" + "/virtualMachines/windowsmachine?api-version=2018-06-01"); }
public void testConnectContainerToNetwork() throws Exception { MockWebServer server = mockWebServer(new MockResponse().setResponseCode(200)); NetworkApi api = api(DockerApi.class, server.getUrl("/").toString()).getNetworkApi(); try { api.connectContainerToNetwork("123456789", "containerName"); assertSent(server, "POST", "/networks/123456789/connect", "{ \"Container\": \"containerName\" }"); } finally { server.shutdown(); } }
@Test public void testNoRetriesSuccessful() throws Exception { MockWebServer server = mockWebServer(new MockResponse()); IntegrationTestClient client = client(server.getUrl("/").toString()); try { client.download(""); assertEquals(server.getRequestCount(), 1); } finally { closeQuietly(client); server.shutdown(); } }
@Test public void getLinksWithUnknownRelReturnsEmptyList() { server().enqueue(new MockResponse().setBody("<html><body/></html>")); List<Link> actual = newBrowser().get(url(server())) .getLinks("x"); assertThat("links", actual, is(empty())); }
@Test public void getRequestsPath() throws InterruptedException { server().enqueue(new MockResponse()); newBrowser().get(url(server(), "/x")); assertThat("request path", server().takeRequest().getPath(), is("/x")); }
@After public void tearDown() throws Exception { server.shutdown(); } }
@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)); }
/** * Ensures server received authentication request. */ public void assertAuthentication(MockWebServer server) { assertTrue(server.getRequestCount() >= 1); try { assertEquals(server.takeRequest().getRequestLine(), "POST /tokens HTTP/1.1"); } catch (InterruptedException e) { Throwables.propagate(e); } }
private RecordedRequest takeNoAdURLRequestFromQueue(int position){ RecordedRequest request = null; for (int i = 1; i <= position; i++) { try { request = server.takeRequest(); } catch (InterruptedException e) { e.printStackTrace(); } } return request; }
public static MockWebServer mockOpenStackServer() throws IOException { MockWebServer server = new MockWebServer(); server.play(); URL url = server.getUrl(""); server.setDispatcher(getURLReplacingQueueDispatcher(url)); return server; }
/** * Creates a {@link MockWebServer} that uses the * {@link GlobalChecksRequestDispatcher}. */ protected static MockWebServer mockWebServer(MockResponse... responses) throws IOException { MockWebServer server = new MockWebServer(); server.play(); for (MockResponse response : responses) { server.enqueue(response); } return server; }
@BeforeMethod public void start() throws IOException { tempAuthServer = new MockWebServer(); tempAuthServer.play(); swiftServer = new MockWebServer(); swiftServer.play(); }
static MockWebServer createMockWebServer() throws IOException { MockWebServer server = new MockWebServer(); server.play(); URL url = server.getUrl(""); return server; }
private void runMockServer() throws IOException { server = new MockWebServer(); server.setDispatcher(dispatcher); server.start(0); } }
/** * 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; }