Skip to content

Commit

Permalink
Merge pull request #163 from ba-st/better_unprocessable_entity_support
Browse files Browse the repository at this point in the history
Improve unprocessable entities detection during updates
  • Loading branch information
gcotelli authored Jul 6, 2022
2 parents d6627af + 7d718d5 commit b1be7b1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
16 changes: 16 additions & 0 deletions source/Stargate-Examples-Tests/PetsRESTfulControllerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ PetsRESTfulControllerTest >> testCantUpdatePetIfMissingETag [
withExceptionDo: [ :error | self assert: error code equals: 428 ]
]

{ #category : #'tests - update' }
PetsRESTfulControllerTest >> testCantUpdatePetNameWhenEmpty [

self
createPet;
assert: petRepository findAll first name equals: 'Firulais'.

self
should: [
resourceController
updatePetBasedOn: ( self requestToUpdatePetIdentifiedBy: 1 nameTo: '' )
within: self newHttpRequestContext
]
raise: HTTPClientError unprocessableEntity
]

{ #category : #'tests - update' }
PetsRESTfulControllerTest >> testCantUpdatePetWhenETagDoesNotMatch [

Expand Down
5 changes: 5 additions & 0 deletions source/Stargate-Examples/Pet.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Pet class >> named: aName ofType: aPetType [
{ #category : #'instance creation' }
Pet class >> named: aName ofType: aPetType withStatus: status [

AssertionChecker
enforce: [ aName notEmpty ]
because: 'A pet must have a name'
raising: InstanceCreationFailed.

^ self new initializeNamed: aName ofType: aPetType withStatus: status
]

Expand Down
28 changes: 15 additions & 13 deletions source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,21 @@ RESTfulRequestHandlerBehavior >> from: httpRequest within: requestContext get: f
from: httpRequest
within: requestContext
get: [ :id |
| resourceToUpdate updatedResource |

resourceToUpdate := findBlock cull: id.
self
assert: etag
matchesEntityTagOf: resourceToUpdate
encodedAs: httpRequest contentType
within: requestContext.

updatedResource := self decode: httpRequest within: requestContext.
self exceptionHandler
handleConflictsDuring: [ updateBlock value: resourceToUpdate value: updatedResource ]
]
| resourceToUpdate updatedResource |

resourceToUpdate := findBlock cull: id.
self
assert: etag
matchesEntityTagOf: resourceToUpdate
encodedAs: httpRequest contentType
within: requestContext.

updatedResource := self decode: httpRequest within: requestContext.
self exceptionHandler handleConflictsDuring: [
self exceptionHandler handleDecodingFailedDuring: [
updateBlock value: resourceToUpdate value: updatedResource ]
]
]
]

{ #category : #API }
Expand Down

0 comments on commit b1be7b1

Please sign in to comment.