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.
 
 
 

71 lines
2.1 KiB

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