Skip to content
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
23 changes: 23 additions & 0 deletions source/HOP-Core-Tests/HOPTestResourceRoute.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"
A resource route that resolves its stand-in segment to a fixed object, so a test can build uris relative to a route without a model behind it.
"
Class {
#name : 'HOPTestResourceRoute',
#superclass : 'HOPResourceRoute',
#instVars : [
'standIn'
],
#category : 'HOP-Core-Tests-TestData',
#package : 'HOP-Core-Tests',
#tag : 'TestData'
}

{ #category : 'accessing' }
HOPTestResourceRoute >> standIn [
^ standIn
]

{ #category : 'accessing' }
HOPTestResourceRoute >> standIn: anObject [
standIn := anObject
]
48 changes: 48 additions & 0 deletions source/HOP-Core-Tests/HOPUriBuilderTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,51 @@ HOPUriBuilderTest >> testBuildUriForResourceInAMountedUriSpace [
uri := uriSpace buildUriForResource: object.
self assert: uri equals: '/mounted/objects/1/children/2'
]

{ #category : 'tests' }
HOPUriBuilderTest >> testBuildUriForResourceThroughAStandIn [
"an object below the resource a stand-in gives access to keeps the stand-in's uri as its
prefix when the request came through the stand-in"
| uriSpace route object child |
uriSpace := self uriSpaceWithStandIn.
object := HOPUriBuildObject new.
child := HOPUriBuildChildObject new container: object.
route := (HOPTestResourceRoute new
routeMatch: (uriSpace newRouteMatcher matchUri: '/api/standins/7' asZnUrl);
standIn: (HOPUriStandIn new standsFor: object);
yourself).
self
assert: (uriSpace newUriBuilder resource: child; resourceRoute: route; build)
equals: '/api/standins/7/children/2'.
self
assert: (uriSpace newUriBuilder resource: child; build)
equals: '/api/objects/1/children/2'
]

{ #category : 'tests' }
HOPUriBuilderTest >> testBuildUriOutsideTheScopeOfTheRoute [
"a resource the stand-in does not lead to is built the canonical way"
| uriSpace route child |
uriSpace := self uriSpaceWithStandIn.
child := HOPUriBuildChildObject new container: HOPUriBuildObject new.
route := (HOPTestResourceRoute new
routeMatch: (uriSpace newRouteMatcher matchUri: '/api/standins/7' asZnUrl);
standIn: (HOPUriStandIn new standsFor: HOPUriBuildObject new);
yourself).
self
assert: (uriSpace newUriBuilder resource: child; resourceRoute: route; build)
equals: '/api/objects/1/children/2'
]

{ #category : 'running' }
HOPUriBuilderTest >> uriSpaceWithStandIn [
"objects have their canonical place under /api/objects, a stand-in for one of them lives
under /api/standins"
| api uriSpace |
api := #api asHOPSegment.
uriSpace := (HOPUriSpace /+ api) root.
api /+ #objects /+ (HOPVariableSegment new name: #objectId; segmentNameSelector: #id; yourself) --> HOPUriBuildObject
/+ #children /+ (HOPVariableSegment new name: #childId; segmentNameSelector: #id; yourself) --> HOPUriBuildChildObject.
api /+ #standins /+ (HOPVariableSegment new name: #standInId; segmentNameSelector: #id; resourceSelector: #standIn; yourself) --> HOPUriStandIn.
^ uriSpace
]
28 changes: 28 additions & 0 deletions source/HOP-Core-Tests/HOPUriStandIn.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"
I stand in for another resource, the way a link or a hook gives access to a space: a route through me continues at the resource I stand for.
"
Class {
#name : 'HOPUriStandIn',
#superclass : 'HOPUriObject',
#instVars : [
'standsFor'
],
#category : 'HOP-Core-Tests-TestData',
#package : 'HOP-Core-Tests',
#tag : 'TestData'
}

{ #category : 'accessing' }
HOPUriStandIn >> extraReceiver [
^ standsFor
]

{ #category : 'accessing' }
HOPUriStandIn >> id [
^ '7'
]

{ #category : 'accessing' }
HOPUriStandIn >> standsFor: anObject [
standsFor := anObject
]
14 changes: 13 additions & 1 deletion source/HOP-Core/HOPUriBuilder.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,19 @@ HOPUriBuilder >> visitUriSpace: aUriSpace [
]

{ #category : 'visiting' }
HOPUriBuilder >> visitVariableSegment: aHOPVariableSegment [
HOPUriBuilder >> visitVariableSegment: aHOPVariableSegment [
"walking up from the resource, a resource the current route already stands at takes the
rest of its uri from the route instead of the canonical tree. This keeps a link inside
whatever the request came through, e.g. an object reached via a stand-in for it (see
Object>>extraReceiver). A route continued at another resource looks at the route it
continued from"
resourceRoute ifNotNil: [
| effectiveRoute |
effectiveRoute := resourceRoute previous ifNil: [ resourceRoute ].
(uriSpace isResource: resource inScopeOf: effectiveRoute receiver) ifTrue: [
effectiveRoute receiverSegments reversed
do: [ :each | segments add: (each segmentIn: effectiveRoute) ].
^ self ] ].
segments add: (self valueFor: aHOPVariableSegment) .
resource := self containerOf: aHOPVariableSegment .
super visitVariableSegment: aHOPVariableSegment
Expand Down
9 changes: 9 additions & 0 deletions source/HOP-Core/HOPUriSpace.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ HOPUriSpace >> initialize [
operationSegments := Dictionary new.
]

{ #category : 'testing' }
HOPUriSpace >> isResource: anObject inScopeOf: aReceiver [
"anObject is in the scope of a route's receiver when it is the receiver or the resource the
receiver stands for. A space whose objects can be stand-ins for themselves, like
database proxies, compares them its own way"

^ anObject == aReceiver or: [ anObject == aReceiver extraReceiver ]
]

{ #category : 'printing' }
HOPUriSpace >> isRoot [
^ parent isNil
Expand Down
Loading