Repository: go-kratos/kratos
Pull request #3038 (2023-10-16 13:37:45)

refactor: support adding headers to before calloption header
Adding capability to pass headers to generated transport http code, by allowing before to add header carrier to calloption and using it on the client side.

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/go-kratos/kratos/pull/3038?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#3038](https://app.codecov.io/gh/go-kratos/kratos/pull/3038?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (9e44d7f) into [main](https://app.codecov.io/gh/go-kratos/kratos/commit/6c026bce56a9e388006cb97e99036c0a043ae95d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (6c026bc) will **decrease** coverage by `0.04%`.
> The diff coverage is `60.00%`.

> :exclamation: Current head 9e44d7f differs from pull request most recent head 7c28676. Consider uploading reports for the commit 7c28676 to get more accurate results

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

```diff
@@            Coverage Diff             @@
##             main    #3038      +/-   ##
==========================================
- Coverage   84.34%   84.31%   -0.04%     
==========================================
  Files          88       88              
  Lines        3986     3991       +5     
==========================================
+ Hits         3362     3365       +3     
- Misses        448      449       +1     
- Partials      176      177       +1     
```


| [Files](https://app.codecov.io/gh/go-kratos/kratos/pull/3038?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) | Coverage Δ | |
|---|---|---|
| [transport/http/calloption.go](https://app.codecov.io/gh/go-kratos/kratos/pull/3038?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-dHJhbnNwb3J0L2h0dHAvY2FsbG9wdGlvbi5nbw==) | `100.00% <100.00%> (ø)` | |
| [transport/http/client.go](https://app.codecov.io/gh/go-kratos/kratos/pull/3038?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-dHJhbnNwb3J0L2h0dHAvY2xpZW50Lmdv) | `83.33% <0.00%> (-0.88%)` | :arrow_down: |



Patch:
diff --git a/transport/http/calloption.go b/transport/http/calloption.go
index e8329410..921e18ac 100644
--- a/transport/http/calloption.go
+++ b/transport/http/calloption.go
@@ -17,9 +17,10 @@ type CallOption interface {
 }
 
 type callInfo struct {
-	contentType  string
-	operation    string
-	pathTemplate string
+	contentType   string
+	operation     string
+	pathTemplate  string
+	headerCarrier *http.Header
 }
 
 // EmptyCallOption does not alter the Call configuration.
@@ -102,6 +103,11 @@ type HeaderCallOption struct {
 	header *http.Header
 }
 
+func (o HeaderCallOption) before(c *callInfo) error {
+	c.headerCarrier = o.header
+	return nil
+}
+
 func (o HeaderCallOption) after(_ *callInfo, cs *csAttempt) {
 	if cs.res != nil && cs.res.Header != nil {
 		*o.header = cs.res.Header
diff --git a/transport/http/client.go b/transport/http/client.go
index 2d2a352c..7784a1c2 100644
--- a/transport/http/client.go
+++ b/transport/http/client.go
@@ -231,6 +231,10 @@ func (client *Client) Invoke(ctx context.Context, method, path string, args inte
 	if err != nil {
 		return err
 	}
+	if c.headerCarrier != nil {
+		req.Header = *c.headerCarrier
+	}
+
 	if contentType != "" {
 		req.Header.Set("Content-Type", c.contentType)
 	}


Test patch:
diff --git a/transport/http/calloption_test.go b/transport/http/calloption_test.go
index 4fe6f3a5..42c2dac0 100644
--- a/transport/http/calloption_test.go
+++ b/transport/http/calloption_test.go
@@ -86,6 +86,19 @@ func TestHeader(t *testing.T) {
 	}
 }
 
+func TestHeaderCallOption_before(t *testing.T) {
+	h := http.Header{"A": []string{"123"}}
+	c := &callInfo{}
+	o := Header(&h)
+	err := o.before(c)
+	if err != nil {
+		t.Errorf("unexpected error: %v", err)
+	}
+	if !reflect.DeepEqual(&h, c.headerCarrier) {
+		t.Errorf("want: %v,got: %v", &h, o.(HeaderCallOption).header)
+	}
+}
+
 func TestHeaderCallOption_after(t *testing.T) {
 	h := http.Header{"A": []string{"123"}}
 	c := &callInfo{}