context_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 martian
  15. import (
  16. "bufio"
  17. "bytes"
  18. "io/ioutil"
  19. "net"
  20. "net/http"
  21. "testing"
  22. )
  23. func TestContexts(t *testing.T) {
  24. req, err := http.NewRequest("GET", "http://example.com", nil)
  25. if err != nil {
  26. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  27. }
  28. ctx, remove, err := TestContext(req, nil, nil)
  29. if err != nil {
  30. t.Fatalf("TestContext(): got %v, want no error", err)
  31. }
  32. defer remove()
  33. if len(ctx.ID()) != 16 {
  34. t.Errorf("ctx.ID(): got %q, want 16 character random ID", ctx.ID())
  35. }
  36. ctx.Set("key", "value")
  37. got, ok := ctx.Get("key")
  38. if !ok {
  39. t.Errorf("ctx.Get(%q): got !ok, want ok", "key")
  40. }
  41. if want := "value"; got != want {
  42. t.Errorf("ctx.Get(%q): got %q, want %q", "key", got, want)
  43. }
  44. ctx.SkipRoundTrip()
  45. if !ctx.SkippingRoundTrip() {
  46. t.Error("ctx.SkippingRoundTrip(): got false, want true")
  47. }
  48. ctx.SkipLogging()
  49. if !ctx.SkippingLogging() {
  50. t.Error("ctx.SkippingLogging(): got false, want true")
  51. }
  52. s := ctx.Session()
  53. if len(s.ID()) != 16 {
  54. t.Errorf("s.ID(): got %q, want 16 character random ID", s.ID())
  55. }
  56. s.MarkSecure()
  57. if !s.IsSecure() {
  58. t.Error("s.IsSecure(): got false, want true")
  59. }
  60. s.Set("key", "value")
  61. got, ok = s.Get("key")
  62. if !ok {
  63. t.Errorf("s.Get(%q): got !ok, want ok", "key")
  64. }
  65. if want := "value"; got != want {
  66. t.Errorf("s.Get(%q): got %q, want %q", "key", got, want)
  67. }
  68. ctx2, remove, err := TestContext(req, nil, nil)
  69. if err != nil {
  70. t.Fatalf("TestContext(): got %v, want no error", err)
  71. }
  72. defer remove()
  73. if ctx != ctx2 {
  74. t.Error("TestContext(): got new context, want existing context")
  75. }
  76. }
  77. func TestContextHijack(t *testing.T) {
  78. rc, wc := net.Pipe()
  79. req, err := http.NewRequest("GET", "http://example.com", nil)
  80. if err != nil {
  81. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  82. }
  83. ctx, remove, err := TestContext(req, rc, bufio.NewReadWriter(bufio.NewReader(rc), bufio.NewWriter(rc)))
  84. if err != nil {
  85. t.Fatalf("TestContext(): got %v, want no error", err)
  86. }
  87. defer remove()
  88. session := ctx.Session()
  89. if session.Hijacked() {
  90. t.Fatal("session.Hijacked(): got true, want false")
  91. }
  92. conn, brw, err := session.Hijack()
  93. if err != nil {
  94. t.Fatalf("session.Hijack(): got %v, want no error", err)
  95. }
  96. if !session.Hijacked() {
  97. t.Fatal("session.Hijacked(): got false, want true")
  98. }
  99. if _, _, err := session.Hijack(); err == nil {
  100. t.Fatal("session.Hijack(): got nil, want rehijack error")
  101. }
  102. go func() {
  103. brw.Write([]byte("test message"))
  104. brw.Flush()
  105. conn.Close()
  106. }()
  107. got, err := ioutil.ReadAll(wc)
  108. if err != nil {
  109. t.Fatalf("ioutil.ReadAll(): got %v, want no error", err)
  110. }
  111. if want := []byte("test message"); !bytes.Equal(got, want) {
  112. t.Errorf("connection: got %q, want %q", got, want)
  113. }
  114. }