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.
 
 
 

177 lines
4.2 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 parse
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "testing"
  19. "github.com/google/martian"
  20. "github.com/google/martian/martiantest"
  21. )
  22. func TestFromJSON(t *testing.T) {
  23. msg := []byte(`{
  24. "first.Modifier": { },
  25. "second.Modifier": { }
  26. }`)
  27. if _, err := FromJSON(msg); err == nil {
  28. t.Error("FromJson(): got nil, want more than one key error")
  29. }
  30. Register("martiantest.Modifier", func(b []byte) (*Result, error) {
  31. type testJSON struct {
  32. Scope []ModifierType `json:"scope"`
  33. }
  34. msg := &testJSON{}
  35. if err := json.Unmarshal(b, msg); err != nil {
  36. return nil, err
  37. }
  38. tm := martiantest.NewModifier()
  39. return NewResult(tm, msg.Scope)
  40. })
  41. msg = []byte(`{
  42. "martiantest.Modifier": { }
  43. }`)
  44. r, err := FromJSON(msg)
  45. if err != nil {
  46. t.Fatalf("FromJSON(): got %v, want no error", err)
  47. }
  48. if _, ok := r.RequestModifier().(*martiantest.Modifier); !ok {
  49. t.Fatal("r.RequestModifier().(*martiantest.Modifier): got !ok, want ok")
  50. }
  51. if _, ok := r.ResponseModifier().(*martiantest.Modifier); !ok {
  52. t.Fatal("r.ResponseModifier().(*martiantest.Modifier): got !ok, want ok")
  53. }
  54. msg = []byte(`{
  55. "martiantest.Modifier": {
  56. "scope": ["request"]
  57. }
  58. }`)
  59. r, err = FromJSON(msg)
  60. if err != nil {
  61. t.Fatalf("FromJSON(): got %v, want no error", err)
  62. }
  63. if _, ok := r.RequestModifier().(*martiantest.Modifier); !ok {
  64. t.Fatal("r.RequestModifier().(*martiantest.Modifier): got !ok, want ok")
  65. }
  66. resmod := r.ResponseModifier()
  67. if resmod != nil {
  68. t.Error("r.ResponseModifier(): got not nil, want nil")
  69. }
  70. msg = []byte(`{
  71. "martiantest.Modifier": {
  72. "scope": ["response"]
  73. }
  74. }`)
  75. r, err = FromJSON(msg)
  76. if err != nil {
  77. t.Fatalf("FromJSON(): got %v, want no error", err)
  78. }
  79. if _, ok := r.ResponseModifier().(*martiantest.Modifier); !ok {
  80. t.Fatal("r.ResponseModifier().(*martiantest.Modifier): got !ok, want ok")
  81. }
  82. reqmod := r.RequestModifier()
  83. if reqmod != nil {
  84. t.Error("r.RequestModifier(): got not nil, want nil")
  85. }
  86. }
  87. func TestNewResultMismatchedScopes(t *testing.T) {
  88. reqmod := martian.RequestModifierFunc(
  89. func(*http.Request) error {
  90. return nil
  91. })
  92. resmod := martian.ResponseModifierFunc(
  93. func(*http.Response) error {
  94. return nil
  95. })
  96. if _, err := NewResult(reqmod, []ModifierType{Response}); err == nil {
  97. t.Error("NewResult(reqmod, RESPONSE): got nil, want error")
  98. }
  99. if _, err := NewResult(resmod, []ModifierType{Request}); err == nil {
  100. t.Error("NewResult(resmod, REQUEST): got nil, want error")
  101. }
  102. if _, err := NewResult(reqmod, []ModifierType{ModifierType("unknown")}); err == nil {
  103. t.Error("NewResult(resmod, REQUEST): got nil, want error")
  104. }
  105. }
  106. func TestResultModifierAccessors(t *testing.T) {
  107. tm := martiantest.NewModifier()
  108. r := &Result{
  109. reqmod: tm,
  110. resmod: nil,
  111. }
  112. if reqmod := r.RequestModifier(); reqmod == nil {
  113. t.Error("r.RequestModifier: got nil, want reqmod")
  114. }
  115. if resmod := r.ResponseModifier(); resmod != nil {
  116. t.Error("r.ResponseModifier: got resmod, want nil")
  117. }
  118. r = &Result{
  119. reqmod: nil,
  120. resmod: tm,
  121. }
  122. if reqmod := r.RequestModifier(); reqmod != nil {
  123. t.Errorf("r.RequestModifier: got reqmod, want nil")
  124. }
  125. if resmod := r.ResponseModifier(); resmod == nil {
  126. t.Error("r.ResponseModifier: got nil, want resmod")
  127. }
  128. }
  129. func TestParseUnknownModifierReturnsError(t *testing.T) {
  130. msg := []byte(`{
  131. "unknown.Key": {
  132. "scope": ["request", "response"]
  133. }
  134. }`)
  135. _, err := FromJSON(msg)
  136. umerr, ok := err.(ErrUnknownModifier)
  137. if !ok {
  138. t.Fatalf("FromJSON(): got %v, want ErrUnknownModifier", err)
  139. }
  140. if got, want := umerr.Error(), "parse: unknown modifier: unknown.Key"; got != want {
  141. t.Errorf("Error(): got %q, want %q", got, want)
  142. }
  143. }