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.
 
 
 

73 lines
2.1 KiB

  1. // Copyright 2018 Google LLC
  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. // +build go1.8
  15. package propagation
  16. import (
  17. "net/http"
  18. "reflect"
  19. "testing"
  20. "go.opencensus.io/trace"
  21. )
  22. func TestHTTPFormat(t *testing.T) {
  23. format := &HTTPFormat{}
  24. traceID := [16]byte{16, 84, 69, 170, 120, 67, 188, 139, 242, 6, 177, 32, 0, 16, 0, 0}
  25. spanID1 := [8]byte{255, 0, 0, 0, 0, 0, 0, 123}
  26. spanID2 := [8]byte{0, 0, 0, 0, 0, 0, 0, 123}
  27. tests := []struct {
  28. incoming string
  29. wantSpanContext trace.SpanContext
  30. }{
  31. {
  32. incoming: "105445aa7843bc8bf206b12000100000/18374686479671623803;o=1",
  33. wantSpanContext: trace.SpanContext{
  34. TraceID: traceID,
  35. SpanID: spanID1,
  36. TraceOptions: 1,
  37. },
  38. },
  39. {
  40. incoming: "105445aa7843bc8bf206b12000100000/123;o=0",
  41. wantSpanContext: trace.SpanContext{
  42. TraceID: traceID,
  43. SpanID: spanID2,
  44. TraceOptions: 0,
  45. },
  46. },
  47. }
  48. for _, tt := range tests {
  49. t.Run(tt.incoming, func(t *testing.T) {
  50. req, _ := http.NewRequest("GET", "http://example.com", nil)
  51. req.Header.Add(httpHeader, tt.incoming)
  52. sc, ok := format.SpanContextFromRequest(req)
  53. if !ok {
  54. t.Errorf("exporter.SpanContextFromRequest() = false; want true")
  55. }
  56. if got, want := sc, tt.wantSpanContext; !reflect.DeepEqual(got, want) {
  57. t.Errorf("exporter.SpanContextFromRequest() returned span context %v; want %v", got, want)
  58. }
  59. req, _ = http.NewRequest("GET", "http://example.com", nil)
  60. format.SpanContextToRequest(sc, req)
  61. if got, want := req.Header.Get(httpHeader), tt.incoming; got != want {
  62. t.Errorf("exporter.SpanContextToRequest() returned header %q; want %q", got, want)
  63. }
  64. })
  65. }
  66. }