Skip to content

Commit

Permalink
refactor: simplificado o método delete para utilizar a função `_exe…
Browse files Browse the repository at this point in the history
…cute`, removendo duplicação de código
  • Loading branch information
shonorio committed Jan 14, 2025
1 parent 632a6e1 commit bb341fc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
15 changes: 8 additions & 7 deletions lib/app/core/network/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,19 @@ class ApiProvider implements IApiProvider {

@override
Future<String> delete({
String? path,
required String path,
Map<String, String?> parameters = const {},
}) async {
final Uri uriRequest = _setupHttpRequest(
final streamedResponse = await _execute(
method: ApiHttpRequestMethod.delete,
path: path,
queryParameters: parameters,
body: '',
headers: const {},
parameters: parameters,
);
final header = await _setupHttpHeader({});
final response = await _httpClient.delete(uriRequest, headers: header);
final parsed = await _parseResponse(response);

return parsed.body;
final response = await _parseStreamedResponse(streamedResponse);
return response.body;
}

@override
Expand Down
15 changes: 10 additions & 5 deletions test/app/core/network/api_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,10 @@ void main() {
headers: headers,
parameters: parameters,
);
final captured = verify(() => apiClient.send(captureAny())).captured;
// assert
expect(actual, expected);
expect(captured.first.method, 'POST');
});
});

Expand All @@ -319,11 +321,12 @@ void main() {
const String path = 'some_path';
final parameters = <String, String>{'param1': 'value1'};
const expected = '{}';
when(() => networkInfo.isConnected).thenAnswer((_) async => false);
when(() => apiClient.delete(
any(),
headers: any(named: 'headers'),
)).thenAnswer((_) async => http.Response(expected, 200));
when(() => apiClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(
http.ByteStream.fromBytes(utf8.encode(expected)),
HttpStatus.ok,
),
);

final sut = ApiProvider(
serverConfiguration: serverConfiguration,
Expand All @@ -337,6 +340,8 @@ void main() {
);
// assert
expect(actual, expected);
final captured = verify(() => apiClient.send(captureAny())).captured;
expect(captured.first.method, 'DELETE');
});
});

Expand Down

0 comments on commit bb341fc

Please sign in to comment.