ZnockClient.class.st 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 >> fakeResponse: aZnResponse [
  25. response := aZnResponse.
  26. (response respondsTo: #signal) ifTrue: [ response signal ]
  27. ]
  28. { #category : #'private protocol' }
  29. ZnockClient >> getConnectionAndExecute [
  30. | expectation |
  31. expectation := interceptor consumeExpectationFor: self.
  32. expectation ifNil: [ ^ super executeRequestResponse ].
  33. expectation customizeResponseFromRequest: request.
  34. expectation delay ifNotNil: [ :delay | self fakeDelay: delay ].
  35. self fakeResponse: expectation response.
  36. ^ response contents
  37. ]
  38. { #category : #accessing }
  39. ZnockClient >> interceptor: anObject [
  40. interceptor := anObject
  41. ]
  42. { #category : #comparing }
  43. ZnockClient >> matches: aZnockExpectation [
  44. | req exp |
  45. req := self request.
  46. exp := aZnockExpectation request.
  47. exp url scheme ifNotNil:
  48. [ :scheme | scheme = req url scheme ifFalse: [ ^ false ] ].
  49. exp url authority ifNotNil:
  50. [ :authority | authority = req url authority ifFalse: [ ^ false ] ].
  51. exp method ifNotNil:
  52. [ :method | method = req method ifFalse: [ ^ false ] ].
  53. (req url path beginsWith: exp url path) ifFalse: [ ^ false ].
  54. ^ true
  55. ]