From e712e7f3b1c81b150efd95de886778458be8073a Mon Sep 17 00:00:00 2001 From: anirut Date: Thu, 1 Sep 2022 00:36:30 +0700 Subject: [PATCH 1/4] :zap: add headers option --- transport/http/calloption.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/transport/http/calloption.go b/transport/http/calloption.go index 7b0ed82d1..0b245079a 100644 --- a/transport/http/calloption.go +++ b/transport/http/calloption.go @@ -20,6 +20,7 @@ type callInfo struct { contentType string operation string pathTemplate string + headers map[string]string } // EmptyCallOption does not alter the Call configuration. @@ -55,6 +56,7 @@ func defaultCallInfo(path string) callInfo { contentType: "application/json", operation: path, pathTemplate: path, + headers: make(map[string]string), } } @@ -107,3 +109,20 @@ func (o HeaderCallOption) after(c *callInfo, cs *csAttempt) { *o.header = cs.res.Header } } + +// Headers returns a CallOptions that pass the http request header +// to server . +func Headers(headers map[string]string) CallOption { + return HeadersCallOption{headers: headers} +} + +// HeaderCallOption is http headers that want to set before call the client +type HeadersCallOption struct { + EmptyCallOption + headers map[string]string +} + +func (o HeadersCallOption) before(c *callInfo) error { + c.headers = o.headers + return nil +} From ee56ff4e2797aa6cf2f2215d7c8cf25ca0d417d5 Mon Sep 17 00:00:00 2001 From: anirut Date: Thu, 1 Sep 2022 00:36:53 +0700 Subject: [PATCH 2/4] :white_check_mark: add headers testing --- transport/http/calloption_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/transport/http/calloption_test.go b/transport/http/calloption_test.go index 4fe6f3a58..39be878d3 100644 --- a/transport/http/calloption_test.go +++ b/transport/http/calloption_test.go @@ -96,3 +96,30 @@ func TestHeaderCallOption_after(t *testing.T) { t.Errorf("want: %v,got: %v", &h, o.(HeaderCallOption).header) } } + +func TestHeaders(t *testing.T) { + if !reflect.DeepEqual(map[string]string{ + "trace_id": "xxxx", + }, Headers(map[string]string{ + "trace_id": "xxxx", + }).(HeadersCallOption).headers) { + t.Errorf("want: %v,got: %v", "{'trace_id': 'xxxx'}", Headers(map[string]string{ + "trace_id": "xxxx", + }).(HeadersCallOption).headers) + } +} + +func TestHeadersCallOption_before(t *testing.T) { + c := &callInfo{} + err := Headers(map[string]string{ + "trace_id": "xxxx", + }).before(c) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(map[string]string{ + "trace_id": "xxxx", + }, c.headers) { + t.Errorf("want: %v, got: %v", "{'trace_id': 'xxxx'}", c.headers) + } +} From fb46db740d2a06d2ec9251e9b12b6f975c405e3b Mon Sep 17 00:00:00 2001 From: anirut Date: Thu, 1 Sep 2022 00:37:18 +0700 Subject: [PATCH 3/4] :zap: add headers before invoke --- transport/http/client.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/transport/http/client.go b/transport/http/client.go index c0ad5ee38..29e16976e 100644 --- a/transport/http/client.go +++ b/transport/http/client.go @@ -221,6 +221,9 @@ func (client *Client) Invoke(ctx context.Context, method, path string, args inte if err != nil { return err } + for key, value := range c.headers { + req.Header.Set(key, value) + } if contentType != "" { req.Header.Set("Content-Type", c.contentType) } From 831132dbd4e01ca5fa5ff1e8ea717d093f7fde65 Mon Sep 17 00:00:00 2001 From: anirut Date: Thu, 1 Sep 2022 00:37:34 +0700 Subject: [PATCH 4/4] :white_check_mark: add client test --- transport/http/client_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/transport/http/client_test.go b/transport/http/client_test.go index 4ee00456f..2da7dc0ca 100644 --- a/transport/http/client_test.go +++ b/transport/http/client_test.go @@ -363,4 +363,11 @@ func TestNewClient(t *testing.T) { if err == nil { t.Error("err should be equal to encoder error") } + headersCallOpt := Headers(map[string]string{ + "trace_id": "xxxx", + }) + err = client.Invoke(context.Background(), "POST", "/go", map[string]string{"name": "kratos"}, nil, headersCallOpt) + if err == nil { + t.Error("err should be equal to encoder error") + } }