trace_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package trace
  5. import (
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. type s struct{}
  11. func (s) String() string { return "lazy string" }
  12. // TestReset checks whether all the fields are zeroed after reset.
  13. func TestReset(t *testing.T) {
  14. tr := New("foo", "bar")
  15. tr.LazyLog(s{}, false)
  16. tr.LazyPrintf("%d", 1)
  17. tr.SetRecycler(func(_ interface{}) {})
  18. tr.SetTraceInfo(3, 4)
  19. tr.SetMaxEvents(100)
  20. tr.SetError()
  21. tr.Finish()
  22. tr.(*trace).reset()
  23. if !reflect.DeepEqual(tr, new(trace)) {
  24. t.Errorf("reset didn't clear all fields: %+v", tr)
  25. }
  26. }
  27. // TestResetLog checks whether all the fields are zeroed after reset.
  28. func TestResetLog(t *testing.T) {
  29. el := NewEventLog("foo", "bar")
  30. el.Printf("message")
  31. el.Errorf("error")
  32. el.Finish()
  33. el.(*eventLog).reset()
  34. if !reflect.DeepEqual(el, new(eventLog)) {
  35. t.Errorf("reset didn't clear all fields: %+v", el)
  36. }
  37. }
  38. func TestAuthRequest(t *testing.T) {
  39. testCases := []struct {
  40. host string
  41. want bool
  42. }{
  43. {host: "192.168.23.1", want: false},
  44. {host: "192.168.23.1:8080", want: false},
  45. {host: "malformed remote addr", want: false},
  46. {host: "localhost", want: true},
  47. {host: "localhost:8080", want: true},
  48. {host: "127.0.0.1", want: true},
  49. {host: "127.0.0.1:8080", want: true},
  50. {host: "::1", want: true},
  51. {host: "[::1]:8080", want: true},
  52. }
  53. for _, tt := range testCases {
  54. req := &http.Request{RemoteAddr: tt.host}
  55. any, sensitive := AuthRequest(req)
  56. if any != tt.want || sensitive != tt.want {
  57. t.Errorf("AuthRequest(%q) = %t, %t; want %t, %t", tt.host, any, sensitive, tt.want, tt.want)
  58. }
  59. }
  60. }