diff --git a/CHANGELOG.md b/CHANGELOG.md index cfc329b..9cbadca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/src/exceptions/rest_exception.dart b/lib/src/exceptions/rest_exception.dart index 73a4918..400b1ac 100644 --- a/lib/src/exceptions/rest_exception.dart +++ b/lib/src/exceptions/rest_exception.dart @@ -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. diff --git a/lib/src/models/rest_response_model.dart b/lib/src/models/rest_response_model.dart index c6bd33e..6c1bc07 100644 --- a/lib/src/models/rest_response_model.dart +++ b/lib/src/models/rest_response_model.dart @@ -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; @@ -10,18 +17,25 @@ 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. @@ -29,6 +43,9 @@ class RestResponse { 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; } } diff --git a/lib/src/models/rest_uri_model.dart b/lib/src/models/rest_uri_model.dart index f45e890..c96e6a8 100644 --- a/lib/src/models/rest_uri_model.dart +++ b/lib/src/models/rest_uri_model.dart @@ -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. diff --git a/lib/src/services/http_client_service.dart b/lib/src/services/http_client_service.dart index 3738fe5..edcf982 100644 --- a/lib/src/services/http_client_service.dart +++ b/lib/src/services/http_client_service.dart @@ -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!, ); diff --git a/pubspec.yaml b/pubspec.yaml index 72f5268..6dfeb88 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: diff --git a/test/lib/src/exceptions/rest_exception_test.dart b/test/lib/src/exceptions/rest_exception_test.dart index d93bbf5..164f25b 100644 --- a/test/lib/src/exceptions/rest_exception_test.dart +++ b/test/lib/src/exceptions/rest_exception_test.dart @@ -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 mockHeaders = { 'Content-Type': 'application/json', @@ -24,6 +24,8 @@ void main() { }; final RestResponse mockResponseBody = RestResponse( + requestUri: mockRestUri, + requestMethod: mockRequestMethod, body: 'This is the body.', statusCode: RestStatus.ok, ); @@ -39,7 +41,7 @@ void main() { // Act final RestException exception = RestException( errorMessage: mockErrorMessage, - method: mockVerb, + method: mockRequestMethod, uri: mockRestUri, ); @@ -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); @@ -64,7 +66,7 @@ void main() { // Act final RestException exception = RestException( errorMessage: mockErrorMessage, - method: mockVerb, + method: mockRequestMethod, headers: mockHeaders, uri: mockRestUri, ); @@ -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)); @@ -93,7 +95,7 @@ void main() { // Act final RestException exception = RestException( errorMessage: mockErrorMessage, - method: mockVerb, + method: mockRequestMethod, uri: mockRestUri, headers: mockHeaders, requestBody: mockRequestBody, @@ -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)); @@ -125,7 +127,7 @@ void main() { // Act final RestException exception = RestException( errorMessage: mockErrorMessage, - method: mockVerb, + method: mockRequestMethod, uri: mockRestUri, headers: mockHeaders, requestBody: mockRequestBody, @@ -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)); @@ -159,7 +161,7 @@ void main() { // Act final RestException exception = RestException( errorMessage: mockErrorMessage, - method: mockVerb, + method: mockRequestMethod, uri: mockRestUri, headers: mockHeaders, requestBody: mockRequestBody, @@ -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)); @@ -195,7 +197,7 @@ void main() { // Act final RestException exception = RestException( errorMessage: mockErrorMessage, - method: mockVerb, + method: mockRequestMethod, uri: mockRestUri, headers: mockHeaders, requestBody: mockRequestBody, @@ -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)); @@ -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.)', ), ); }, diff --git a/test/lib/src/models/rest_response_model_test.dart b/test/lib/src/models/rest_response_model_test.dart index 81fd354..3a98f32 100644 --- a/test/lib/src/models/rest_response_model_test.dart +++ b/test/lib/src/models/rest_response_model_test.dart @@ -1,5 +1,4 @@ -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() { @@ -7,11 +6,30 @@ void main() { 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)); @@ -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, ); @@ -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, ); @@ -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, ); @@ -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)', ), ); }, @@ -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, ); @@ -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, ); diff --git a/test/lib/src/models/rest_uri_model_test.dart b/test/lib/src/models/rest_uri_model_test.dart index c456143..5b154e5 100644 --- a/test/lib/src/models/rest_uri_model_test.dart +++ b/test/lib/src/models/rest_uri_model_test.dart @@ -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})', ), ); },