carrier_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package trace
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "git.i2edu.net/i2/go-zero/core/stringx"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestHttpCarrier(t *testing.T) {
  10. tests := []map[string]string{
  11. {},
  12. {
  13. "first": "a",
  14. "second": "b",
  15. },
  16. }
  17. for _, test := range tests {
  18. t.Run(stringx.RandId(), func(t *testing.T) {
  19. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  20. carrier := httpCarrier(req.Header)
  21. for k, v := range test {
  22. carrier.Set(k, v)
  23. }
  24. for k, v := range test {
  25. assert.Equal(t, v, carrier.Get(k))
  26. }
  27. assert.Equal(t, "", carrier.Get("none"))
  28. })
  29. }
  30. }
  31. func TestGrpcCarrier(t *testing.T) {
  32. tests := []map[string]string{
  33. {},
  34. {
  35. "first": "a",
  36. "second": "b",
  37. },
  38. }
  39. for _, test := range tests {
  40. t.Run(stringx.RandId(), func(t *testing.T) {
  41. m := make(map[string][]string)
  42. carrier := grpcCarrier(m)
  43. for k, v := range test {
  44. carrier.Set(k, v)
  45. }
  46. for k, v := range test {
  47. assert.Equal(t, v, carrier.Get(k))
  48. }
  49. assert.Equal(t, "", carrier.Get("none"))
  50. })
  51. }
  52. }