添加前端支持

This commit is contained in:
2026-03-13 15:03:36 +08:00
parent c2b9c5d4c0
commit 8612b2dbde
12 changed files with 897 additions and 43 deletions

View File

@@ -28,30 +28,30 @@ void main() {
});
group('Root endpoint', () {
test('GET / returns Hello World', () async {
final response = await http.get(Uri.parse('$host/'));
test('GET /api/v1/ returns Hello World', () async {
final response = await http.get(Uri.parse('$host/api/v1/'));
expect(response.statusCode, 200);
expect(response.body, 'Hello, World!\n');
});
});
group('Echo endpoint', () {
test('GET /echo/<message> returns the message', () async {
final response = await http.get(Uri.parse('$host/echo/hello'));
test('GET /api/v1/echo/<message> returns the message', () async {
final response = await http.get(Uri.parse('$host/api/v1/echo/hello'));
expect(response.statusCode, 200);
expect(response.body, 'hello\n');
});
test('GET /echo/<message> with special characters', () async {
final response = await http.get(Uri.parse('$host/echo/test%20message'));
test('GET /api/v1/echo/<message> with special characters', () async {
final response = await http.get(Uri.parse('$host/api/v1/echo/test%20message'));
expect(response.statusCode, 200);
expect(response.body, contains('test'));
});
});
group('Album endpoints', () {
test('GET /album returns list of albums', () async {
final response = await http.get(Uri.parse('$host/album'));
test('GET /api/v1/album returns list of albums', () async {
final response = await http.get(Uri.parse('$host/api/v1/album'));
expect(response.statusCode, 200);
expect(response.headers['content-type'], contains('application/json'));
final albums = jsonDecode(response.body) as List;
@@ -62,27 +62,27 @@ void main() {
}
});
test('GET /album/<id> with invalid id returns 400', () async {
final response = await http.get(Uri.parse('$host/album/invalid_id'));
test('GET /api/v1/album/<id> with invalid id returns 400', () async {
final response = await http.get(Uri.parse('$host/api/v1/album/invalid_id'));
expect(response.statusCode, 400);
expect(response.body, contains('Invalid album id'));
});
test('GET /album/<id> with non-existent id returns 404', () async {
final response = await http.get(Uri.parse('$host/album/999999'));
test('GET /api/v1/album/<id> with non-existent id returns 404', () async {
final response = await http.get(Uri.parse('$host/api/v1/album/999999'));
expect(response.statusCode, 404);
expect(response.body, contains('Album not found'));
});
test('GET /album/<id>/photo with invalid id returns 400', () async {
final response = await http.get(Uri.parse('$host/album/invalid_id/photo'));
test('GET /api/v1/album/<id>/photo with invalid id returns 400', () async {
final response = await http.get(Uri.parse('$host/api/v1/album/invalid_id/photo'));
expect(response.statusCode, 400);
expect(response.body, contains('Invalid album id'));
});
test('GET /album/<id>/photo with valid id returns photos', () async {
test('GET /api/v1/album/<id>/photo with valid id returns photos', () async {
// First get the list to find a valid album id
final listResponse = await http.get(Uri.parse('$host/album'));
final listResponse = await http.get(Uri.parse('$host/api/v1/album'));
expect(listResponse.statusCode, 200);
final albums = jsonDecode(listResponse.body) as List;
@@ -94,7 +94,7 @@ void main() {
final album = albums.first as Map<String, dynamic>;
final albumId = album['id'];
final response = await http.get(Uri.parse('$host/album/$albumId/photo'));
final response = await http.get(Uri.parse('$host/api/v1/album/$albumId/photo'));
expect(response.statusCode, 200);
expect(response.headers['content-type'], contains('application/json'));
final photos = jsonDecode(response.body) as List;
@@ -103,35 +103,35 @@ void main() {
});
group('Photo endpoints', () {
test('GET /photo/<id> with invalid id returns 400', () async {
final response = await http.get(Uri.parse('$host/photo/abc'));
test('GET /api/v1/photo/<id> with invalid id returns 400', () async {
final response = await http.get(Uri.parse('$host/api/v1/photo/abc'));
expect(response.statusCode, 400);
expect(response.body, contains('Invalid photo id'));
});
test('GET /photo/<id> with non-existent id returns 404', () async {
test('GET /api/v1/photo/<id> with non-existent id returns 404', () async {
// Use a hash that won't match any file
final response = await http.get(Uri.parse('$host/photo/12345'));
final response = await http.get(Uri.parse('$host/api/v1/photo/12345'));
expect(response.statusCode, 404);
expect(response.body, contains('Photo not found'));
});
test('GET /photo/<id>/file with invalid id returns 400', () async {
final response = await http.get(Uri.parse('$host/photo/abc/file'));
test('GET /api/v1/photo/<id>/file with invalid id returns 400', () async {
final response = await http.get(Uri.parse('$host/api/v1/photo/abc/file'));
expect(response.statusCode, 400);
expect(response.body, contains('Invalid photo id'));
});
test('GET /photo/<id>/file with non-existent id returns 404', () async {
test('GET /api/v1/photo/<id>/file with non-existent id returns 404', () async {
// Use a hash that won't match any file
final response = await http.get(Uri.parse('$host/photo/12345/file'));
final response = await http.get(Uri.parse('$host/api/v1/photo/12345/file'));
expect(response.statusCode, 404);
expect(response.body, contains('Photo not found'));
});
test('GET /photo/<id>/file with valid id returns file', () async {
test('GET /api/v1/photo/<id>/file with valid id returns file', () async {
// First get a valid photo id from an album
final albumResponse = await http.get(Uri.parse('$host/album'));
final albumResponse = await http.get(Uri.parse('$host/api/v1/album'));
expect(albumResponse.statusCode, 200);
final albums = jsonDecode(albumResponse.body) as List;
@@ -144,7 +144,7 @@ void main() {
final albumId = album['id'];
final photosResponse = await http.get(
Uri.parse('$host/album/$albumId/photo'),
Uri.parse('$host/api/v1/album/$albumId/photo'),
);
expect(photosResponse.statusCode, 200);
final photos = jsonDecode(photosResponse.body) as List;
@@ -158,7 +158,7 @@ void main() {
final photoId = photo['id'];
final response = await http.get(
Uri.parse('$host/photo/$photoId/file'),
Uri.parse('$host/api/v1/photo/$photoId/file'),
);
expect(response.statusCode, 200);
expect(response.contentLength, greaterThan(0));
@@ -172,7 +172,7 @@ void main() {
});
test('Unknown nested route returns 404', () async {
final response = await http.get(Uri.parse('$host/api/v1/unknown'));
final response = await http.get(Uri.parse('$host/api/v2/unknown'));
expect(response.statusCode, 404);
});
});