Skip to content

Timeout HTTP requests #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions lib/providers/api_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ class ApiProvider {
}) async {
dynamic responseJson;
try {
final response = await httpClient.get(
url,
headers: headers,
);
final response = await httpClient
.get(
url,
headers: headers,
)
.timeout(const Duration(seconds: 10));

responseJson = _response(response);
} on SocketException {
throw FetchDataException('No Internet connection');
} on TimeoutException {
throw FetchDataException('Request timed out');
}
return responseJson;
}
Expand Down Expand Up @@ -58,14 +63,21 @@ class ApiProvider {
) async {
dynamic responseJson;
try {
final response = await httpClient.post(
url,
headers: headers,
body: jsonEncode(body),
);
final response = await httpClient
.post(
url,
headers: headers,
body: jsonEncode(body),
)
.timeout(const Duration(
seconds: 10,
));

responseJson = _response(response);
} on SocketException {
throw FetchDataException('No Internet connection');
} on TimeoutException {
throw FetchDataException('Request timed out');
}
return responseJson;
}
Expand Down
44 changes: 44 additions & 0 deletions test/providers/api_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ void main() {
);
});

test('Test makeHttpGet timeout failure', () async {
final url = Uri.parse('https://example.com/api/timeout_endpoint');

when(
mockClient.get(
url,
headers: anyNamed('headers'),
),
).thenAnswer(
(_) => Future.delayed(
const Duration(seconds: 11),
() => http.Response('{}', 200),
),
);

expect(
() async => await apiProvider.makeHttpGet(url),
throwsA(isA<FetchDataException>()),
);
});

test('Test makeHttpPost success', () async {
final url = Uri.parse('https://example.com/api/endpoint');
final requestBody = {'key': 'value'};
Expand Down Expand Up @@ -111,6 +132,29 @@ void main() {
),
);
});

test('Test makeHttpPost timeout failure', () async {
final url = Uri.parse('https://example.com/api/endpoint');
final requestBody = {'key': 'value'};

when(
mockClient.post(
url,
headers: anyNamed('headers'),
body: jsonEncode(requestBody),
),
).thenAnswer(
(_) => Future.delayed(
const Duration(seconds: 11), // Simulate delay to trigger timeout
() => http.Response('{}', 200),
),
);

expect(
() async => await apiProvider.makeHttpPost(url, null, requestBody),
throwsA(isA<FetchDataException>()),
);
});
});

test('Test makeHttpPost 400 failure', () {
Expand Down