| 1 | // Copyright 2022 The OpenZipkin 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 | |
| 15 | package baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/ |
| 16 | |
| 17 | import ( |
| 18 | "stringsscip-go gomod github.com/golang/go/src go1.20 strings/" |
| 19 | "testingscip-go gomod github.com/golang/go/src go1.20 testing/" |
| 20 | ) |
| 21 | |
| 22 | func TestBaggageRegistryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/TestBaggageRegistry().(t *testingscip-go gomod github.com/golang/go/src go1.20 testing/.Tscip-go gomod github.com/golang/go/src go1.20 testing/T#) { |
| 23 | baggageHandler := Newscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/New().("X-Request-Id", "some-header", "x-request-id") |
| 24 | |
| 25 | var items int |
| 26 | baggageHandler.(*baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#).IterateKeysscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#IterateKeys().(func(key string) { |
| 27 | if key != "some-header" && key != "x-request-id" { |
| 28 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("Unexpected registry item: %s", key) |
| 29 | } |
| 30 | items++ |
| 31 | }) |
| 32 | |
| 33 | if items != 2 { |
| 34 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("Unexpected registration count: want %d, have %d", 2, items) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestBaggageValuesscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/TestBaggageValues().(t *testingscip-go gomod github.com/golang/go/src go1.20 testing/.Tscip-go gomod github.com/golang/go/src go1.20 testing/T#) { |
| 39 | // register the baggage fields we'll propagate |
| 40 | baggageHandler := Newscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/New().("X-Request-Id", "Some-Header") |
| 41 | |
| 42 | // initialize fresh BaggageFields container |
| 43 | baggage := baggageHandler.Newscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/middleware`/BaggageHandler#New.() |
| 44 | |
| 45 | t.Runscip-go gomod github.com/golang/go/src go1.20 testing/T#Run().("AddHeader", func(t *testingscip-go gomod github.com/golang/go/src go1.20 testing/.Tscip-go gomod github.com/golang/go/src go1.20 testing/T#) { |
| 46 | if baggage.Addscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields#Add.("Invalid-Key", "Invalid-Key-Value") { |
| 47 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected Invalid-Key to return false") |
| 48 | } |
| 49 | if !baggage.Addscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields#Add.("X-Request-Id", "X-Request-Id-Value") { |
| 50 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected X-Request-Id to return true") |
| 51 | } |
| 52 | if !baggage.Addscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields#Add.("Some-Header", "Some-Header-Value1", "Some-Header-Value2") { |
| 53 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected Some-Header to return true") |
| 54 | } |
| 55 | if !baggage.Addscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields#Add.("Some-Header", "Some-Header-Value3") { |
| 56 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected Some-Header to return true") |
| 57 | } |
| 58 | }) |
| 59 | |
| 60 | baggageHandler.Newscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/middleware`/BaggageHandler#New.().Iteratescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields#Iterate.(func(key string, values []string) { |
| 61 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected no header data to exist, have: key=%s values=%v", key, values) |
| 62 | }) |
| 63 | |
| 64 | t.Runscip-go gomod github.com/golang/go/src go1.20 testing/T#Run().("IterateHeaders", func(t *testingscip-go gomod github.com/golang/go/src go1.20 testing/.Tscip-go gomod github.com/golang/go/src go1.20 testing/T#) { |
| 65 | baggage.Iteratescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields#Iterate.(func(key string, have []string) { |
| 66 | if stringsscip-go gomod github.com/golang/go/src go1.20 strings/.EqualFoldscip-go gomod github.com/golang/go/src go1.20 strings/EqualFold().(key, "x-request-id") { |
| 67 | want := 1 |
| 68 | if len(have) != want { |
| 69 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected different value count: want %d, have %d", want, len(have)) |
| 70 | } |
| 71 | if have[0] != "X-Request-Id-Value" { |
| 72 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected different value: want %s, have %s", "X-Request-Id-Value", have[0]) |
| 73 | } |
| 74 | return |
| 75 | } |
| 76 | if stringsscip-go gomod github.com/golang/go/src go1.20 strings/.EqualFoldscip-go gomod github.com/golang/go/src go1.20 strings/EqualFold().(key, "some-header") { |
| 77 | want := 3 |
| 78 | if len(have) != want { |
| 79 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected different value count: want %d, have %d", want, len(have)) |
| 80 | } |
| 81 | wantVal := "Some-Header-Value1" |
| 82 | if have[0] != wantVal { |
| 83 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected different value: want %s, have %s", wantVal, have[0]) |
| 84 | } |
| 85 | wantVal = "Some-Header-Value2" |
| 86 | if have[1] != wantVal { |
| 87 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected different value: want %s, have %s", wantVal, have[1]) |
| 88 | } |
| 89 | wantVal = "Some-Header-Value3" |
| 90 | if have[2] != wantVal { |
| 91 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected different value: want %s, have %s", wantVal, have[2]) |
| 92 | } |
| 93 | return |
| 94 | } |
| 95 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("unexpected header key: %s", key) |
| 96 | }) |
| 97 | }) |
| 98 | |
| 99 | t.Runscip-go gomod github.com/golang/go/src go1.20 testing/T#Run().("DeleteHeader", func(t *testingscip-go gomod github.com/golang/go/src go1.20 testing/.Tscip-go gomod github.com/golang/go/src go1.20 testing/T#) { |
| 100 | if baggage.Deletescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields#Delete.("Invalid-Key") { |
| 101 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected Invalid-Key to return false") |
| 102 | } |
| 103 | |
| 104 | if !baggage.Deletescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields#Delete.("some-header") { |
| 105 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected some-header to return true") |
| 106 | } |
| 107 | |
| 108 | baggage.Iteratescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields#Iterate.(func(key string, have []string) { |
| 109 | if stringsscip-go gomod github.com/golang/go/src go1.20 strings/.EqualFoldscip-go gomod github.com/golang/go/src go1.20 strings/EqualFold().(key, "x-request-id") { |
| 110 | want := 1 |
| 111 | if len(have) != want { |
| 112 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected different value count: want %d, have %d", want, len(have)) |
| 113 | } |
| 114 | if have[0] != "X-Request-Id-Value" { |
| 115 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("expected different value: want %s, have %s", "X-Request-Id-Value", have[0]) |
| 116 | } |
| 117 | return |
| 118 | } |
| 119 | t.Errorfscip-go gomod github.com/golang/go/src go1.20 testing/common#Errorf().("unexpected header key: %s", key) |
| 120 | }) |
| 121 | }) |
| 122 | |
| 123 | } |