Skip to content

Commit 7243e5e

Browse files
authored
Merge pull request #32 from easyops-cn/kestrel/schema
chore(): support https scheme refs MONITOR-6046
2 parents 3963017 + 64fbac2 commit 7243e5e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

plugins/restv2/client.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Client struct {
2525
Middleware Middleware
2626
NameService giraffe.NameService
2727
retryConf RetryConfig
28+
Scheme string
2829
}
2930

3031
// ClientOption Client 配置函数
@@ -111,6 +112,9 @@ func (c *Client) sendWithENS(req *http.Request, contract giraffe.Contract) (resp
111112
// 备份 http body
112113
var originalBody []byte
113114
req.URL.Scheme = "http"
115+
if c.Scheme != "" {
116+
req.URL.Scheme = c.Scheme
117+
}
114118
if req != nil && req.Body != nil {
115119
originalBody, _ = copyBody(req)
116120
}
@@ -185,6 +189,7 @@ func NewClient(opts ...ClientOption) *Client {
185189
Client: &http.Client{},
186190
Middleware: DefaultMiddleware,
187191
NameService: nil,
192+
Scheme: "http",
188193
}
189194
for _, o := range opts {
190195
o(c)
@@ -216,3 +221,9 @@ func WithRetryConfig(conf RetryConfig) ClientOption {
216221
c.retryConf.init()
217222
}
218223
}
224+
225+
func WithScheme(scheme string) ClientOption {
226+
return func(c *Client) {
227+
c.Scheme = scheme
228+
}
229+
}

plugins/restv2/client_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ func TestNewClient(t *testing.T) {
481481
retryConf: RetryConfig{
482482
RetryInterval: defaultWaitDuration,
483483
},
484+
Scheme: "http",
484485
},
485486
},
486487
}
@@ -658,3 +659,33 @@ func TestClient_sendWithENS_503_retry_no_retryAfter(t *testing.T) {
658659
_, _ = c.sendWithENS(req1, nil)
659660
})
660661
}
662+
663+
func TestWithScheme(t *testing.T) {
664+
type args struct {
665+
scheme string
666+
}
667+
tests := []struct {
668+
name string
669+
args args
670+
want string
671+
}{
672+
{
673+
name: "success",
674+
args: args{
675+
scheme: "https",
676+
},
677+
want: "https",
678+
},
679+
}
680+
for _, tt := range tests {
681+
t.Run(tt.name, func(t *testing.T) {
682+
got := WithScheme(tt.args.scheme)
683+
client := &Client{}
684+
got(client)
685+
686+
if !reflect.DeepEqual(client.Scheme, tt.want) {
687+
t.Errorf("WithScheme() = %v, want %v", client.Scheme, tt.want)
688+
}
689+
})
690+
}
691+
}

0 commit comments

Comments
 (0)