ZnockClient.class.st 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. Class {
  2. #name : #ZnockClient,
  3. #superclass : #ZnClient,
  4. #instVars : [
  5. 'interceptor'
  6. ],
  7. #category : #Znock
  8. }
  9. { #category : #'instance creation' }
  10. ZnockClient class >> newWithInterceptor: aZnock [
  11. ^ self new
  12. interceptor: aZnock;
  13. yourself
  14. ]
  15. { #category : #'private protocol' }
  16. ZnockClient >> fakeDelay: aDuration [
  17. | localTimeout |
  18. localTimeout := self timeout seconds.
  19. localTimeout <= aDuration
  20. ifTrue: [ localTimeout asDelay wait. ConnectionTimedOut signal: 'Znock timeout' ]
  21. ifFalse: [ aDuration asDelay wait ]
  22. ]
  23. { #category : #'private protocol' }
  24. ZnockClient >> getConnectionAndExecute [
  25. | expectation |
  26. expectation := interceptor consumeExpectationFor: self.
  27. expectation ifNil: [ ^ super executeRequestResponse ].
  28. expectation customizeResponseFromRequest: request.
  29. expectation delay ifNotNil: [ :delay | self fakeDelay: delay ].
  30. response := expectation response.
  31. ^ response contents
  32. ]
  33. { #category : #accessing }
  34. ZnockClient >> interceptor: anObject [
  35. interceptor := anObject
  36. ]
  37. { #category : #comparing }
  38. ZnockClient >> matches: aZnockExpectation [
  39. | req exp |
  40. req := self request.
  41. exp := aZnockExpectation request.
  42. exp url scheme ifNotNil:
  43. [ :scheme | scheme = req url scheme ifFalse: [ ^ false ] ].
  44. exp url authority ifNotNil:
  45. [ :authority | authority = req url authority ifFalse: [ ^ false ] ].
  46. exp method ifNotNil:
  47. [ :method | method = req method ifFalse: [ ^ false ] ].
  48. (req url path beginsWith: exp url path) ifFalse: [ ^ false ].
  49. ^ true
  50. ]