25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

177 satır
4.4 KiB

  1. // Copyright 2018 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 proxy
  15. import (
  16. "io/ioutil"
  17. "net/http"
  18. "net/url"
  19. "strings"
  20. "testing"
  21. "cloud.google.com/go/internal/testutil"
  22. "github.com/google/go-cmp/cmp/cmpopts"
  23. "github.com/google/martian"
  24. )
  25. func TestLogger(t *testing.T) {
  26. req := &http.Request{
  27. Method: "POST",
  28. URL: &url.URL{
  29. Scheme: "https",
  30. Host: "example.com",
  31. Path: "a/b/c",
  32. },
  33. Header: http.Header{"H1": {"v1", "v2"}, "Content-Type": {"text/plain"}},
  34. Body: ioutil.NopCloser(strings.NewReader("hello")),
  35. Trailer: http.Header{"T1": {"v3", "v4"}},
  36. }
  37. res := &http.Response{
  38. Request: req,
  39. StatusCode: 204,
  40. Body: ioutil.NopCloser(strings.NewReader("goodbye")),
  41. Header: http.Header{"H2": {"v5"}},
  42. Trailer: http.Header{"T2": {"v6", "v7"}},
  43. }
  44. l := newLogger()
  45. _, remove, err := martian.TestContext(req, nil, nil)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. defer remove()
  50. if err := l.ModifyRequest(req); err != nil {
  51. t.Fatal(err)
  52. }
  53. if err := l.ModifyResponse(res); err != nil {
  54. t.Fatal(err)
  55. }
  56. lg := l.Extract()
  57. want := []*Entry{
  58. {
  59. ID: lg.Entries[0].ID,
  60. Request: &Request{
  61. Method: "POST",
  62. URL: "https://example.com/a/b/c",
  63. Header: http.Header{"H1": {"v1", "v2"}},
  64. MediaType: "text/plain",
  65. BodyParts: [][]byte{[]byte("hello")},
  66. Trailer: http.Header{"T1": {"v3", "v4"}},
  67. },
  68. Response: &Response{
  69. StatusCode: 204,
  70. Body: []byte("goodbye"),
  71. Header: http.Header{"H2": {"v5"}},
  72. Trailer: http.Header{"T2": {"v6", "v7"}},
  73. },
  74. },
  75. }
  76. if diff := testutil.Diff(lg.Entries, want); diff != "" {
  77. t.Error(diff)
  78. }
  79. }
  80. func TestToHTTPResponse(t *testing.T) {
  81. for _, test := range []struct {
  82. desc string
  83. lr *Response
  84. req *http.Request
  85. want *http.Response
  86. }{
  87. {
  88. desc: "GET request",
  89. lr: &Response{
  90. StatusCode: 201,
  91. Proto: "1.1",
  92. Header: http.Header{"h": {"v"}},
  93. Body: []byte("text"),
  94. },
  95. req: &http.Request{Method: "GET"},
  96. want: &http.Response{
  97. Request: &http.Request{Method: "GET"},
  98. StatusCode: 201,
  99. Proto: "1.1",
  100. Header: http.Header{"h": {"v"}},
  101. ContentLength: 4,
  102. },
  103. },
  104. {
  105. desc: "HEAD request with no Content-Length header",
  106. lr: &Response{
  107. StatusCode: 201,
  108. Proto: "1.1",
  109. Header: http.Header{"h": {"v"}},
  110. Body: []byte("text"),
  111. },
  112. req: &http.Request{Method: "HEAD"},
  113. want: &http.Response{
  114. Request: &http.Request{Method: "HEAD"},
  115. StatusCode: 201,
  116. Proto: "1.1",
  117. Header: http.Header{"h": {"v"}},
  118. ContentLength: -1,
  119. },
  120. },
  121. {
  122. desc: "HEAD request with Content-Length header",
  123. lr: &Response{
  124. StatusCode: 201,
  125. Proto: "1.1",
  126. Header: http.Header{"h": {"v"}, "Content-Length": {"17"}},
  127. Body: []byte("text"),
  128. },
  129. req: &http.Request{Method: "HEAD"},
  130. want: &http.Response{
  131. Request: &http.Request{Method: "HEAD"},
  132. StatusCode: 201,
  133. Proto: "1.1",
  134. Header: http.Header{"h": {"v"}, "Content-Length": {"17"}},
  135. ContentLength: 17,
  136. },
  137. },
  138. } {
  139. got := toHTTPResponse(test.lr, test.req)
  140. got.Body = nil
  141. if diff := testutil.Diff(got, test.want, cmpopts.IgnoreUnexported(http.Request{})); diff != "" {
  142. t.Errorf("%s: %s", test.desc, diff)
  143. }
  144. }
  145. }
  146. func TestEmptyBody(t *testing.T) {
  147. // Verify that a zero-length body is nil after logging.
  148. // That will ensure that net/http sends a "Content-Length: 0" header.
  149. req := &http.Request{
  150. Method: "POST",
  151. URL: &url.URL{
  152. Scheme: "https",
  153. Host: "example.com",
  154. Path: "a/b/c",
  155. },
  156. Body: ioutil.NopCloser(strings.NewReader("")),
  157. }
  158. l := newLogger()
  159. _, remove, err := martian.TestContext(req, nil, nil)
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. defer remove()
  164. if err := l.ModifyRequest(req); err != nil {
  165. t.Fatal(err)
  166. }
  167. if req.Body != nil {
  168. t.Error("got non-nil req.Body, want nil")
  169. }
  170. }