Skip to content

Commit

Permalink
[kit] add helper method http stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Nov 22, 2023
1 parent 0deb771 commit b2c7244
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
26 changes: 26 additions & 0 deletions kit/stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"runtime"
"time"

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions kit/stub/stub_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b2c7244

Please sign in to comment.