mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-29 20:54:06 +01:00
Compare commits
No commits in common. "ded7486244f8c6c31587b0e80f05af40947dd015" and "957ddc7ddcd6d961b1a45026c0ebf7568793cd48" have entirely different histories.
ded7486244
...
957ddc7ddc
@ -1,35 +0,0 @@
|
|||||||
/// A wordless page for an error the window itself loaded: Chrome would
|
|
||||||
/// otherwise paint its own "This page isn't working" sentences over an
|
|
||||||
/// empty error body. Only the title carries the app's name (OS-level label).
|
|
||||||
library;
|
|
||||||
|
|
||||||
import 'dart:io';
|
|
||||||
|
|
||||||
/// HTML page with only an icon — no words painted by the browser.
|
|
||||||
const String errorIconPage =
|
|
||||||
'<!doctype html><html><head><meta charset="utf-8"> '
|
|
||||||
'<title>Diet Guard</title> '
|
|
||||||
'<style>html,body{margin:0;height:100%; '
|
|
||||||
'background:#211D1B;display:flex;align-items:center; '
|
|
||||||
'justify-content:center}</style></head><body> '
|
|
||||||
'<svg width="96" height="96" viewBox="0 0 24 24" fill="none" '
|
|
||||||
'stroke="#A39892" stroke-width="2" stroke-linecap="round" '
|
|
||||||
'aria-hidden="true"><circle cx="12" cy="12" r="9"/> '
|
|
||||||
'<line x1="5.6" y1="18.4" x2="18.4" y2="5.6"/></svg></body></html>';
|
|
||||||
|
|
||||||
/// Whether [request] is the window loading a page, not the app's fetch().
|
|
||||||
bool isNavigation(HttpRequest request) =>
|
|
||||||
request.headers.value('sec-fetch-mode') == 'navigate';
|
|
||||||
|
|
||||||
/// Replaces an error answer to a page load with [errorIconPage]. Does
|
|
||||||
/// nothing for fetch() calls, successes, or a response already written.
|
|
||||||
void paintErrorIcon(HttpRequest request) {
|
|
||||||
final response = request.response;
|
|
||||||
if (!isNavigation(request) || response.statusCode < 400) return;
|
|
||||||
try {
|
|
||||||
response.headers.contentType = ContentType.html;
|
|
||||||
response.write(errorIconPage);
|
|
||||||
} on Exception {
|
|
||||||
return; // body already sent: leave it alone
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -19,7 +19,6 @@ library;
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:diet_guard_app/desktop/error_page.dart' show isNavigation;
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
/// GitHub's REST API base, which [GitHubProxy] fronts.
|
/// GitHub's REST API base, which [GitHubProxy] fronts.
|
||||||
@ -178,13 +177,8 @@ class GitHubProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _passThrough(HttpRequest request, http.Response response) {
|
void _passThrough(HttpRequest request, http.Response response) {
|
||||||
request.response.statusCode = response.statusCode;
|
|
||||||
if (isNavigation(request)) {
|
|
||||||
// Top-level navigation (window load) must not paint error JSON; the app's
|
|
||||||
// own fetch() calls never send 'navigate', so they still get the body.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
request.response
|
request.response
|
||||||
|
..statusCode = response.statusCode
|
||||||
..headers.contentType = ContentType.json
|
..headers.contentType = ContentType.json
|
||||||
..add(response.bodyBytes);
|
..add(response.bodyBytes);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,7 +24,6 @@ import 'dart:io';
|
|||||||
import 'package:crdt_sync/crdt_sync.dart';
|
import 'package:crdt_sync/crdt_sync.dart';
|
||||||
|
|
||||||
import 'package:diet_guard_app/desktop/content_types.dart' as content_types;
|
import 'package:diet_guard_app/desktop/content_types.dart' as content_types;
|
||||||
import 'package:diet_guard_app/desktop/error_page.dart';
|
|
||||||
import 'package:diet_guard_app/desktop/github_proxy.dart';
|
import 'package:diet_guard_app/desktop/github_proxy.dart';
|
||||||
import 'package:diet_guard_app/services/desktop_wrapper.dart';
|
import 'package:diet_guard_app/services/desktop_wrapper.dart';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
@ -92,7 +91,6 @@ class WrapperServer {
|
|||||||
} on Exception {
|
} on Exception {
|
||||||
request.response.statusCode = HttpStatus.internalServerError;
|
request.response.statusCode = HttpStatus.internalServerError;
|
||||||
}
|
}
|
||||||
paintErrorIcon(request);
|
|
||||||
await request.response.close();
|
await request.response.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,20 +24,6 @@ class LocalNotificationsBackend implements NotificationBackend {
|
|||||||
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
|
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
|
||||||
);
|
);
|
||||||
await plugin.initialize(settings: settings);
|
await plugin.initialize(settings: settings);
|
||||||
|
|
||||||
// Re-create the channel so Android renames it if an install still shows
|
|
||||||
// the old "Meal reminders" label.
|
|
||||||
await plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
|
||||||
>()
|
|
||||||
?.createNotificationChannel(
|
|
||||||
const AndroidNotificationChannel(
|
|
||||||
_channelId,
|
|
||||||
_channelName,
|
|
||||||
importance: Importance.high,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Requests Android 13+'s runtime `POST_NOTIFICATIONS` permission.
|
/// Requests Android 13+'s runtime `POST_NOTIFICATIONS` permission.
|
||||||
|
|||||||
@ -2,7 +2,7 @@ name: diet_guard_app
|
|||||||
description: "Companion phone app for diet_guard: log meals on the go."
|
description: "Companion phone app for diet_guard: log meals on the go."
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
|
|
||||||
version: 1.0.154+1
|
version: 1.0.150+1
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.12.2
|
sdk: ^3.12.2
|
||||||
|
|||||||
@ -13,16 +13,6 @@ import 'package:http/http.dart' as http;
|
|||||||
import 'package:http/testing.dart';
|
import 'package:http/testing.dart';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
|
|
||||||
/// Removes <title> and <style> tags, strips all remaining HTML tags, and
|
|
||||||
/// returns every [A-Za-z]{2,} match — i.e. all visible words the browser would
|
|
||||||
/// paint for a top-level navigation.
|
|
||||||
List<String> paintedWords(String html) {
|
|
||||||
var noTitle = html.replaceAll(RegExp(r'<title>.*?</title>', dotAll: true), '');
|
|
||||||
var noStyle = noTitle.replaceAll(RegExp(r'<style>.*?</style>', dotAll: true), '');
|
|
||||||
var noTags = noStyle.replaceAll(RegExp(r'<[^>]*>'), '');
|
|
||||||
return RegExp(r'[A-Za-z]{2,}').allMatches(noTags).map((m) => m.group(0)!).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
late Directory tempDir;
|
late Directory tempDir;
|
||||||
late WrapperServer server;
|
late WrapperServer server;
|
||||||
@ -79,75 +69,5 @@ void main() {
|
|||||||
expect(response.statusCode, status);
|
expect(response.statusCode, status);
|
||||||
expect(response.body, isEmpty, reason: 'nothing painted in the window');
|
expect(response.body, isEmpty, reason: 'nothing painted in the window');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('$method $path loaded as a page shows only an icon', () async {
|
|
||||||
final request = http.Request(method, url(path))
|
|
||||||
..headers['sec-fetch-mode'] = 'navigate';
|
|
||||||
final response = await http.Response.fromStream(
|
|
||||||
await http.Client().send(request),
|
|
||||||
);
|
|
||||||
expect(response.statusCode, status);
|
|
||||||
expect(response.headers['content-type'], contains('text/html'));
|
|
||||||
expect(response.body, contains('<svg'));
|
|
||||||
expect(paintedWords(response.body), isEmpty);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
group('proxy error responses', () {
|
|
||||||
late Directory tempDir;
|
|
||||||
late WrapperServer server;
|
|
||||||
|
|
||||||
setUp(() async {
|
|
||||||
tempDir = await Directory.systemTemp.createTemp('diet_guard_proxy_err_');
|
|
||||||
final webRoot = p.join(tempDir.path, 'web');
|
|
||||||
final dataDir = p.join(tempDir.path, 'data');
|
|
||||||
await Directory(webRoot).create(recursive: true);
|
|
||||||
File(p.join(webRoot, 'index.html')).writeAsStringSync('<html></html>');
|
|
||||||
await Directory(dataDir).create(recursive: true);
|
|
||||||
File(p.join(dataDir, 'documents')).writeAsStringSync('');
|
|
||||||
final tokenPath = p.join(tempDir.path, 'sync_token');
|
|
||||||
await File(tokenPath).writeAsString('t');
|
|
||||||
server = WrapperServer(
|
|
||||||
webRoot: webRoot,
|
|
||||||
dataDir: dataDir,
|
|
||||||
gitHubProxy: GitHubProxy(
|
|
||||||
tokenPath: tokenPath,
|
|
||||||
fallbackTokenPath: p.join(tempDir.path, 'fallback_token'),
|
|
||||||
httpClient: MockClient((_) async =>
|
|
||||||
http.Response('{"message": "Bad credentials"}', 401)),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
await server.start(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
tearDown(() async {
|
|
||||||
await server.stop();
|
|
||||||
await tempDir.delete(recursive: true);
|
|
||||||
});
|
|
||||||
|
|
||||||
Uri url(String path) => Uri.parse('http://localhost:${server.port}$path');
|
|
||||||
|
|
||||||
test('a proxy error loaded as a page shows only an icon', () async {
|
|
||||||
final request = http.Request('GET', url('/github/api/repos/o/r/contents/x'))
|
|
||||||
..headers['sec-fetch-mode'] = 'navigate';
|
|
||||||
final response = await http.Response.fromStream(
|
|
||||||
await http.Client().send(request),
|
|
||||||
);
|
|
||||||
expect(response.statusCode, HttpStatus.unauthorized);
|
|
||||||
expect(response.headers['content-type'], contains('text/html'));
|
|
||||||
expect(response.body, contains('<svg'));
|
|
||||||
expect(paintedWords(response.body), isEmpty);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('the app fetch still gets the proxy error body', () async {
|
|
||||||
final response = await http.Response.fromStream(
|
|
||||||
await http.Client().send(http.Request(
|
|
||||||
'GET',
|
|
||||||
url('/github/api/repos/o/r/contents/x'),
|
|
||||||
)),
|
|
||||||
);
|
|
||||||
expect(response.statusCode, HttpStatus.unauthorized);
|
|
||||||
expect(response.body, contains('Bad credentials'));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,23 +34,6 @@ void main() {
|
|||||||
expect(log.where((c) => c.method == 'initialize'), hasLength(1));
|
expect(log.where((c) => c.method == 'initialize'), hasLength(1));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('init renames the reminder channel to the app name', () async {
|
|
||||||
final log = installFakeAndroidNotifications();
|
|
||||||
NotificationService.resetForTesting(
|
|
||||||
backend: LocalNotificationsBackend(FlutterLocalNotificationsPlugin()),
|
|
||||||
);
|
|
||||||
|
|
||||||
await NotificationService.init();
|
|
||||||
|
|
||||||
final createCall = log.firstWhere(
|
|
||||||
(c) => c.method == 'createNotificationChannel',
|
|
||||||
orElse: () => throw StateError('no createNotificationChannel call logged'),
|
|
||||||
);
|
|
||||||
final args = createCall.arguments as Map;
|
|
||||||
expect(args['id'], reminderChannelId);
|
|
||||||
expect(args['name'], 'DietGuard');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('requestPermission delegates to the Android implementation', () async {
|
test('requestPermission delegates to the Android implementation', () async {
|
||||||
installFakeAndroidNotifications();
|
installFakeAndroidNotifications();
|
||||||
NotificationService.resetForTesting(
|
NotificationService.resetForTesting(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user