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.
 
 
 

222 lines
5.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 port
  15. import (
  16. "net"
  17. "net/http"
  18. "testing"
  19. "github.com/google/martian/parse"
  20. )
  21. func TestPortModifierOnPort(t *testing.T) {
  22. mod := NewModifier()
  23. mod.UsePort(8080)
  24. req, err := http.NewRequest("GET", "http://example.com", nil)
  25. if err != nil {
  26. t.Fatalf("NewRequest(): got %v, want no error", err)
  27. }
  28. if err := mod.ModifyRequest(req); err != nil {
  29. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  30. }
  31. _, port, err := net.SplitHostPort(req.URL.Host)
  32. if err != nil {
  33. t.Fatalf("net.SplitHostPort(%q): got %v, want no error", req.URL.Host, err)
  34. }
  35. if got, want := port, "8080"; got != want {
  36. t.Errorf("port: got %v, want %v", got, want)
  37. }
  38. }
  39. func TestPortModifierWithNoConfiguration(t *testing.T) {
  40. mod := NewModifier()
  41. req, err := http.NewRequest("GET", "http://example.com", nil)
  42. if err != nil {
  43. t.Fatalf("NewRequest(): got %v, want no error", err)
  44. }
  45. if err := mod.ModifyRequest(req); err != nil {
  46. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  47. }
  48. if got, want := req.URL.Host, "example.com"; got != want {
  49. t.Errorf("req.URL.Host: got %v, want %v", got, want)
  50. }
  51. req, err = http.NewRequest("GET", "http://example.com:80", nil)
  52. if err != nil {
  53. t.Fatalf("NewRequest(): got %v, want no error", err)
  54. }
  55. if err := mod.ModifyRequest(req); err != nil {
  56. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  57. }
  58. if got, want := req.URL.Host, "example.com:80"; got != want {
  59. t.Errorf("req.URL.Host: got %v, want %v", got, want)
  60. }
  61. }
  62. func TestPortModifierDefaultForScheme(t *testing.T) {
  63. mod := NewModifier()
  64. mod.DefaultPortForScheme()
  65. req, err := http.NewRequest("GET", "HtTp://example.com", nil)
  66. if err != nil {
  67. t.Fatalf("NewRequest(): got %v, want no error", err)
  68. }
  69. if err := mod.ModifyRequest(req); err != nil {
  70. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  71. }
  72. if got, want := req.URL.Host, "example.com:80"; got != want {
  73. t.Errorf("req.URL.Host: got %v, want %v", got, want)
  74. }
  75. }
  76. func TestPortModifierRemove(t *testing.T) {
  77. mod := NewModifier()
  78. mod.RemovePort()
  79. req, err := http.NewRequest("GET", "http://example.com:8080", nil)
  80. if err != nil {
  81. t.Fatalf("NewRequest(): got %v, want no error", err)
  82. }
  83. if err := mod.ModifyRequest(req); err != nil {
  84. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  85. }
  86. if got, want := req.URL.Host, "example.com"; got != want {
  87. t.Errorf("req.URL.Host: got %v, want %v", got, want)
  88. }
  89. }
  90. func TestPortModifierAllFields(t *testing.T) {
  91. mod := NewModifier()
  92. mod.UsePort(8081)
  93. mod.DefaultPortForScheme()
  94. mod.RemovePort()
  95. req, err := http.NewRequest("GET", "http://example.com:8080", nil)
  96. if err != nil {
  97. t.Fatalf("NewRequest(): got %v, want no error", err)
  98. }
  99. if err := mod.ModifyRequest(req); err != nil {
  100. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  101. }
  102. // Last configuration was to remove.
  103. if got, want := req.URL.Host, "example.com"; got != want {
  104. t.Errorf("req.URL.Host: got %v, want %v", got, want)
  105. }
  106. }
  107. func TestModiferFromJSON(t *testing.T) {
  108. msg := []byte(`{
  109. "port.Modifier": {
  110. "scope": ["request"],
  111. "port": 8080
  112. }
  113. }`)
  114. r, err := parse.FromJSON(msg)
  115. if err != nil {
  116. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  117. }
  118. reqmod := r.RequestModifier()
  119. if reqmod == nil {
  120. t.Fatal("reqmod: got nil, want not nil")
  121. }
  122. req, err := http.NewRequest("GET", "http://example.com", nil)
  123. if err != nil {
  124. t.Fatalf("NewRequest(): got %v, want no error", err)
  125. }
  126. if err := reqmod.ModifyRequest(req); err != nil {
  127. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  128. }
  129. _, port, err := net.SplitHostPort(req.URL.Host)
  130. if err != nil {
  131. t.Fatalf("net.SplitHostPort(%q): got %v, want no error", req.URL.Host, err)
  132. }
  133. if got, want := port, "8080"; got != want {
  134. t.Errorf("port: got %v, want %v", got, want)
  135. }
  136. }
  137. func TestModiferFromJSONInvalidConfigurations(t *testing.T) {
  138. for _, msg := range [][]byte{
  139. []byte(`{
  140. "port.Modifier": {
  141. "scope": ["request"],
  142. "port": 8080,
  143. "defaultForScheme": true,
  144. "remove": true
  145. }
  146. }`),
  147. []byte(`{
  148. "port.Modifier": {
  149. "scope": ["request"],
  150. "port": 8080
  151. "remove": true
  152. }
  153. }`),
  154. []byte(`{
  155. "port.Modifier": {
  156. "scope": ["request"],
  157. "port": 8080
  158. "defaultForScheme": true,
  159. }
  160. }`),
  161. []byte(`{
  162. "port.Modifier": {
  163. "scope": ["request"],
  164. "defaultForScheme": true,
  165. "remove": true
  166. }
  167. }`),
  168. []byte(`{
  169. "port.Modifier": {
  170. "scope": ["request"],
  171. }
  172. }`),
  173. []byte(`{
  174. "port.Modifier": {
  175. "scope": ["response"],
  176. "remove": true
  177. }
  178. }`),
  179. } {
  180. _, err := parse.FromJSON(msg)
  181. if err == nil {
  182. t.Fatalf("parseFromJSON(msg): Got no error, but should have gotten one.")
  183. }
  184. }
  185. }