Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Oct 12, 2023
1 parent 5583ff7 commit 6ad4406
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestHTTPRouting(t *testing.T) {
us := testutil.NewUpstreamServer(t, u.hostname)
m[u.hostname] = us.URL + u.rootPath
}
r := testutil.NewRelayer(m)
r := testutil.NewSimpleRelayer(m)
port, err := testutil.NewPort()
if err != nil {
t.Fatal(err)
Expand Down
30 changes: 30 additions & 0 deletions testutil/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -50,3 +51,32 @@ func (r *Relayer) GetCertificate(i *tls.ClientHelloInfo) (*tls.Certificate, erro
}
return &c, nil
}

func (r *Relayer) Rewrite(*httputil.ProxyRequest) error {
return nil
}

type SimpleRelayer struct {
h map[string]string
}

func NewSimpleRelayer(h map[string]string) *SimpleRelayer {
return &SimpleRelayer{
h: h,
}
}

func (r *SimpleRelayer) GetUpstream(req *http.Request) (*url.URL, error) {
host := req.Host
if upstream, ok := r.h[host]; ok {
uu, err := url.Parse(upstream)
if err != nil {
return nil, err
}
req.URL.Scheme = uu.Scheme
req.URL.Host = uu.Host
req.URL.Path = strings.ReplaceAll(path.Join(uu.Path, req.URL.Path), "//", "/")
return req.URL, nil
}
return nil, fmt.Errorf("not found upstream: %v", host)
}

0 comments on commit 6ad4406

Please sign in to comment.