@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 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(); }
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()))); }
/** * Mocked standard Dimension Data Unexpected Error 400 response * * @return {@link MockResponse} */ protected MockResponse responseUnexpectedError() { return new MockResponse().setResponseCode(400).setStatus("HTTP/1.1 400 Bad Request") .setBody("{\"operation\":\"OPERATION\",\"responseCode\":\"UNEXPECTED_ERROR\"}"); }
@Test public void test2CookiesReset() { server.enqueue(new MockResponse().setResponseCode(200).setHeader("Set-Cookie", TestResponsesUT.UUID_COOKIE_RESET).setBody(TestResponsesUT.banner())); requestManager.execute(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); String wvcookie = WebviewUtil.getCookie(); System.out.println(wvcookie); assertEquals(getUUId2(wvcookie), getUUId2(TestResponsesUT.UUID_COOKIE_RESET)); }
@Test public void testSucceedingSSMMediationCall() { server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.mediatedSSMBanner())); server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.DUMMY_BANNER_CONTENT).setBodyDelay(2, TimeUnit.MILLISECONDS)); server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blank())); runBasicSSMMediationTest(SUCCESS, ASSERT_AD_LOAD_SUCESS, CHECK_LATENCY_TRUE); }
private static MockResponse enabledResponse() { return new MockResponse() .addHeader(CDN_ENABLED, "true") .addHeader(CDN_LOG_RETENTION, "false") .addHeader(CDN_TTL, "777777") .addHeader(CDN_URI, "http://id-1.cdn.rackspace.com") .addHeader(CDN_SSL_URI, "https://ssl-id-1.ssl.rackspace.com") .addHeader(CDN_STREAMING_URI, "http://streaming-id-1.stream.rackspace.com") .addHeader(CDN_IOS_URI, "http://ios-id-1.iosr.rackspace.com") .addHeader(CONTENT_LENGTH, "0") .addHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); }
protected void enqueueXml(Response.Status status, String region, String resource) { enqueue(region, new MockResponse() .setStatus("HTTP/1.1 " + status.getStatusCode() + " " + status.getReasonPhrase()) .addHeader(CONTENT_TYPE, APPLICATION_XML) .setBody(stringFromResource(resource))); }
public void setFailFast(boolean failFast) { MockResponse failFastResponse = failFast ? new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) : null; setFailFast(failFastResponse); }
protected MockResponse response202WithHeader() { return new MockResponse() .setStatus("HTTP/1.1 202 Accepted") .addHeader( "Location", "https://management.azure.com/subscriptions/SUBSCRIPTIONID/operationresults/eyJqb2JJZCI6IlJFU09VUkNFR1JPVVBERUxFVElPTkpPQi1SVEVTVC1DRU5UUkFMVVMiLCJqb2JMb2NhdGlvbiI6ImNlbnRyYWx1cyJ9?api-version=2014-04-01"); }
/** * 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); } }
@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"); }
/** * Removes all headers named {@code name}, then adds a new header with the * name and value. */ public MockResponse setHeader(String name, Object value) { removeHeader(name); return addHeader(name, value); }
public void testGetJobStatusNoContent() throws InterruptedException { server.enqueue(jsonResponse("/resourcegroup.json").setStatus("HTTP/1.1 204 No Content")); JobStatus status = api.getJobApi().jobStatus(URI.create(requestUrl)); assertEquals(status, JobStatus.NO_CONTENT); assertSent(server, "GET", requestUrl); }
/** * Attempts to perform a web socket upgrade on the connection. This will overwrite any previously * set status or body. */ public MockResponse withWebSocketUpgrade(WebSocketListener listener) { setStatus("HTTP/1.1 101 Switching Protocols"); setHeader("Connection", "Upgrade"); setHeader("Upgrade", "websocket"); body = null; webSocketListener = listener; return this; }
/** Creates a new mock response with an empty body. */ public MockResponse() { setHeader("Content-Length", 0); }
@Test public void testExport() throws Exception { server.enqueue(new MockResponse().setBody(PHOTOS_RESPONSE)); server.enqueue(new MockResponse().setBody(FOLDER_RESPONSE));
/** * Mocked standard Dimension Data Resource Not Found 400 response * * @return {@link MockResponse} */ protected MockResponse responseResourceNotFound() { return new MockResponse().setResponseCode(400).setStatus("HTTP/1.1 400 Bad Request") .setBody("{\"operation\":\"OPERATION\",\"responseCode\":\"RESOURCE_NOT_FOUND\"}"); }
@Test public void test1CookiesSync() { server.enqueue(new MockResponse().setResponseCode(200).setHeader("Set-Cookie", TestResponsesUT.UUID_COOKIE_1).setBody(TestResponsesUT.banner())); requestManager.execute(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); String wvcookie = WebviewUtil.getCookie(); //Asserts the Cookie stored in the device is the same as that of the one we sent back in the response. assertEquals(getUUId2(wvcookie), getUUId2(TestResponsesUT.UUID_COOKIE_1)); }