Skip to content
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

fix(#89): add check to the statusCode setter in ResponseProperties - fixes #89 #90

Merged
merged 2 commits into from
Sep 17, 2024
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
4 changes: 4 additions & 0 deletions .website/foundations/request_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ The `RequestContext` class has the following properties:
| **path** | The path of the request that triggered the handler |
| **res** | Utility class to interact with the response headers and statusCode. |

::: warning
The statusCode value must be between 100 and 999.
:::

## Methods

The `RequestContext` class has the following methods:
Expand Down
13 changes: 12 additions & 1 deletion packages/serinus/lib/src/contexts/request_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ final class Redirect {
/// It contains the status code, headers, and redirect properties.
final class ResponseProperties {
/// The [statusCode] property contains the status code of the response.
int statusCode = HttpStatus.ok;
int _statusCode = HttpStatus.ok;

/// The [statusCode] getter is used to get the status code of the response.
int get statusCode => _statusCode;

/// The [statusCode] setter is used to set the status code of the response.
set statusCode(int value) {
if (value < 100 || value > 999) {
throw ArgumentError('The status code must be between 100 and 999. $value is not a valid status code.');
}
_statusCode = value;
}

/// The [contentType] property contains the content type of the response.
ContentType? contentType;
Expand Down
22 changes: 22 additions & 0 deletions packages/serinus/test/commons/response_properties_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:serinus/serinus.dart';
import 'package:test/test.dart';

void main() async {
group('$ResponseProperties', () {
test(
'should throw an error when the status code is not a valid status code',
() {
final ResponseProperties res = ResponseProperties();
expect(() => res.statusCode = 1000, throwsArgumentError);
expect(() => res.statusCode = 99, throwsArgumentError);
});

test(
'should set the status code when the status code is a valid status code',
() {
final ResponseProperties res = ResponseProperties();
res.statusCode = 200;
expect(res.statusCode, equals(200));
});
});
}
Loading