Skip to content

Commit 4890a7a

Browse files
refactor(example): Add WebView tests (#23)
- **refactor: migrate example to expo-router** - **refactor: migrate example to app.config.ts** - **docs: explain the example pins** - **refactor: add webview tests**
1 parent 05d69cb commit 4890a7a

8 files changed

Lines changed: 818 additions & 69 deletions

File tree

example/app.config.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { ExpoConfig } from "expo/config";
2+
3+
const config: ExpoConfig = {
4+
name: "react-native-app-security-example",
5+
slug: "react-native-app-security-example",
6+
scheme: "rnas-example",
7+
version: "1.0.0",
8+
orientation: "portrait",
9+
icon: "./assets/icon.png",
10+
userInterfaceStyle: "light",
11+
splash: {
12+
image: "./assets/splash.png",
13+
resizeMode: "contain",
14+
backgroundColor: "#ffffff",
15+
},
16+
assetBundlePatterns: ["**/*"],
17+
ios: {
18+
supportsTablet: true,
19+
bundleIdentifier: "tech.bam.rnas.example",
20+
},
21+
android: {
22+
adaptiveIcon: {
23+
foregroundImage: "./assets/adaptive-icon.png",
24+
backgroundColor: "#ffffff",
25+
},
26+
package: "tech.bam.rnas.example",
27+
},
28+
plugins: [
29+
[
30+
"../app.plugin.js",
31+
{
32+
sslPinning: {
33+
"*.yahoo.com": [
34+
// Invalid pins to test failure
35+
"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
36+
"rFjc3wG7lTZe43zeYTvPq8k4xdDEutCmIhI5dn4oCeE=",
37+
],
38+
"google.com": [
39+
// One valid pin to test success
40+
"2MXZa6jBZjmb6FYPT3yf4oZFB67aQGmsX4DQgddQ7XA=",
41+
"ylrexmVB/d9PHCARU9i0R9km/ahwuNpWaWXbpLyR7jQ=",
42+
],
43+
},
44+
preventRecentScreenshots: {
45+
ios: {
46+
enabled: true,
47+
},
48+
android: {
49+
enabled: true,
50+
},
51+
},
52+
},
53+
],
54+
"expo-router",
55+
],
56+
};
57+
58+
export default config;

example/app.json

Lines changed: 0 additions & 50 deletions
This file was deleted.

example/app/_layout.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Stack } from "expo-router";
2+
3+
export default function Layout() {
4+
return <Stack />;
5+
}

example/App.tsx renamed to example/app/index.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { SafeKeyboardDetector } from "@bam.tech/react-native-app-security";
2+
import { useRouter } from "expo-router";
23
import { useState } from "react";
34
import { Button, Modal, Platform, StyleSheet, View } from "react-native";
45

56
export default function App() {
67
const [isModalVisible, setIsModalVisible] = useState(false);
8+
const router = useRouter();
79

810
return (
911
<View style={styles.container}>
@@ -23,6 +25,14 @@ export default function App() {
2325
title="fetch - invalid certificates - subdomain"
2426
onPress={fetchInvalidSubdomain}
2527
/>
28+
<Button
29+
title="WebView - valid pin (Google)"
30+
onPress={() => router.push("/webview-pin-ok")}
31+
/>
32+
<Button
33+
title="WebView - invalid pin (Yahoo)"
34+
onPress={() => router.push("/webview-pin-ko")}
35+
/>
2636
<Button title="Is current keyboard safe?" onPress={checkIsKeyboardSafe} />
2737
{Platform.OS === "android" ? (
2838
<Button
@@ -61,15 +71,17 @@ const fetchUnpinned = async () => {
6171
}
6272
};
6373

64-
6574
const fetchValid = async () => {
6675
try {
6776
const response = await fetch("https://google.com");
6877
console.warn("✅ valid certificate and fetch succeeded", {
6978
status: response.status,
7079
});
7180
} catch (error) {
72-
console.warn("❌ valid certificate but fetch failed - public keys expire, make sure they are up to date", error);
81+
console.warn(
82+
"❌ valid certificate but fetch failed - public keys expire, make sure they are up to date",
83+
error
84+
);
7385
}
7486
};
7587

example/app/webview-pin-ko.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useRef } from "react";
2+
import { StyleSheet } from "react-native";
3+
import WebView from "react-native-webview";
4+
5+
export default function WebViewPinOk() {
6+
const webViewRef = useRef<WebView>(null);
7+
8+
return (
9+
<WebView
10+
ref={webViewRef}
11+
source={{ uri: "https://yahoo.com" }}
12+
style={styles.webview}
13+
onError={({ nativeEvent }) => {
14+
console.warn("✅ Invalid pin and load failed", nativeEvent.description);
15+
}}
16+
/>
17+
);
18+
}
19+
20+
const styles = StyleSheet.create({
21+
webview: {
22+
flex: 1,
23+
},
24+
});

example/app/webview-pin-ok.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useRef } from "react";
2+
import { StyleSheet } from "react-native";
3+
import WebView from "react-native-webview";
4+
5+
export default function WebViewPinOk() {
6+
const webViewRef = useRef<WebView>(null);
7+
8+
return (
9+
<WebView
10+
ref={webViewRef}
11+
source={{ uri: "https://google.com" }}
12+
style={styles.webview}
13+
onError={({ nativeEvent }) => {
14+
console.warn("❌ Valid pin and load failed", nativeEvent.description);
15+
}}
16+
/>
17+
);
18+
}
19+
20+
const styles = StyleSheet.create({
21+
webview: {
22+
flex: 1,
23+
},
24+
});

example/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
{
22
"name": "react-native-app-security-example",
33
"version": "1.0.0",
4-
"main": "node_modules/expo/AppEntry.js",
4+
"main": "expo-router/entry",
55
"scripts": {
66
"start": "expo start --dev-client",
77
"android": "expo run:android",
88
"ios": "expo run:ios"
99
},
1010
"dependencies": {
1111
"expo": "^51.0.0",
12+
"expo-constants": "~16.0.2",
13+
"expo-linking": "~6.3.1",
14+
"expo-router": "~3.5.24",
1215
"expo-splash-screen": "~0.27.7",
1316
"expo-status-bar": "~1.12.1",
1417
"react": "18.2.0",
15-
"react-native": "0.74.5"
18+
"react-native": "0.74.5",
19+
"react-native-safe-area-context": "4.10.5",
20+
"react-native-screens": "3.31.1",
21+
"react-native-webview": "13.8.6"
1622
},
1723
"devDependencies": {
1824
"@babel/core": "^7.24.0",

0 commit comments

Comments
 (0)