ZnockBase.class.st 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. Class {
  2. #name : #ZnockBase,
  3. #superclass : #Object,
  4. #instVars : [
  5. 'owner',
  6. 'state',
  7. 'builtClient',
  8. 'baseUrl'
  9. ],
  10. #category : #Znock
  11. }
  12. { #category : #'instance creation' }
  13. ZnockBase class >> in: aZnock url: aZnUrl [
  14. ^ super new
  15. baseUrl: aZnUrl;
  16. owner: aZnock;
  17. yourself
  18. ]
  19. { #category : #accessing }
  20. ZnockBase >> baseUrl [
  21. ^ baseUrl
  22. ]
  23. { #category : #accessing }
  24. ZnockBase >> baseUrl: aZnUrl [
  25. baseUrl := aZnUrl
  26. ]
  27. { #category : #building }
  28. ZnockBase >> closeState: aSymbol [
  29. "self shouldBeImplemented."
  30. ]
  31. { #category : #building }
  32. ZnockBase >> doesNotUnderstand: aMessage [
  33. self forwardedSelectors keysAndValuesDo: [ :handler :candidates |
  34. (candidates includes: aMessage selector)
  35. ifTrue: [ ^ self perform: handler with: aMessage ] ].
  36. ^ super doesNotUnderstand: aMessage
  37. ]
  38. { #category : #building }
  39. ZnockBase >> forwardedSelectors [
  40. ^ {
  41. #handleClientMessage: ->
  42. #(addPath: addPathSegment: delete get head host: http https method: options patch path: port: post put #username:password:).
  43. #handleResponseCreationMessage: ->
  44. #(accepted noContent notModified ok: redirect: #redirect:entity: serverError: serverErrorWithEntity: statusCode: statusLine: unauthorized unauthorized: #unauthorized:entity:).
  45. #handleResponseCreationFromRequestMessage: ->
  46. #(#badRequest: badRequest:entity: methodNotAllowed: methodNotAllowed:entity:).
  47. #handleResponseCreationFromUrlMessage: ->
  48. #(#created: created:entity: notFound: notFound:entity: redirect: redirect:entity:).
  49. #handleResponseMessage: ->
  50. #(addCookie: entity: headers: resetEntity: setLocation: setWWWAuthenticate: statusLine:).
  51. } asDictionary
  52. ]
  53. { #category : #building }
  54. ZnockBase >> handleClientMessage: aMessage [
  55. self request.
  56. ^ aMessage sendTo: builtClient
  57. ]
  58. { #category : #building }
  59. ZnockBase >> handleResponseCreationFromRequestMessage: aMessage [
  60. aMessage argument ifNotNil: [ ^ self handleResponseCreationMessage: aMessage ].
  61. self will: [ :req :res |
  62. aMessage argument: req.
  63. res customizeFrom: (aMessage sendTo: ZnResponse) ]
  64. ]
  65. { #category : #building }
  66. ZnockBase >> handleResponseCreationFromUrlMessage: aMessage [
  67. aMessage argument ifNotNil: [ ^ self handleResponseCreationMessage: aMessage ].
  68. self will: [ :req :res |
  69. aMessage argument: req url.
  70. res customizeFrom: (aMessage sendTo: ZnResponse) ]
  71. ]
  72. { #category : #building }
  73. ZnockBase >> handleResponseCreationMessage: aMessage [
  74. ^ self response: (aMessage sendTo: ZnResponse)
  75. ]
  76. { #category : #building }
  77. ZnockBase >> handleResponseMessage: aMessage [
  78. self response.
  79. ^ aMessage sendTo: builtClient response
  80. ]
  81. { #category : #building }
  82. ZnockBase >> newCleanResponse [
  83. ^ ZnResponse statusLine: ZnStatusLine ok
  84. ]
  85. { #category : #building }
  86. ZnockBase >> openState: aSymbol [
  87. aSymbol == #request ifTrue: [
  88. builtClient := ZnockExpectation new.
  89. builtClient url: baseUrl.
  90. builtClient request url scheme: baseUrl scheme ].
  91. aSymbol == #response ifTrue: [ self owner addExpectation: builtClient ]
  92. ]
  93. { #category : #accessing }
  94. ZnockBase >> owner [
  95. ^ owner
  96. ]
  97. { #category : #accessing }
  98. ZnockBase >> owner: anObject [
  99. owner := anObject
  100. ]
  101. { #category : #building }
  102. ZnockBase >> request [
  103. self state: #request.
  104. ^ builtClient
  105. ]
  106. { #category : #building }
  107. ZnockBase >> resetState [
  108. self state: nil
  109. ]
  110. { #category : #building }
  111. ZnockBase >> response [
  112. self state: #response.
  113. ^ builtClient response
  114. ]
  115. { #category : #building }
  116. ZnockBase >> response: response [
  117. self response ifNotNil: [ self error: 'Response already created' ].
  118. builtClient response: response
  119. ]
  120. { #category : #building }
  121. ZnockBase >> state: aSymbol [
  122. state == aSymbol ifTrue: [ ^ self ].
  123. self closeState: state.
  124. state := aSymbol.
  125. self openState: state
  126. ]
  127. { #category : #public }
  128. ZnockBase >> url: anObject [
  129. | scheme |
  130. self request.
  131. scheme := builtClient request url scheme.
  132. builtClient url: anObject.
  133. builtClient request url scheme: scheme
  134. ]
  135. { #category : #public }
  136. ZnockBase >> waitFor: aDuration [
  137. self response.
  138. builtClient delay: aDuration
  139. ]
  140. { #category : #public }
  141. ZnockBase >> will: aBlock [
  142. self response: self newCleanResponse.
  143. builtClient customizeResponseBlock: aBlock
  144. ]
  145. { #category : #public }
  146. ZnockBase >> willError: aString [
  147. self will: [ :req :res | self error: aString ]
  148. ]