- Updated unittest with the new TMVCResponse behavior

This commit is contained in:
Daniele Teti 2023-08-04 14:34:42 +02:00
parent 8631a155ff
commit 4a27f8b64d
2 changed files with 78 additions and 24 deletions

View File

@ -318,14 +318,14 @@ Congratulations to Daniele Teti and all the staff for the excellent work!" -- Ma
- ⚡ Improved `TMVCResponse` type to better suits the new functional actions.
`TMVCResponse` can be used with "message based" responses and also with "data based" responses (with single object or with a list of objects).
`TMVCResponse` can be used with "message based" responses and also with "data based" responses (with single object, with a list of objects or with a dictionary of objects).
**Message based responses**
```pascal
function TMyController.GetMVCResponse: TMVCResponse;
begin
Result := TMVCResponse.Create(HTTP_STATUS.OK, 'My Reason String', 'My Message');
Result := MVCResponse(HTTP_STATUS.OK, 'My Message');
end;
```
@ -333,20 +333,18 @@ Congratulations to Daniele Teti and all the staff for the excellent work!" -- Ma
```json
{
"statuscode":200,
"reasonstring":"My Reason String",
"message":"My Message"
}
```
**Data based reponses with single object**
**Data based response with single object**
```pascal
function TMyController.GetMVCResponse2: TMVCResponse;
begin
Result := TMVCResponse.Create(HTTP_STATUS.OK, 'My Reason String', TPerson.Create('Daniele','Teti', 99));
Result := MVCResponse(HTTP_STATUS.OK, TPerson.Create('Daniele','Teti', 99));
end;
```
@ -354,9 +352,6 @@ Congratulations to Daniele Teti and all the staff for the excellent work!" -- Ma
```json
{
"statuscode": 200,
"reasonstring": "My Reason String",
"message": "",
"data": {
"firstName": "Daniele",
"lastName": "Teti",
@ -365,12 +360,12 @@ Congratulations to Daniele Teti and all the staff for the excellent work!" -- Ma
}
```
**Data based reponses with list of objects**
**Data based response with list of objects**
```pascal
function TMyController.GetMVCResponse3: TMVCResponse;
begin
Result := TMVCResponse.Create(HTTP_STATUS.OK, 'My Reason String',
Result := MVCResponse(HTTP_STATUS.OK,
TObjectList<TPerson>.Create([
TPerson.Create('Daniele','Teti', 99),
TPerson.Create('Peter','Parker', 25),
@ -384,9 +379,6 @@ Congratulations to Daniele Teti and all the staff for the excellent work!" -- Ma
```json
{
"statuscode": 200,
"reasonstring": "My Reason String",
"message": "",
"data": [
{
"firstName": "Daniele",
@ -407,6 +399,72 @@ Congratulations to Daniele Teti and all the staff for the excellent work!" -- Ma
}
```
**Data dictionary based response with `IMVCObjectDictionary` **
```pascal
function TMyController.GetMVCResponseWithObjectDictionary: IMVCResponse;
begin
Result := MVCResponse(
HTTP_STATUS.OK,
ObjectDict()
.Add('employees', TObjectList<TPerson>.Create([
TPerson.Create('Daniele','Teti', 99),
TPerson.Create('Peter','Parker', 25),
TPerson.Create('Bruce','Banner', 45)
])
)
.Add('customers', TObjectList<TPerson>.Create([
TPerson.Create('Daniele','Teti', 99),
TPerson.Create('Peter','Parker', 25),
TPerson.Create('Bruce','Banner', 45)
])
)
);
end;
```
Produces
```json
{
"employees": [
{
"firstName": "Daniele",
"lastName": "Teti",
"age": 99
},
{
"firstName": "Peter",
"lastName": "Parker",
"age": 25
},
{
"firstName": "Bruce",
"lastName": "Banner",
"age": 45
}
],
"customers": [
{
"firstName": "Daniele",
"lastName": "Teti",
"age": 99
},
{
"firstName": "Peter",
"lastName": "Parker",
"age": 25
},
{
"firstName": "Bruce",
"lastName": "Banner",
"age": 45
}
]
}
```
- Removed `statuscode` and `reasonstring` from exception's JSON rendering.
## Old Versions

View File

@ -1084,12 +1084,11 @@ var
begin
res := RESTClient.Get('/exception/emvcexception1');
Assert.areEqual<Integer>(HTTP_STATUS.InternalServerError, res.StatusCode);
Assert.areEqual('Internal Server Error', res.StatusText);
lJSON := StrToJSONObject(res.Content);
try
Assert.areEqual<string>('message', lJSON.S['message'], lJSON.ToJSON());
Assert.areEqual<string>('EMVCException', lJSON.S['classname'], lJSON.ToJSON());
Assert.areEqual<Integer>(500, lJSON.I['statuscode'], lJSON.ToJSON());
Assert.areEqual<string>('Internal Server Error', lJSON.S['reasonstring'], lJSON.ToJSON());
Assert.areEqual(0, lJSON.A['items'].Count, lJSON.ToJSON());
Assert.isTrue(lJSON.IsNull('data'), lJSON.ToJSON());
finally
@ -1105,12 +1104,11 @@ var
begin
res := RESTClient.Get('/exception/emvcexception2');
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, res.StatusCode);
Assert.areEqual('Bad Request', res.StatusText);
lJSON := StrToJSONObject(res.Content);
try
Assert.areEqual<string>('message', lJSON.S['message'], lJSON.ToJSON());
Assert.areEqual<string>('EMVCException', lJSON.S['classname'], lJSON.ToJSON());
Assert.areEqual<Integer>(HTTP_STATUS.BadRequest, lJSON.I['statuscode'], lJSON.ToJSON());
Assert.areEqual<string>('Bad Request', lJSON.S['reasonstring'], lJSON.ToJSON());
Assert.areEqual(0, lJSON.A['items'].Count, lJSON.ToJSON());
Assert.isTrue(lJSON.IsNull('data'), lJSON.ToJSON());
finally
@ -1125,12 +1123,11 @@ var
begin
res := RESTClient.Get('/exception/emvcexception3');
Assert.areEqual<Integer>(HTTP_STATUS.Created, res.StatusCode);
Assert.areEqual('Created', res.StatusText);
lJSON := StrToJSONObject(res.Content);
try
Assert.areEqual('message', lJSON.S['message'], lJSON.ToJSON());
Assert.areEqual('EMVCException', lJSON.S['classname'], lJSON.ToJSON());
Assert.areEqual(HTTP_STATUS.Created, lJSON.I['statuscode'], lJSON.ToJSON());
Assert.areEqual('Created', lJSON.S['reasonstring'], lJSON.ToJSON());
Assert.areEqual(999, lJSON.I['apperrorcode'], lJSON.ToJSON());
Assert.areEqual(0, lJSON.A['items'].Count, lJSON.ToJSON());
Assert.isTrue(lJSON.IsNull('data'), lJSON.ToJSON());
@ -1146,13 +1143,12 @@ var
begin
res := RESTClient.Get('/exception/emvcexception4');
Assert.areEqual<Integer>(HTTP_STATUS.Created, res.StatusCode);
Assert.areEqual('Created', res.StatusText);
lJSON := StrToJSONObject(res.Content);
try
Assert.areEqual('message', lJSON.S['message'], lJSON.ToJSON());
Assert.areEqual('detailedmessage', lJSON.S['detailedmessage'], lJSON.ToJSON());
Assert.areEqual('EMVCException', lJSON.S['classname'], lJSON.ToJSON());
Assert.areEqual(HTTP_STATUS.Created, lJSON.I['statuscode'], lJSON.ToJSON());
Assert.areEqual('Created', lJSON.S['reasonstring'], lJSON.ToJSON());
Assert.areEqual(999, lJSON.I['apperrorcode'], lJSON.ToJSON());
Assert.areEqual(2, lJSON.A['items'].Count, lJSON.ToJSON());
Assert.areEqual('erritem1', lJSON.A['items'].O[0].S['message'], lJSON.ToJSON());