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.
 
 
 

137 lines
3.5 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 martianlog
  15. import (
  16. "bytes"
  17. "compress/gzip"
  18. "fmt"
  19. "net/http"
  20. "strings"
  21. "testing"
  22. "github.com/google/martian"
  23. "github.com/google/martian/parse"
  24. "github.com/google/martian/proxyutil"
  25. )
  26. func ExampleLogger() {
  27. l := NewLogger()
  28. l.SetLogFunc(func(line string) {
  29. // Remove \r to make it easier to test with examples.
  30. fmt.Print(strings.Replace(line, "\r", "", -1))
  31. })
  32. l.SetDecode(true)
  33. buf := new(bytes.Buffer)
  34. gw := gzip.NewWriter(buf)
  35. gw.Write([]byte("request content"))
  36. gw.Close()
  37. req, err := http.NewRequest("GET", "http://example.com/path?querystring", buf)
  38. if err != nil {
  39. fmt.Println(err)
  40. return
  41. }
  42. req.TransferEncoding = []string{"chunked"}
  43. req.Header.Set("Content-Encoding", "gzip")
  44. _, remove, err := martian.TestContext(req, nil, nil)
  45. if err != nil {
  46. fmt.Println(err)
  47. return
  48. }
  49. defer remove()
  50. if err := l.ModifyRequest(req); err != nil {
  51. fmt.Println(err)
  52. return
  53. }
  54. res := proxyutil.NewResponse(200, strings.NewReader("response content"), req)
  55. res.ContentLength = 16
  56. res.Header.Set("Date", "Tue, 15 Nov 1994 08:12:31 GMT")
  57. res.Header.Set("Other-Header", "values")
  58. if err := l.ModifyResponse(res); err != nil {
  59. fmt.Println(err)
  60. return
  61. }
  62. // Output:
  63. // --------------------------------------------------------------------------------
  64. // Request to http://example.com/path?querystring
  65. // --------------------------------------------------------------------------------
  66. // GET http://example.com/path?querystring HTTP/1.1
  67. // Host: example.com
  68. // Transfer-Encoding: chunked
  69. // Content-Encoding: gzip
  70. //
  71. // request content
  72. //
  73. // --------------------------------------------------------------------------------
  74. //
  75. // --------------------------------------------------------------------------------
  76. // Response from http://example.com/path?querystring
  77. // --------------------------------------------------------------------------------
  78. // HTTP/1.1 200 OK
  79. // Content-Length: 16
  80. // Date: Tue, 15 Nov 1994 08:12:31 GMT
  81. // Other-Header: values
  82. //
  83. // response content
  84. // --------------------------------------------------------------------------------
  85. }
  86. func TestLoggerFromJSON(t *testing.T) {
  87. msg := []byte(`{
  88. "log.Logger": {
  89. "scope": ["request", "response"],
  90. "headersOnly": true,
  91. "decode": true
  92. }
  93. }`)
  94. r, err := parse.FromJSON(msg)
  95. if err != nil {
  96. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  97. }
  98. reqmod := r.RequestModifier()
  99. if reqmod == nil {
  100. t.Fatal("r.RequestModifier(): got nil, want not nil")
  101. }
  102. if _, ok := reqmod.(*Logger); !ok {
  103. t.Error("reqmod.(*Logger): got !ok, want ok")
  104. }
  105. resmod := r.ResponseModifier()
  106. if resmod == nil {
  107. t.Fatal("r.ResponseModifier(): got nil, want not nil")
  108. }
  109. l, ok := resmod.(*Logger)
  110. if !ok {
  111. t.Error("resmod.(*Logger); got !ok, want ok")
  112. }
  113. if !l.headersOnly {
  114. t.Error("l.headersOnly: got false, want true")
  115. }
  116. if !l.decode {
  117. t.Error("l.decode: got false, want true")
  118. }
  119. }