feat(header): add func Append to interface Header

pull/2293/head
zhanghu 2 years ago
parent 6ca225b078
commit 5da388485f
  1. 6
      middleware/auth/jwt/jwt_test.go
  2. 6
      middleware/metadata/metadata_test.go
  3. 21
      middleware/selector/selector_test.go
  4. 7
      middleware/tracing/tracing_test.go
  5. 5
      transport/grpc/transport.go
  6. 7
      transport/http/transport.go
  7. 1
      transport/transport.go

@ -24,6 +24,12 @@ func (hc headerCarrier) Get(key string) string { return http.Header(hc).Get(key)
func (hc headerCarrier) Set(key string, value string) { http.Header(hc).Set(key, value) }
func (hc headerCarrier) Append(key string, value ...string) {
for _, v := range value {
http.Header(hc).Add(key, v)
}
}
// Keys lists the keys stored in this carrier.
func (hc headerCarrier) Keys() []string {
keys := make([]string, 0, len(hc))

@ -16,6 +16,12 @@ func (hc headerCarrier) Get(key string) string { return http.Header(hc).Get(key)
func (hc headerCarrier) Set(key string, value string) { http.Header(hc).Set(key, value) }
func (hc headerCarrier) Append(key string, value ...string) {
for _, v := range value {
http.Header(hc).Add(key, v)
}
}
// Keys lists the keys stored in this carrier.
func (hc headerCarrier) Keys() []string {
keys := make([]string, 0, len(hc))

@ -40,15 +40,22 @@ func (tr *Transport) ReplyHeader() transport.Header {
}
type mockHeader struct {
m map[string]string
m map[string][]string
}
func (m *mockHeader) Get(key string) string {
return m.m[key]
if m.m[key] == nil {
return ""
}
return m.m[key][0]
}
func (m *mockHeader) Set(key, value string) {
m.m[key] = value
m.m[key] = []string{value}
}
func (m *mockHeader) Append(key string, value ...string) {
m.m[key] = append(m.m[key], value...)
}
func (m *mockHeader) Keys() []string {
@ -185,28 +192,28 @@ func TestHeaderFunc(t *testing.T) {
name: "/hello.Update/world",
ctx: transport.NewServerContext(context.Background(), &Transport{
operation: "/hello.Update/world",
headers: &mockHeader{map[string]string{"X-Test": "test"}},
headers: &mockHeader{map[string][]string{"X-Test": {"test"}}},
}),
},
{
name: "/hi.Create/world",
ctx: transport.NewServerContext(context.Background(), &Transport{
operation: "/hi.Create/world",
headers: &mockHeader{map[string]string{"X-Test": "test2", "go-kratos": "kratos"}},
headers: &mockHeader{map[string][]string{"X-Test": {"test2"}, "go-kratos": {"kratos"}}},
}),
},
{
name: "/test.Name/1234",
ctx: transport.NewServerContext(context.Background(), &Transport{
operation: "/test.Name/1234",
headers: &mockHeader{map[string]string{"X-Test": "test3"}},
headers: &mockHeader{map[string][]string{"X-Test": {"test3"}}},
}),
},
{
name: "/go-kratos.dev/kratos",
ctx: transport.NewServerContext(context.Background(), &Transport{
operation: "/go-kratos.dev/kratos",
headers: &mockHeader{map[string]string{"X-Test": "test"}},
headers: &mockHeader{map[string][]string{"X-Test": {"test"}}},
}),
},
}

@ -28,6 +28,13 @@ func (hc headerCarrier) Set(key string, value string) {
http.Header(hc).Set(key, value)
}
// Append appends the key-value pair.
func (hc headerCarrier) Append(key string, value ...string) {
for _, v := range value {
http.Header(hc).Add(key, v)
}
}
// Keys lists the keys stored in this carrier.
func (hc headerCarrier) Keys() []string {
keys := make([]string, 0, len(hc))

@ -63,6 +63,11 @@ func (mc headerCarrier) Set(key string, value string) {
metadata.MD(mc).Set(key, value)
}
// Append appends the key-value pair.
func (mc headerCarrier) Append(key string, value ...string) {
metadata.MD(mc).Append(key, value...)
}
// Keys lists the keys stored in this carrier.
func (mc headerCarrier) Keys() []string {
keys := make([]string, 0, len(mc))

@ -82,6 +82,13 @@ func (hc headerCarrier) Set(key string, value string) {
http.Header(hc).Set(key, value)
}
// Append appends the key-value pair.
func (hc headerCarrier) Append(key string, value ...string) {
for _, v := range value {
http.Header(hc).Add(key, v)
}
}
// Keys lists the keys stored in this carrier.
func (hc headerCarrier) Keys() []string {
keys := make([]string, 0, len(hc))

@ -27,6 +27,7 @@ type Endpointer interface {
type Header interface {
Get(key string) string
Set(key string, value string)
Append(key string, value ...string)
Keys() []string
}

Loading…
Cancel
Save