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.
 
 
 

82 lines
2.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 header
  15. import (
  16. "net/http"
  17. "testing"
  18. "github.com/google/martian"
  19. "github.com/google/martian/parse"
  20. )
  21. func TestIdModifier(t *testing.T) {
  22. mod := NewIDModifier()
  23. req, err := http.NewRequest("GET", "/", nil)
  24. if err != nil {
  25. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  26. }
  27. ctx, remove, err := martian.TestContext(req, nil, nil)
  28. if err != nil {
  29. t.Fatalf("TestContext(): got %v, want no error", err)
  30. }
  31. defer remove()
  32. if err := mod.ModifyRequest(req); err != nil {
  33. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  34. }
  35. if got, want := req.Header.Get("X-Martian-ID"), ctx.ID(); got != want {
  36. t.Errorf("req.Header.Get(%q): got %q, want %q", "X-Martian-ID", got, want)
  37. }
  38. }
  39. func TestFromJSON(t *testing.T) {
  40. msg := []byte(`{
  41. "header.Id": {}
  42. }`)
  43. r, err := parse.FromJSON(msg)
  44. if err != nil {
  45. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  46. }
  47. reqmod := r.RequestModifier()
  48. if _, ok := reqmod.(*idModifier); !ok {
  49. t.Fatal("reqmod.(*idModifier): got !ok, want ok")
  50. }
  51. req, err := http.NewRequest("GET", "/", nil)
  52. if err != nil {
  53. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  54. }
  55. ctx, remove, err := martian.TestContext(req, nil, nil)
  56. if err != nil {
  57. t.Fatalf("TestContext(): got %v, want no error", err)
  58. }
  59. defer remove()
  60. if err := reqmod.ModifyRequest(req); err != nil {
  61. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  62. }
  63. if got, want := req.Header.Get("X-Martian-ID"), ctx.ID(); got != want {
  64. t.Errorf("req.Header.Get(%q): got %q, want %q", "X-Martian-ID", got, want)
  65. }
  66. }