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.
 
 
 

109 line
3.1 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 querystring
  15. import (
  16. "net/http"
  17. "testing"
  18. "github.com/google/martian/parse"
  19. )
  20. func TestNewQueryStringModifier(t *testing.T) {
  21. mod := NewModifier("testing", "true")
  22. req, err := http.NewRequest("GET", "/", nil)
  23. if err != nil {
  24. t.Fatalf("NewRequest(): got %v, want no error", err)
  25. }
  26. if err := mod.ModifyRequest(req); err != nil {
  27. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  28. }
  29. if got, want := req.URL.Query().Get("testing"), "true"; got != want {
  30. t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "testing", got, want)
  31. }
  32. }
  33. func TestQueryStringModifierQueryExists(t *testing.T) {
  34. mod := NewModifier("testing", "true")
  35. req, err := http.NewRequest("GET", "/?testing=false", nil)
  36. if err != nil {
  37. t.Fatalf("NewRequest(): got %v, want no error", err)
  38. }
  39. if err := mod.ModifyRequest(req); err != nil {
  40. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  41. }
  42. if got, want := req.URL.Query().Get("testing"), "true"; got != want {
  43. t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "testing", got, want)
  44. }
  45. }
  46. func TestQueryStringModifierQueryExistsMultipleKeys(t *testing.T) {
  47. mod := NewModifier("testing", "true")
  48. req, err := http.NewRequest("GET", "/?testing=false&testing=foo&foo=bar", nil)
  49. if err != nil {
  50. t.Fatalf("NewRequest(): got %v, want no error", err)
  51. }
  52. if err := mod.ModifyRequest(req); err != nil {
  53. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  54. }
  55. if got, want := req.URL.Query().Get("testing"), "true"; got != want {
  56. t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "testing", got, want)
  57. }
  58. if got, want := req.URL.Query().Get("foo"), "bar"; got != want {
  59. t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "testing", got, want)
  60. }
  61. }
  62. func TestModifierFromJSON(t *testing.T) {
  63. msg := []byte(`
  64. {
  65. "querystring.Modifier": {
  66. "scope": ["request"],
  67. "name": "param",
  68. "value": "true"
  69. }
  70. }`)
  71. r, err := parse.FromJSON(msg)
  72. if err != nil {
  73. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  74. }
  75. req, err := http.NewRequest("GET", "http://martian.test", nil)
  76. if err != nil {
  77. t.Fatalf("http.NewRequest(): got %q, want no error", err)
  78. }
  79. reqmod := r.RequestModifier()
  80. if reqmod == nil {
  81. t.Fatalf("reqmod: got nil, want not nil")
  82. }
  83. if err := reqmod.ModifyRequest(req); err != nil {
  84. t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
  85. }
  86. if got, want := req.URL.Query().Get("param"), "true"; got != want {
  87. t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "param", got, want)
  88. }
  89. }