Skip to content

Commit

Permalink
proxy test
Browse files Browse the repository at this point in the history
  • Loading branch information
recallsong committed Aug 30, 2021
1 parent d5bcd89 commit eac33b6
Showing 1 changed file with 161 additions and 0 deletions.
161 changes: 161 additions & 0 deletions modules/core/openapi-ng/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
package proxy

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"

"github.com/erda-project/erda-infra/base/logs/logrusx"
discover "github.com/erda-project/erda/providers/service-discover"
)

func Test_buildPathToSegments(t *testing.T) {
Expand Down Expand Up @@ -125,3 +132,157 @@ func Test_buildPathToSegments(t *testing.T) {
})
}
}

type testDiscover struct {
addr string
err error
}

func (d *testDiscover) Endpoint(scheme, service string) (string, error) {
if d.err != nil {
return "", d.err
}
return d.addr, nil
}
func (d *testDiscover) ServiceURL(scheme, service string) (string, error) {
if d.err != nil {
return "", d.err
}
return fmt.Sprintf("%s://%s", scheme, d.addr), nil
}

func Test_Proxy(t *testing.T) {
logger := logrusx.New()
tests := []struct {
name string
method string
path string
backendPath string
wrapers []func(h http.HandlerFunc) http.HandlerFunc
reqPath string
backendRecvPath string
wrapError bool
pathError bool
}{
{
method: "GET",
path: "/abc/def",
backendPath: "/abc/def",
reqPath: "/abc/def",
backendRecvPath: "/abc/def",
},
{
method: "GET",
path: "/api/abc/def",
backendPath: "/abc/def",
reqPath: "/api/abc/def",
backendRecvPath: "/abc/def",
},
{
method: "GET",
path: "/api/abc/{name}",
backendPath: "/abc/{name}",
reqPath: "/api/abc/def",
backendRecvPath: "/abc/def",
},
{
method: "GET",
path: "/api/abc/{name}",
backendPath: "/abc/{name}",
reqPath: "/api/abc/def",
backendRecvPath: "/abc/xxxx",
pathError: true,
},
{
method: "GET",
path: "/api/abc/{name}/{name2}",
backendPath: "/abc/{name}",
reqPath: "/api/abc/def/g",
backendRecvPath: "/abc/def",
},
{
method: "GET",
path: "/api/abc/{name}",
backendPath: "/abc/{name}/{name2}",
wrapError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != tt.method {
t.Errorf("unexpected method %q", r.Method)
}
if r.URL.Path != tt.backendRecvPath && !tt.pathError {
t.Errorf("unexpected backend path %q, want path %q", r.URL.Path, tt.backendRecvPath)
} else if r.URL.Path == tt.backendRecvPath && tt.pathError {
t.Errorf("want path error, but got path %q", r.URL.Path)
}
}))
defer backend.Close()
backendURL, err := url.Parse(backend.URL)
if err != nil {
t.Fatal(err)
}

p := Proxy{
Log: logger,
Discover: &testDiscover{addr: backendURL.Host},
}
handler, err := p.Wrap(tt.method, tt.path, tt.backendPath, "", tt.wrapers...)
if err != nil {
if !tt.wrapError {
t.Fatalf("Proxy.Wrap() got error: %v", err)
}
return
} else if tt.wrapError && err == nil {
t.Fatalf("Proxy.Wrap() want error, but got nil error")
return
}

frontend := httptest.NewServer(handler)
defer frontend.Close()

getReq, _ := http.NewRequest(tt.method, frontend.URL+tt.reqPath, nil)
getReq.Host = "some-name"
getReq.Header.Set("Connection", "close")
getReq.Close = true
_, err = frontend.Client().Do(getReq)
if err != nil {
t.Fatalf("Get: %v", err)
}
})
}
}

func Test_Discover(t *testing.T) {
logger := logrusx.New()
tests := []struct {
name string
discover discover.Interface
wantError bool
}{
{
wantError: false,
discover: &testDiscover{addr: "localhost:8080"},
},
{
wantError: true,
discover: &testDiscover{err: fmt.Errorf("test discover error")},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := Proxy{
Log: logger,
Discover: tt.discover,
}
_, err := p.Wrap("GET", "/", "/", "")
if !tt.wantError && err != nil {
t.Fatalf("Proxy.Wrap() got error: %v", err)
} else if tt.wantError && err == nil {
t.Fatalf("Proxy.Wrap() want error, but got nil error")
}
})
}
}

0 comments on commit eac33b6

Please sign in to comment.