Add tests for the URLRewriteController

This commit is contained in:
ligi 2015-11-12 16:50:40 +01:00
parent 08b40ebb35
commit 1985e26d07
5 changed files with 81 additions and 11 deletions

View file

@ -15,7 +15,6 @@ buildscript {
} }
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
classpath 'de.mobilej.unmock:UnMockPlugin:0.3.5' classpath 'de.mobilej.unmock:UnMockPlugin:0.3.5'
} }
} }
@ -144,6 +143,7 @@ dependencies {
testCompile 'com.squareup.assertj:assertj-android:1.1.1' testCompile 'com.squareup.assertj:assertj-android:1.1.1'
testCompile 'com.android.support:support-annotations:23.1.0' testCompile 'com.android.support:support-annotations:23.1.0'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
// cannot use this new version: https://github.com/zxing/zxing/issues/165 // cannot use this new version: https://github.com/zxing/zxing/issues/165
// WARNING: might work on some devices or the emulator - but fails on others // WARNING: might work on some devices or the emulator - but fails on others

View file

@ -16,7 +16,7 @@ public class URLRewriteActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
final String url = URLRewriteController.getUrlByHost(getIntent().getData()); final String url = new URLRewriteController(Tracker.get()).getUrlByUri(getIntent().getData());
if (url == null) { if (url == null) {
new AlertDialog.Builder(this).setTitle("Workaround failed") new AlertDialog.Builder(this).setTitle("Workaround failed")

View file

@ -2,12 +2,20 @@ package org.ligi.passandroid.ui.quirk_fix;
import android.net.Uri; import android.net.Uri;
import org.ligi.passandroid.Tracker; import org.ligi.passandroid.TrackerInterface;
import java.net.URLEncoder; import java.net.URLEncoder;
public class URLRewriteController { public class URLRewriteController {
public static String getUrlByHost(final Uri uri) {
private final TrackerInterface tracker;
public URLRewriteController(TrackerInterface tracker) {
this.tracker = tracker;
}
public String getUrlByUri(final Uri uri) {
if (uri.getHost().endsWith(".virginaustralia.com")) { // mobile. or checkin. if (uri.getHost().endsWith(".virginaustralia.com")) { // mobile. or checkin.
return getVirginAustraliaURL(uri); return getVirginAustraliaURL(uri);
} }
@ -23,14 +31,14 @@ public class URLRewriteController {
return null; return null;
} }
private static String getVirginAustraliaURL(final Uri uri) { private String getVirginAustraliaURL(final Uri uri) {
final String passId; final String passId;
if (uri.toString().contains("CheckInApiIntegration")) { if (uri.toString().contains("CheckInApiIntegration")) {
passId = uri.getQueryParameter("key"); passId = uri.getQueryParameter("key");
Tracker.get().trackEvent("quirk_fix", "redirect_attempt", "virgin_australia2", null); tracker.trackEvent("quirk_fix", "redirect_attempt", "virgin_australia2", null);
} else { } else {
Tracker.get().trackEvent("quirk_fix", "redirect_attempt", "virgin_australia1", null); tracker.trackEvent("quirk_fix", "redirect_attempt", "virgin_australia1", null);
passId = uri.getQueryParameter("c"); passId = uri.getQueryParameter("c");
} }
@ -38,21 +46,21 @@ public class URLRewriteController {
return null; return null;
} }
Tracker.get().trackEvent("quirk_fix", "redirect", "virgin_australia", null); tracker.trackEvent("quirk_fix", "redirect", "virgin_australia", null);
return "https://mobile.virginaustralia.com/boarding/pass.pkpass?key=" + URLEncoder.encode(passId); return "https://mobile.virginaustralia.com/boarding/pass.pkpass?key=" + URLEncoder.encode(passId);
} }
private static String getCathay(Uri uri) { private String getCathay(Uri uri) {
final String passId = uri.getQueryParameter("v"); final String passId = uri.getQueryParameter("v");
Tracker.get().trackEvent("quirk_fix", "redirect_attempt", "cathay", null); tracker.trackEvent("quirk_fix", "redirect_attempt", "cathay", null);
if (passId == null) { if (passId == null) {
return null; return null;
} }
Tracker.get().trackEvent("quirk_fix", "redirect", "cathay", null); tracker.trackEvent("quirk_fix", "redirect", "cathay", null);
return "https://www.cathaypacific.com/icheckin2/PassbookServlet?v=" + URLEncoder.encode(passId); return "https://www.cathaypacific.com/icheckin2/PassbookServlet?v=" + URLEncoder.encode(passId);
} }

View file

@ -5,6 +5,7 @@ import org.json.JSONObject;
import org.junit.Test; import org.junit.Test;
import org.ligi.passandroid.model.AppleStylePassTranslation; import org.ligi.passandroid.model.AppleStylePassTranslation;
import org.ligi.passandroid.model.PassField; import org.ligi.passandroid.model.PassField;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class ThePassField { public class ThePassField {

View file

@ -0,0 +1,61 @@
package org.ligi.passandroid.unittest;
import android.net.Uri;
import org.junit.Test;
import org.ligi.passandroid.TrackerInterface;
import org.ligi.passandroid.ui.quirk_fix.URLRewriteController;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
public class TheURLRewriteController {
private URLRewriteController tested = new URLRewriteController(mock(TrackerInterface.class));
@Test
public void tesRejection() {
final String res = tested.getUrlByUri(Uri.parse("http://foo.bar"));
assertThat(res).isNull();
}
@Test
public void testThatBrusselWorks() {
final String res = tested.getUrlByUri(Uri.parse("http://prod.wap.ncrwebhost.mobi/mobiqa/wap/14foo/83bar/"));
assertThat(res).isEqualTo("http://prod.wap.ncrwebhost.mobi/mobiqa/wap/14foo/83bar/passbook");
}
@Test
public void testThatSwissWorks() {
final String res = tested.getUrlByUri(Uri.parse("http://mbp.swiss.com/mobiqa/wap/14foo/83bar/"));
assertThat(res).isEqualTo("http://prod.wap.ncrwebhost.mobi/mobiqa/wap/14foo/83bar/passbook");
}
@Test
public void testThatCathayWorks() {
final String res = tested.getUrlByUri(Uri.parse("https://www.cathaypacific.com/foo?v=bar"));
assertThat(res).isEqualTo("https://www.cathaypacific.com/icheckin2/PassbookServlet?v=bar");
}
@Test
public void testVirgin1() {
final String res = tested.getUrlByUri(Uri.parse("https://bazz.virginaustralia.com/boarding/CheckInApiIntegration?key=foo"));
assertThat(res).isEqualTo("https://mobile.virginaustralia.com/boarding/pass.pkpass?key=foo");
}
@Test
public void testVirgin() {
final String res = tested.getUrlByUri(Uri.parse("https://bazz.virginaustralia.com/boarding/pass.pkpass?c=foo"));
assertThat(res).isEqualTo("https://mobile.virginaustralia.com/boarding/pass.pkpass?key=foo");
}
}