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.
 
 
 

85 lines
2.4 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 verify provides support for using martian modifiers for request and
  15. // response verifications.
  16. package verify
  17. import (
  18. "net/http"
  19. "github.com/google/martian"
  20. )
  21. // RequestVerifier is a RequestModifier that maintains a verification state.
  22. // RequestVerifiers should only return an error from ModifyRequest for errors
  23. // unrelated to the expectation.
  24. type RequestVerifier interface {
  25. martian.RequestModifier
  26. VerifyRequests() error
  27. ResetRequestVerifications()
  28. }
  29. // ResponseVerifier is a ResponseModifier that maintains a verification state.
  30. // ResponseVerifiers should only return an error from ModifyResponse for errors
  31. // unrelated to the expectation.
  32. type ResponseVerifier interface {
  33. martian.ResponseModifier
  34. VerifyResponses() error
  35. ResetResponseVerifications()
  36. }
  37. // RequestResponseVerifier is a RequestVerifier and a ResponseVerifier.
  38. type RequestResponseVerifier interface {
  39. RequestVerifier
  40. ResponseVerifier
  41. }
  42. // TestVerifier is a request and response verifier with overridable errors for
  43. // verification.
  44. type TestVerifier struct {
  45. RequestError error
  46. ResponseError error
  47. }
  48. // ModifyRequest is a no-op.
  49. func (tv *TestVerifier) ModifyRequest(*http.Request) error {
  50. return nil
  51. }
  52. // ModifyResponse is a no-op.
  53. func (tv *TestVerifier) ModifyResponse(*http.Response) error {
  54. return nil
  55. }
  56. // VerifyRequests returns the set request error.
  57. func (tv *TestVerifier) VerifyRequests() error {
  58. return tv.RequestError
  59. }
  60. // VerifyResponses returns the set response error.
  61. func (tv *TestVerifier) VerifyResponses() error {
  62. return tv.ResponseError
  63. }
  64. // ResetRequestVerifications clears out the set request error.
  65. func (tv *TestVerifier) ResetRequestVerifications() {
  66. tv.RequestError = nil
  67. }
  68. // ResetResponseVerifications clears out the set response error.
  69. func (tv *TestVerifier) ResetResponseVerifications() {
  70. tv.ResponseError = nil
  71. }