You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

146 lines
3.8 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package martiantest provides helper utilities for testing
  15. // modifiers.
  16. package martiantest
  17. import (
  18. "net/http"
  19. "sync/atomic"
  20. )
  21. // Modifier keeps track of the number of requests and responses it has modified
  22. // and can be configured to return errors or run custom functions.
  23. type Modifier struct {
  24. reqcount int32 // atomic
  25. rescount int32 // atomic
  26. reqerr error
  27. reserr error
  28. reqfunc func(*http.Request)
  29. resfunc func(*http.Response)
  30. }
  31. // NewModifier returns a new test modifier.
  32. func NewModifier() *Modifier {
  33. return &Modifier{}
  34. }
  35. // RequestCount returns the number of requests modified.
  36. func (m *Modifier) RequestCount() int32 {
  37. return atomic.LoadInt32(&m.reqcount)
  38. }
  39. // ResponseCount returns the number of responses modified.
  40. func (m *Modifier) ResponseCount() int32 {
  41. return atomic.LoadInt32(&m.rescount)
  42. }
  43. // RequestModified returns whether a request has been modified.
  44. func (m *Modifier) RequestModified() bool {
  45. return m.RequestCount() != 0
  46. }
  47. // ResponseModified returns whether a response has been modified.
  48. func (m *Modifier) ResponseModified() bool {
  49. return m.ResponseCount() != 0
  50. }
  51. // RequestError overrides the error returned by ModifyRequest.
  52. func (m *Modifier) RequestError(err error) {
  53. m.reqerr = err
  54. }
  55. // ResponseError overrides the error returned by ModifyResponse.
  56. func (m *Modifier) ResponseError(err error) {
  57. m.reserr = err
  58. }
  59. // RequestFunc is a function to run during ModifyRequest.
  60. func (m *Modifier) RequestFunc(reqfunc func(req *http.Request)) {
  61. m.reqfunc = reqfunc
  62. }
  63. // ResponseFunc is a function to run during ModifyResponse.
  64. func (m *Modifier) ResponseFunc(resfunc func(res *http.Response)) {
  65. m.resfunc = resfunc
  66. }
  67. // ModifyRequest increases the count of requests seen and runs reqfunc if configured.
  68. func (m *Modifier) ModifyRequest(req *http.Request) error {
  69. atomic.AddInt32(&m.reqcount, 1)
  70. if m.reqfunc != nil {
  71. m.reqfunc(req)
  72. }
  73. return m.reqerr
  74. }
  75. // ModifyResponse increases the count of responses seen and runs resfunc if configured.
  76. func (m *Modifier) ModifyResponse(res *http.Response) error {
  77. atomic.AddInt32(&m.rescount, 1)
  78. if m.resfunc != nil {
  79. m.resfunc(res)
  80. }
  81. return m.reserr
  82. }
  83. // Reset resets the request and response counts, the custom
  84. // functions, and the modifier errors.
  85. func (m *Modifier) Reset() {
  86. atomic.StoreInt32(&m.reqcount, 0)
  87. atomic.StoreInt32(&m.rescount, 0)
  88. m.reqfunc = nil
  89. m.resfunc = nil
  90. m.reqerr = nil
  91. m.reserr = nil
  92. }
  93. // Matcher is a stubbed matcher used in tests.
  94. type Matcher struct {
  95. resval bool
  96. reqval bool
  97. }
  98. // NewMatcher returns a pointer to martiantest.Matcher with the return values
  99. // for MatchRequest and MatchResponse intiailized to true.
  100. func NewMatcher() *Matcher {
  101. return &Matcher{resval: true, reqval: true}
  102. }
  103. // ResponseEvaluatesTo sets the value returned by MatchResponse.
  104. func (tm *Matcher) ResponseEvaluatesTo(value bool) {
  105. tm.resval = value
  106. }
  107. // RequestEvaluatesTo sets the value returned by MatchRequest.
  108. func (tm *Matcher) RequestEvaluatesTo(value bool) {
  109. tm.reqval = value
  110. }
  111. // MatchRequest returns the stubbed value in tm.reqval.
  112. func (tm *Matcher) MatchRequest(*http.Request) bool {
  113. return tm.reqval
  114. }
  115. // MatchResponse returns the stubbed value in tm.resval.
  116. func (tm *Matcher) MatchResponse(*http.Response) bool {
  117. return tm.resval
  118. }