Generate and verify Https links

This commit is contained in:
Coffeemakr 2018-01-04 17:21:03 +01:00
parent 5f2d0cf6b5
commit fe2583f3d7
No known key found for this signature in database
GPG Key ID: 3F35676D8FF6E743
8 changed files with 74 additions and 24 deletions

View File

@ -0,0 +1,35 @@
package org.schabi.newpipe.extractor.services.soundcloud;
import com.grack.nanojson.JsonObject;
import javax.annotation.Nonnull;
public class SoundcloudExtractorHelper {
private static final String HTTP = "http://";
private static final String HTTPS = "https://";
private static String replaceHttpWithHttps(final String url) {
if(!url.isEmpty() && url.startsWith(HTTP)) {
return HTTPS + url.substring(HTTP.length());
}
return url;
}
@Nonnull
static String getUploaderUrl(JsonObject object) {
String url = object.getObject("user").getString("permalink_url", "");
return replaceHttpWithHttps(url);
}
@Nonnull
static String getAvatarUrl(JsonObject object) {
String url = object.getObject("user", new JsonObject()).getString("avatar_url", "");
return replaceHttpWithHttps(url);
}
public static String getUploaderName(JsonObject object) {
return object.getObject("user").getString("username", "");
}
}

View File

@ -4,7 +4,6 @@ import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@ -69,17 +68,17 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
@Override
public String getUploaderUrl() {
return playlist.getObject("user").getString("permalink_url", "");
return SoundcloudExtractorHelper.getUploaderUrl(playlist);
}
@Override
public String getUploaderName() {
return playlist.getObject("user").getString("username", "");
return SoundcloudExtractorHelper.getUploaderName(playlist);
}
@Override
public String getUploaderAvatarUrl() {
return playlist.getObject("user", new JsonObject()).getString("avatar_url", "");
return SoundcloudExtractorHelper.getAvatarUrl(playlist);
}
@Override

View File

@ -102,19 +102,19 @@ public class SoundcloudStreamExtractor extends StreamExtractor {
@Nonnull
@Override
public String getUploaderUrl() {
return track.getObject("user").getString("permalink_url", "");
return SoundcloudExtractorHelper.getUploaderUrl(track);
}
@Nonnull
@Override
public String getUploaderName() {
return track.getObject("user").getString("username", "");
return SoundcloudExtractorHelper.getUploaderName(track);
}
@Nonnull
@Override
public String getUploaderAvatarUrl() {
return track.getObject("user", new JsonObject()).getString("avatar_url", "");
return SoundcloudExtractorHelper.getAvatarUrl(track);
}
@Override

View File

@ -1,9 +1,12 @@
package org.schabi.newpipe.extractor;
import javax.annotation.Nonnull;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class ExtractorAsserts {
public static void assertEmptyErrors(String message, List<Throwable> errors) {
if(!errors.isEmpty()) {
@ -14,11 +17,21 @@ public class ExtractorAsserts {
}
}
public static void assertIsValidUrl(String url) {
@Nonnull
private static URL urlFromString(String url) {
try {
new URL(url);
return new URL(url);
} catch (MalformedURLException e) {
throw new AssertionError("Invalid url: " + url, e);
}
}
public static void assertIsValidUrl(String url) {
urlFromString(url);
}
public static void assertIsSecureUrl(String urlToCheck) {
URL url = urlFromString(urlToCheck);
assertEquals("Protocol of URL is not secure", "https", url.getProtocol());
}
}

View File

@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import static org.junit.Assert.*;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
/**
@ -43,7 +44,7 @@ public class SoundcloudChannelExtractorTest {
@Test
public void testGetAvatarUrl() throws Exception {
assertTrue(extractor.getAvatarUrl().contains("https://"));
assertIsSecureUrl(extractor.getAvatarUrl());
}
@Test

View File

@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import static org.junit.Assert.*;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
/**
@ -42,12 +43,13 @@ public class SoundcloudPlaylistExtractorTest {
@Test
public void testGetThumbnailUrl() throws Exception {
assertTrue(extractor.getThumbnailUrl(), extractor.getThumbnailUrl().contains("https://"));
assertIsSecureUrl(extractor.getThumbnailUrl());
}
@Test
public void testGetUploaderUrl() throws Exception {
assertEquals(extractor.getUploaderUrl(), "http://soundcloud.com/liluzivert");
assertIsSecureUrl(extractor.getUploaderUrl());
assertEquals(extractor.getUploaderUrl(), "https://soundcloud.com/liluzivert");
}
@Test
@ -57,7 +59,7 @@ public class SoundcloudPlaylistExtractorTest {
@Test
public void testGetUploaderAvatarUrl() throws Exception {
assertTrue(extractor.getUploaderAvatarUrl(), extractor.getUploaderAvatarUrl().contains("https://"));
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
}
@Test

View File

@ -14,6 +14,7 @@ import org.schabi.newpipe.extractor.stream.SubtitlesFormat;
import java.io.IOException;
import static org.junit.Assert.*;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
/**
@ -69,22 +70,23 @@ public class SoundcloudStreamExtractorDefaultTest {
@Test
public void testGetUploadDate() throws ParsingException {
assertEquals(extractor.getUploadDate(), "2016-07-31");
assertEquals("2016-07-31", extractor.getUploadDate());
}
@Test
public void testGetUploaderUrl() throws ParsingException {
assertEquals(extractor.getUploaderUrl(), "http://soundcloud.com/liluzivert");
assertIsSecureUrl(extractor.getUploaderUrl());
assertEquals("https://soundcloud.com/liluzivert", extractor.getUploaderUrl());
}
@Test
public void testGetThumbnailUrl() throws ParsingException {
assertTrue(extractor.getThumbnailUrl(), extractor.getThumbnailUrl().contains("https://"));
assertIsSecureUrl(extractor.getThumbnailUrl());
}
@Test
public void testGetUploaderAvatarUrl() throws ParsingException {
assertTrue(extractor.getUploaderAvatarUrl(), extractor.getUploaderAvatarUrl().contains("https://"));
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
}
@Test

View File

@ -3,6 +3,7 @@ package org.schabi.newpipe.extractor.services.youtube;
import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.Downloader;
import org.schabi.newpipe.extractor.ExtractorAsserts;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@ -12,6 +13,7 @@ import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException;
import static org.junit.Assert.*;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
/*
@ -38,7 +40,6 @@ import static org.schabi.newpipe.extractor.ServiceList.YouTube;
* Test for {@link StreamExtractor}
*/
public class YoutubeStreamExtractorDefaultTest {
public static final String HTTPS = "https://";
private static YoutubeStreamExtractor extractor;
@BeforeClass
@ -102,14 +103,12 @@ public class YoutubeStreamExtractorDefaultTest {
@Test
public void testGetThumbnailUrl() throws ParsingException {
assertTrue(extractor.getThumbnailUrl(),
extractor.getThumbnailUrl().contains(HTTPS));
assertIsSecureUrl(extractor.getThumbnailUrl());
}
@Test
public void testGetUploaderAvatarUrl() throws ParsingException {
assertTrue(extractor.getUploaderAvatarUrl(),
extractor.getUploaderAvatarUrl().contains(HTTPS));
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
}
@Test
@ -120,8 +119,7 @@ public class YoutubeStreamExtractorDefaultTest {
@Test
public void testGetVideoStreams() throws IOException, ExtractionException {
for (VideoStream s : extractor.getVideoStreams()) {
assertTrue(s.url,
s.url.contains(HTTPS));
assertIsSecureUrl(s.url);
assertTrue(s.resolution.length() > 0);
assertTrue(Integer.toString(s.getFormatId()),
0 <= s.getFormatId() && s.getFormatId() <= 4);