Skip to content

Instantly share code, notes, and snippets.

@mgp
Created June 26, 2015 22:27
Show Gist options
  • Save mgp/2384a3172c76ebd80cb3 to your computer and use it in GitHub Desktop.
Save mgp/2384a3172c76ebd80cb3 to your computer and use it in GitHub Desktop.
package org.khanacademy.core.net.videodownloadmanager.okhttp;
import org.khanacademy.core.base.BaseTestCase;
import org.khanacademy.core.net.videodownloadmanager.OkHttpVideoDownloadManager;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
/**
* Tests for {@link OkHttpVideoDownloadManager}.
*/
public class OkHttpVideoDownloadManagerTest extends BaseTestCase {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
/*
MockWebServer server;
File storageDirectory;
OkHttpVideoDownloadManager downloadManager;
Iterator<VideoDownloadEvent> downloadEventIterator;
@Before
public void createMockWebServer() throws IOException {
server = new MockWebServer();
storageDirectory = temporaryFolder.newFolder();
downloadManager = new OkHttpVideoDownloadManager(storageDirectory, new OkHttpClient());
downloadEventIterator =
downloadManager.getVideoDownloadEvents().toBlocking().getIterator();
}
@After
public void shutDownMockWebServer() throws IOException {
server.shutdown();
server = null;
// The temporary folder is automatically deleted by JUnit when the test finishes.
downloadManager.close();
downloadManager = null;
}
*/
/** @return a video with an MP4 download URL with the specified path from the mock server
*/
/*
private Video videoWithMp4DownloadUrlForPath(final String path) throws URISyntaxException {
URL url = server.getUrl(path);
final Video video = Random.video();
return new Video(video.contentId,
ImmutableMap.of(DownloadFormat.MP4, url.toURI()),
video.duration,
video.keywords,
video.readableId,
video.translatedTitle,
video.translatedDescription,
video.webSiteUrl,
video.youTubeId);
}
private void assertManagedVideoDownloads(Set<VideoDownload> expectedVideoDownloads) {
assertEquals(expectedVideoDownloads, downloadManager.getManagedVideoDownloads().all());
}
@Test
public void testMissingVideoGeneratesError() throws Exception {
server.enqueue(new MockResponse().setResponseCode(404));
server.start();
final Video video = videoWithMp4DownloadUrlForPath("/video");
downloadManager.addVideo(video);
final VideoDownload pendingVideoDownload = VideoDownload.createPending(video);
assertEquals(
new VideoDownloadEvent(Type.VIDEO_ADDED, pendingVideoDownload),
downloadEventIterator.next()
);
assertManagedVideoDownloads(ImmutableSet.of(pendingVideoDownload));
assertEquals(
new VideoDownloadEvent(Type.ERROR, pendingVideoDownload),
downloadEventIterator.next()
);
assertManagedVideoDownloads(ImmutableSet.of());
}
@Test
public void testFoo() throws Exception {
final MockResponse response = new MockResponse();
// Get the headers with the Content-Length for a longer body.
response.setBody("0123456789abcdef");
final Headers longerBodyHeaders = response.getHeaders();
// Set a shorter body, but preserve the longer content length.
response.setBody("abcdef");
response.setHeaders(longerBodyHeaders);
server.enqueue(response);
server.start();
final Video video = videoWithMp4DownloadUrlForPath("/video");
downloadManager.addVideo(video);
assertEquals(
new VideoDownloadEvent(Type.VIDEO_ADDED, VideoDownload.createPending(video)),
downloadEventIterator.next()
);
}
@Test
public void testBar() throws Exception {
server.enqueue(new MockResponse().setBody("abcdef"));
server.start();
final Video video = videoWithMp4DownloadUrlForPath("/video");
downloadManager.addVideo(video);
assertEquals(
new VideoDownloadEvent(Type.VIDEO_ADDED, VideoDownload.createPending(video)),
downloadEventIterator.next()
);
while (true) {
VideoDownloadEvent videoDownloadEvent = downloadEventIterator.next();
System.out.println("videoDownloadEvent");
}
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment