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:
committed by
GitHub
parent
421dbc7dae
commit
1e4e37ad87
@@ -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
|
||||
|
@@ -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{}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user