Skip to content

Commit

Permalink
Merge pull request #1582 from nextcloud/refactor/neon_framework/dedup…
Browse files Browse the repository at this point in the history
…licate-mock-server-implementations
  • Loading branch information
provokateurin authored Feb 6, 2024
2 parents 8c48281 + dacc15b commit 7850338
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
28 changes: 28 additions & 0 deletions packages/neon_framework/lib/src/testing/mock_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:neon_framework/src/models/account.dart';

/// Creates an [Account] connected to a fake server defined by [requests].
///
/// To be used for end-to-end testing `Bloc`s.
Account mockServer(
Map<RegExp, Map<String, Response Function(RegExpMatch match, Map<String, String> queryParameters)>> requests,
) =>
Account(
serverURL: Uri.parse('https://example.com'),
username: 'test',
password: 'test',
httpClient: MockClient((request) async {
for (final entry in requests.entries) {
final match = entry.key.firstMatch(request.url.path);
if (match != null) {
final call = entry.value[request.method];
if (call != null) {
return call(match, request.url.queryParameters);
}
}
}

throw Exception(request);
}),
);
1 change: 1 addition & 0 deletions packages/neon_framework/lib/testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ library;

import 'package:flutter/material.dart';

export 'package:neon_framework/src/testing/mock_server.dart';
export 'package:neon_framework/src/testing/mocks.dart';
export 'package:neon_framework/src/testing/utils.dart';
24 changes: 2 additions & 22 deletions packages/neon_framework/test/user_status_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:mocktail/mocktail.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/platform.dart';
Expand Down Expand Up @@ -69,7 +68,7 @@ Account mockUserStatusAccount() {
200,
);

final requests = <RegExp, Map<String, Response Function(RegExpMatch match, Map<String, String> queryParameters)>>{
return mockServer({
RegExp(r'/ocs/v2\.php/apps/user_status/api/v1/predefined_statuses'): {
'get': (match, queryParameters) => predefinedStatusesResponse(),
},
Expand Down Expand Up @@ -125,26 +124,7 @@ Account mockUserStatusAccount() {
return statusResponse();
},
},
};

return Account(
serverURL: Uri.parse('https://example.com'),
username: 'test',
password: 'test',
httpClient: MockClient((request) async {
for (final entry in requests.entries) {
final match = entry.key.firstMatch(request.url.path);
if (match != null) {
final call = entry.value[request.method];
if (call != null) {
return call(match, request.url.queryParameters);
}
}
}

throw Exception(request);
}),
);
});
}

void main() {
Expand Down
24 changes: 2 additions & 22 deletions packages/neon_framework/test/weather_status_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:mocktail/mocktail.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/src/models/account.dart';
Expand Down Expand Up @@ -31,7 +30,7 @@ Account mockWeatherStatusAccount() {
200,
);

final requests = <RegExp, Map<String, Response Function(RegExpMatch match, Map<String, String> queryParameters)>>{
return mockServer({
RegExp(r'/ocs/v2\.php/apps/weather_status/api/v1/location'): {
'get': (match, queryParameters) => locationResponse(),
'put': (match, queryParameters) {
Expand Down Expand Up @@ -92,26 +91,7 @@ Account mockWeatherStatusAccount() {
200,
),
},
};

return Account(
serverURL: Uri.parse('https://example.com'),
username: 'test',
password: 'test',
httpClient: MockClient((request) async {
for (final entry in requests.entries) {
final match = entry.key.firstMatch(request.url.path);
if (match != null) {
final call = entry.value[request.method];
if (call != null) {
return call(match, request.url.queryParameters);
}
}
}

throw Exception(request);
}),
);
});
}

core.OcsGetCapabilitiesResponseApplicationJson_Ocs_Data buildCapabilities({required bool enabled}) =>
Expand Down

0 comments on commit 7850338

Please sign in to comment.