1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-09-16 09:16:35 +02:00

Support adding headers to before calloption header (#3038)

Co-authored-by: Ibrahim Barghouthi <ibrahim.barghouthi@cenomi.com>
This commit is contained in:
ibrahim albarghouthi
2023-11-16 12:09:54 +03:00
committed by GitHub
parent 421dbc7dae
commit 1e4e37ad87
3 changed files with 26 additions and 3 deletions

View File

@@ -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

View File

@@ -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{}

View File

@@ -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)
}