From b2c72449191f417acc894bd38ebecc52373fd019 Mon Sep 17 00:00:00 2001 From: Ehsan Noureddin Moosa Date: Wed, 22 Nov 2023 08:25:40 +0300 Subject: [PATCH] [kit] add helper method http stub --- kit/stub/stub.go | 26 ++++++++++++++++++++++++++ kit/stub/stub_rest.go | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/kit/stub/stub.go b/kit/stub/stub.go index 42ec28c4..fddc484b 100644 --- a/kit/stub/stub.go +++ b/kit/stub/stub.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "fmt" "net/http" + "net/url" "runtime" "time" @@ -50,6 +51,31 @@ func New(hostPort string, opts ...Option) *Stub { } } +func HTTP(rawURL string, opts ...Option) (*RESTCtx, error) { + u, err := url.ParseRequestURI(rawURL) + if err != nil { + return nil, err + } + + switch u.Scheme { + default: + return nil, fmt.Errorf("unsupported scheme: %s", u.Scheme) + case "http": + case "https": + opts = append(opts, Secure()) + } + + s := New(u.Host, opts...).REST() + s.SetPath(u.Path) + for k, v := range u.Query() { + for _, vv := range v { + s.AppendQuery(k, vv) + } + } + + return s, nil +} + func (s *Stub) REST(opt ...RESTOption) *RESTCtx { ctx := &RESTCtx{ c: &s.httpC, diff --git a/kit/stub/stub_rest.go b/kit/stub/stub_rest.go index 45e1a904..ed99c0f1 100644 --- a/kit/stub/stub_rest.go +++ b/kit/stub/stub_rest.go @@ -95,6 +95,12 @@ func (hc *RESTCtx) SetQuery(key, value string) *RESTCtx { return hc } +func (hc *RESTCtx) AppendQuery(key, value string) *RESTCtx { + hc.args.Add(key, value) + + return hc +} + func (hc *RESTCtx) SetQueryMap(kv map[string]string) *RESTCtx { for k, v := range kv { hc.args.Set(k, v)