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

Added request uri and method to the request RestResponse object #8

Merged
merged 1 commit into from
Nov 23, 2023
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.0

- Added request uri and method to the request `RestResponse` object.

## 1.0.5

- Update `dart-sdk` to 3.2.1 version.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/exceptions/rest_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class RestException implements Exception {
/// RestException toString method.
@override
String toString() {
return 'RestException{errorMessage: $errorMessage, method: $method, uri: $uri, headers: $headers, requestBody: $requestBody, responseStatusCode: $responseStatusCode, responseBody: $responseBody, stackTrace: $stackTrace}';
return 'RestException(errorMessage: $errorMessage, method: ${method.value}, uri: $uri, headers: $headers, requestBody: $requestBody, responseStatusCode: $responseStatusCode, responseBody: $responseBody, stackTrace: $stackTrace)';
}

/// RestException equals operator method.
Expand Down
25 changes: 21 additions & 4 deletions lib/src/models/rest_response_model.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import 'rest_status_enum.dart';
// ignore_for_file: public_member_api_docs, sort_constructors_first
import '../../iw_rest_client.dart';

/// This class represents the response of a request by the encapsulated rest client.
class RestResponse {
/// Contains the request method.
final RestMethod requestMethod;

/// Contains the request uri.
final RestUri requestUri;

/// Contains the response body of the request.
final String body;

Expand All @@ -10,25 +17,35 @@ class RestResponse {

/// Constructor
const RestResponse({
required this.requestMethod,
required this.requestUri,
required this.body,
required this.statusCode,
});

/// RestResponse hashCode getter.
@override
int get hashCode => body.hashCode ^ statusCode.hashCode;
int get hashCode {
return requestMethod.hashCode ^
requestUri.hashCode ^
body.hashCode ^
statusCode.hashCode;
}

/// RestResponse toString method.
@override
String toString() {
return 'RestResponse{body: $body, statusCode: $statusCode}';
return 'RestResponse(requestMethod: ${requestMethod.value}, requestUri: $requestUri, body: $body, statusCode: $statusCode)';
}

/// RestResponse equals operator method.
@override
bool operator ==(covariant RestResponse other) {
if (identical(this, other)) return true;

return other.body == body && other.statusCode == statusCode;
return other.requestMethod == requestMethod &&
other.requestUri == requestUri &&
other.body == body &&
other.statusCode == statusCode;
}
}
2 changes: 1 addition & 1 deletion lib/src/models/rest_uri_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RestUri {
/// RestUri toString method.
@override
String toString() {
return 'RestUri{scheme: $scheme, host: $host, path: $path, queryParameters: $queryParameters}';
return 'RestUri(scheme: ${scheme.value}, host: $host, path: $path, queryParameters: $queryParameters)';
}

/// RestUri equals operator method.
Expand Down
2 changes: 2 additions & 0 deletions lib/src/services/http_client_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ final class HttpClientService implements RestClientService {
responseStatusCode = RestStatus.fromValue(httpResponse.statusCode);

response = RestResponse(
requestMethod: method,
requestUri: uri,
body: httpResponse.body,
statusCode: responseStatusCode!,
);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: iw_rest_client
description: This project is an implementation that allow developers to encapsulate their own usage of a http client.
version: 1.0.5
version: 1.1.0
repository: https://github.com/euivanw/iw_rest_client

topics:
Expand Down
30 changes: 16 additions & 14 deletions test/lib/src/exceptions/rest_exception_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void main() {
group('RestException', () {
final String mockErrorMessage = 'This is an exception';

final RestMethod mockVerb = RestMethod.get;
final RestMethod mockRequestMethod = RestMethod.get;

final Map<String, String> mockHeaders = {
'Content-Type': 'application/json',
Expand All @@ -24,6 +24,8 @@ void main() {
};

final RestResponse mockResponseBody = RestResponse(
requestUri: mockRestUri,
requestMethod: mockRequestMethod,
body: 'This is the body.',
statusCode: RestStatus.ok,
);
Expand All @@ -39,7 +41,7 @@ void main() {
// Act
final RestException exception = RestException(
errorMessage: mockErrorMessage,
method: mockVerb,
method: mockRequestMethod,
uri: mockRestUri,
);

Expand All @@ -48,7 +50,7 @@ void main() {
expect(exception.errorMessage, isNotEmpty);
expect(exception.errorMessage, equals(mockErrorMessage));
expect(exception.method, isNotNull);
expect(exception.method, equals(mockVerb));
expect(exception.method, equals(mockRequestMethod));
expect(exception.uri, isNotNull);
expect(exception.headers, isNull);
expect(exception.requestBody, isNull);
Expand All @@ -64,7 +66,7 @@ void main() {
// Act
final RestException exception = RestException(
errorMessage: mockErrorMessage,
method: mockVerb,
method: mockRequestMethod,
headers: mockHeaders,
uri: mockRestUri,
);
Expand All @@ -74,7 +76,7 @@ void main() {
expect(exception.errorMessage, isNotEmpty);
expect(exception.errorMessage, equals(mockErrorMessage));
expect(exception.method, isNotNull);
expect(exception.method, equals(mockVerb));
expect(exception.method, equals(mockRequestMethod));
expect(exception.headers, isNotNull);
expect(exception.headers, isNotEmpty);
expect(exception.headers, equals(mockHeaders));
Expand All @@ -93,7 +95,7 @@ void main() {
// Act
final RestException exception = RestException(
errorMessage: mockErrorMessage,
method: mockVerb,
method: mockRequestMethod,
uri: mockRestUri,
headers: mockHeaders,
requestBody: mockRequestBody,
Expand All @@ -104,7 +106,7 @@ void main() {
expect(exception.errorMessage, isNotEmpty);
expect(exception.errorMessage, equals(mockErrorMessage));
expect(exception.method, isNotNull);
expect(exception.method, equals(mockVerb));
expect(exception.method, equals(mockRequestMethod));
expect(exception.headers, isNotNull);
expect(exception.headers, isNotEmpty);
expect(exception.headers, equals(mockHeaders));
Expand All @@ -125,7 +127,7 @@ void main() {
// Act
final RestException exception = RestException(
errorMessage: mockErrorMessage,
method: mockVerb,
method: mockRequestMethod,
uri: mockRestUri,
headers: mockHeaders,
requestBody: mockRequestBody,
Expand All @@ -137,7 +139,7 @@ void main() {
expect(exception.errorMessage, isNotEmpty);
expect(exception.errorMessage, equals(mockErrorMessage));
expect(exception.method, isNotNull);
expect(exception.method, equals(mockVerb));
expect(exception.method, equals(mockRequestMethod));
expect(exception.headers, isNotNull);
expect(exception.headers, isNotEmpty);
expect(exception.headers, equals(mockHeaders));
Expand All @@ -159,7 +161,7 @@ void main() {
// Act
final RestException exception = RestException(
errorMessage: mockErrorMessage,
method: mockVerb,
method: mockRequestMethod,
uri: mockRestUri,
headers: mockHeaders,
requestBody: mockRequestBody,
Expand All @@ -172,7 +174,7 @@ void main() {
expect(exception.errorMessage, isNotEmpty);
expect(exception.errorMessage, equals(mockErrorMessage));
expect(exception.method, isNotNull);
expect(exception.method, equals(mockVerb));
expect(exception.method, equals(mockRequestMethod));
expect(exception.headers, isNotNull);
expect(exception.headers, isNotEmpty);
expect(exception.headers, equals(mockHeaders));
Expand All @@ -195,7 +197,7 @@ void main() {
// Act
final RestException exception = RestException(
errorMessage: mockErrorMessage,
method: mockVerb,
method: mockRequestMethod,
uri: mockRestUri,
headers: mockHeaders,
requestBody: mockRequestBody,
Expand All @@ -209,7 +211,7 @@ void main() {
expect(exception.errorMessage, isNotEmpty);
expect(exception.errorMessage, equals(mockErrorMessage));
expect(exception.method, isNotNull);
expect(exception.method, equals(mockVerb));
expect(exception.method, equals(mockRequestMethod));
expect(exception.headers, isNotNull);
expect(exception.headers, isNotEmpty);
expect(exception.headers, equals(mockHeaders));
Expand Down Expand Up @@ -305,7 +307,7 @@ void main() {
expect(
exceptionString,
equals(
'RestException{errorMessage: This is an error., method: RestMethod.delete, uri: RestUri{scheme: RestScheme.https, host: ivanwilhelm.dev, path: /posts, queryParameters: null}, headers: null, requestBody: null, responseStatusCode: null, responseBody: null, stackTrace: This is the stacktrace.}',
'RestException(errorMessage: This is an error., method: DELETE, uri: RestUri(scheme: https, host: ivanwilhelm.dev, path: /posts, queryParameters: null), headers: null, requestBody: null, responseStatusCode: null, responseBody: null, stackTrace: This is the stacktrace.)',
),
);
},
Expand Down
78 changes: 75 additions & 3 deletions test/lib/src/models/rest_response_model_test.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import 'package:iw_rest_client/src/models/rest_response_model.dart';
import 'package:iw_rest_client/src/models/rest_status_enum.dart';
import 'package:iw_rest_client/iw_rest_client.dart';
import 'package:test/test.dart';

void main() {
group('RestResponse', () {
test('Should return a RestResponse.', () {
// Act
final RestResponse response = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is the body',
statusCode: RestStatus.ok,
);

// Asserts
expect(response.requestMethod, isNotNull);
expect(response.requestMethod, equals(RestMethod.get));
expect(response.requestUri, isNotNull);
expect(
response.requestUri,
equals(
RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
),
);
expect(response.body, isNotEmpty);
expect(response.body, equals('This is the body'));
expect(response.statusCode, equals(RestStatus.ok));
Expand All @@ -20,11 +38,23 @@ void main() {
test('Should return TRUE when two RestResponse objets are equals.', () {
// Act
final RestResponse response1 = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is the response body.',
statusCode: RestStatus.ok,
);

final RestResponse response2 = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is the response body.',
statusCode: RestStatus.ok,
);
Expand All @@ -36,11 +66,23 @@ void main() {
test('Should return FALSE when two RestResponse objets are different.', () {
// Act
final RestResponse response1 = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is the response body.',
statusCode: RestStatus.ok,
);

final RestResponse response2 = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is another response body.',
statusCode: RestStatus.created,
);
Expand All @@ -54,6 +96,12 @@ void main() {
() {
// Arrange
final RestResponse response = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is the response body.',
statusCode: RestStatus.ok,
);
Expand All @@ -65,7 +113,7 @@ void main() {
expect(
responseString,
equals(
'RestResponse{body: This is the response body., statusCode: RestStatus.ok}',
'RestResponse(requestMethod: GET, requestUri: RestUri(scheme: https, host: localhost, path: /, queryParameters: null), body: This is the response body., statusCode: RestStatus.ok)',
),
);
},
Expand All @@ -76,11 +124,23 @@ void main() {
() {
// Arrange
final RestResponse response1 = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is the response body.',
statusCode: RestStatus.ok,
);

final RestResponse response2 = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is the response body.',
statusCode: RestStatus.ok,
);
Expand All @@ -99,11 +159,23 @@ void main() {
() {
// Arrange
final RestResponse response1 = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is the response body.',
statusCode: RestStatus.ok,
);

final RestResponse response2 = RestResponse(
requestMethod: RestMethod.get,
requestUri: RestUri(
scheme: RestScheme.https,
host: 'localhost',
path: '/',
),
body: 'This is another response body.',
statusCode: RestStatus.created,
);
Expand Down
2 changes: 1 addition & 1 deletion test/lib/src/models/rest_uri_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void main() {
expect(
uriString,
equals(
'RestUri{scheme: RestScheme.https, host: ivanwilhelm.dev, path: /posts, queryParameters: {code: 1}}',
'RestUri(scheme: https, host: ivanwilhelm.dev, path: /posts, queryParameters: {code: 1})',
),
);
},
Expand Down