mirror of
https://github.com/nextcloud/notes-android.git
synced 2024-11-28 11:29:01 +03:00
#358 Better URL handling
This commit is contained in:
parent
56907e4d93
commit
ec492b85ec
4 changed files with 73 additions and 16 deletions
|
@ -2,6 +2,9 @@ package it.niedermann.owncloud.notes.util;
|
|||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Tests the NoteUtil
|
||||
* Created by stefan on 06.10.15.
|
||||
|
@ -39,19 +42,39 @@ public class NoteUtilTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testIsEmptyLine() {
|
||||
assertTrue(NoteUtil.isEmptyLine(" "));
|
||||
assertTrue(NoteUtil.isEmptyLine("\n"));
|
||||
assertTrue(NoteUtil.isEmptyLine("\n "));
|
||||
assertTrue(NoteUtil.isEmptyLine(" \n"));
|
||||
assertTrue(NoteUtil.isEmptyLine(" \n "));
|
||||
assertFalse(NoteUtil.isEmptyLine("a \n "));
|
||||
try {
|
||||
Method m = NoteUtil.class.getDeclaredMethod("isEmptyLine");
|
||||
m.setAccessible(true);
|
||||
assertTrue((Boolean) m.invoke(null, " "));
|
||||
assertTrue((Boolean) m.invoke(null, "\n"));
|
||||
assertTrue((Boolean) m.invoke(null, "\n "));
|
||||
assertTrue((Boolean) m.invoke(null, " \n"));
|
||||
assertTrue((Boolean) m.invoke(null, " \n "));
|
||||
assertFalse((Boolean) m.invoke(null, "a \n "));
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetLineWithoutMarkDown() {
|
||||
assertEquals("Test", NoteUtil.getLineWithoutMarkDown("Test", 0));
|
||||
assertEquals("Test", NoteUtil.getLineWithoutMarkDown("\nTest", 0));
|
||||
assertEquals("Foo", NoteUtil.getLineWithoutMarkDown("Foo\nBar", 0));
|
||||
assertEquals("Bar", NoteUtil.getLineWithoutMarkDown("Foo\nBar", 1));
|
||||
try {
|
||||
Method m = NoteUtil.class.getDeclaredMethod("isEmptyLine");
|
||||
m.setAccessible(true);
|
||||
assertEquals("Test", (String) m.invoke(null, "Test", 0));
|
||||
assertEquals("Test", (String) m.invoke(null, "\nTest", 0));
|
||||
assertEquals("Foo", (String) m.invoke(null, "Foo\nBar", 0));
|
||||
assertEquals("Bar", (String) m.invoke(null, "Foo\nBar", 1));
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void testGenerateNoteTitle() {
|
||||
|
|
|
@ -2,11 +2,27 @@ package it.niedermann.owncloud.notes.util;
|
|||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Tests the NotesClientUtil
|
||||
* Created by stefan on 24.09.15.
|
||||
*/
|
||||
public class NotesClientUtilTest extends TestCase {
|
||||
public void testFormatURL() {
|
||||
assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/"));
|
||||
assertEquals("http://example.com/", NotesClientUtil.formatURL("http://example.com/"));
|
||||
assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php"));
|
||||
assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/"));
|
||||
assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps"));
|
||||
assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps/notes"));
|
||||
assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps/notes/api"));
|
||||
assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps/notes/api/v0.2"));
|
||||
assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps/notes/api/v0.2/notes"));
|
||||
assertEquals("https://example.com/nextcloud/", NotesClientUtil.formatURL("example.com/nextcloud"));
|
||||
assertEquals("http://example.com:443/nextcloud/", NotesClientUtil.formatURL("http://example.com:443/nextcloud/index.php/apps/notes/api/v0.2/notes"));
|
||||
}
|
||||
|
||||
public void testIsHttp() {
|
||||
assertTrue(NotesClientUtil.isHttp("http://example.com"));
|
||||
assertTrue(NotesClientUtil.isHttp("http://www.example.com/"));
|
||||
|
|
|
@ -178,16 +178,13 @@ public class SettingsActivity extends AppCompatActivity {
|
|||
String url = field_url.getText().toString().trim();
|
||||
String username = field_username.getText().toString();
|
||||
String password = field_password.getText().toString();
|
||||
|
||||
if(password.isEmpty()) {
|
||||
password = old_password;
|
||||
}
|
||||
|
||||
if (!url.endsWith("/")) {
|
||||
url += "/";
|
||||
}
|
||||
if (!url.startsWith("http://") && !url.startsWith("https://")) {
|
||||
url = "https://" + url;
|
||||
}
|
||||
url = NotesClientUtil.formatURL(url);
|
||||
|
||||
new LoginValidatorAsyncTask().execute(url, username, password);
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,27 @@ public class NotesClientUtil {
|
|||
return url != null && url.length() > 4 && url.startsWith("http") && url.charAt(4) != 's';
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips the api part from the path of a given url, handles trailing slash and missing protocol
|
||||
* @param url String
|
||||
* @return formatted URL
|
||||
*/
|
||||
public static String formatURL(String url) {
|
||||
if (!url.endsWith("/")) {
|
||||
url += "/";
|
||||
}
|
||||
if (!url.startsWith("http://") && !url.startsWith("https://")) {
|
||||
url = "https://" + url;
|
||||
}
|
||||
String[] replacements = new String[] {"notes/", "v0.2/", "api/", "notes/", "apps/", "index.php/"};
|
||||
for(String replacement: replacements) {
|
||||
if(url.endsWith(replacement)) {
|
||||
url = url.substring(0, url.length() - replacement.length());
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url String
|
||||
* @param username String
|
||||
|
|
Loading…
Reference in a new issue