本文整理汇总了Golang中github.com/google/martian/martiantest.NewModifier函数的典型用法代码示例。如果您正苦于以下问题:Golang NewModifier函数的具体用法?Golang NewModifier怎么用?Golang NewModifier使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewModifier函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestPriorityGroupModifyRequest
func TestPriorityGroupModifyRequest(t *testing.T) {
var order []string
pg := NewGroup()
tm50 := martiantest.NewModifier()
tm50.RequestFunc(func(*http.Request) {
order = append(order, "tm50")
})
pg.AddRequestModifier(tm50, 50)
tm100a := martiantest.NewModifier()
tm100a.RequestFunc(func(*http.Request) {
order = append(order, "tm100a")
})
pg.AddRequestModifier(tm100a, 100)
tm100b := martiantest.NewModifier()
tm100b.RequestFunc(func(*http.Request) {
order = append(order, "tm100b")
})
pg.AddRequestModifier(tm100b, 100)
tm75 := martiantest.NewModifier()
tm75.RequestFunc(func(*http.Request) {
order = append(order, "tm75")
})
if err := pg.RemoveRequestModifier(tm75); err != ErrModifierNotFound {
t.Fatalf("RemoveRequestModifier(): got %v, want ErrModifierNotFound", err)
}
pg.AddRequestModifier(tm75, 100)
if err := pg.RemoveRequestModifier(tm75); err != nil {
t.Fatalf("RemoveRequestModifier(): got %v, want no error", err)
}
req, err := http.NewRequest("GET", "http://example.com/", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
if err := pg.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if got, want := order, []string{"tm100b", "tm100a", "tm50"}; !reflect.DeepEqual(got, want) {
t.Fatalf("reflect.DeepEqual(%v, %v): got false, want true", got, want)
}
}
示例2: TestModifyRequest
func TestModifyRequest(t *testing.T) {
f := NewFilter()
tm := martiantest.NewModifier()
f.SetRequestModifier("id", tm)
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("NewRequest(): got %v, want no error", err)
}
// No ID, auth required.
f.SetAuthRequired(true)
ctx := session.FromContext(nil)
martian.SetContext(req, ctx)
defer martian.RemoveContext(req)
if err := f.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
actx := FromContext(ctx)
if actx.Error() == nil {
t.Error("actx.Error(): got nil, want error")
}
if tm.RequestModified() {
t.Error("tm.RequestModified(): got true, want false")
}
tm.Reset()
// No ID, auth not required.
f.SetAuthRequired(false)
actx.SetError(nil)
if err := f.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if actx.Error() != nil {
t.Errorf("actx.Error(): got %v, want no error", err)
}
if tm.RequestModified() {
t.Error("tm.RequestModified(): got true, want false")
}
// Valid ID.
actx.SetError(nil)
actx.SetID("id")
if err := f.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if actx.Error() != nil {
t.Errorf("actx.Error(): got %v, want no error", actx.Error())
}
if !tm.RequestModified() {
t.Error("tm.RequestModified(): got false, want true")
}
}
示例3: TestResultModifierAccessors
func TestResultModifierAccessors(t *testing.T) {
tm := martiantest.NewModifier()
r := &Result{
reqmod: tm,
resmod: nil,
}
if reqmod := r.RequestModifier(); reqmod == nil {
t.Error("r.RequestModifier: got nil, want reqmod")
}
if resmod := r.ResponseModifier(); resmod != nil {
t.Error("r.ResponseModifier: got resmod, want nil")
}
r = &Result{
reqmod: nil,
resmod: tm,
}
if reqmod := r.RequestModifier(); reqmod != nil {
t.Errorf("r.RequestModifier: got reqmod, want nil")
}
if resmod := r.ResponseModifier(); resmod == nil {
t.Error("r.ResponseModifier: got nil, want resmod")
}
}
示例4: TestModifyRequest
func TestModifyRequest(t *testing.T) {
m := NewModifier()
m.SetRequestModifier(nil)
req, err := http.NewRequest("CONNECT", "https://www.example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
ctx := session.FromContext(nil)
martian.SetContext(req, ctx)
defer martian.RemoveContext(req)
if err := m.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
actx := auth.FromContext(ctx)
if got, want := actx.ID(), ""; got != want {
t.Errorf("actx.ID(): got %q, want %q", got, want)
}
// IP with port and modifier with error.
tm := martiantest.NewModifier()
reqerr := errors.New("request error")
tm.RequestError(reqerr)
req.RemoteAddr = "1.1.1.1:8111"
m.SetRequestModifier(tm)
if err := m.ModifyRequest(req); err != reqerr {
t.Fatalf("ModifyConnectRequest(): got %v, want %v", err, reqerr)
}
if got, want := actx.ID(), "1.1.1.1"; got != want {
t.Errorf("actx.ID(): got %q, want %q", got, want)
}
// IP without port and modifier with auth error.
req.RemoteAddr = "4.4.4.4"
autherr := errors.New("auth error")
tm.RequestError(nil)
tm.RequestFunc(func(req *http.Request) {
ctx := martian.Context(req)
actx := auth.FromContext(ctx)
actx.SetError(autherr)
})
if err := m.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if got, want := actx.ID(), ""; got != want {
t.Errorf("actx.ID(): got %q, want %q", got, want)
}
}
示例5: TestPriorityGroupModifyResponse
func TestPriorityGroupModifyResponse(t *testing.T) {
var order []string
pg := NewGroup()
tm50 := martiantest.NewModifier()
tm50.ResponseFunc(func(*http.Response) {
order = append(order, "tm50")
})
pg.AddResponseModifier(tm50, 50)
tm100a := martiantest.NewModifier()
tm100a.ResponseFunc(func(*http.Response) {
order = append(order, "tm100a")
})
pg.AddResponseModifier(tm100a, 100)
tm100b := martiantest.NewModifier()
tm100b.ResponseFunc(func(*http.Response) {
order = append(order, "tm100b")
})
pg.AddResponseModifier(tm100b, 100)
tm75 := martiantest.NewModifier()
tm75.ResponseFunc(func(*http.Response) {
order = append(order, "tm75")
})
if err := pg.RemoveResponseModifier(tm75); err != ErrModifierNotFound {
t.Fatalf("RemoveResponseModifier(): got %v, want ErrModifierNotFound", err)
}
pg.AddResponseModifier(tm75, 100)
if err := pg.RemoveResponseModifier(tm75); err != nil {
t.Fatalf("RemoveResponseModifier(): got %v, want no error", err)
}
res := proxyutil.NewResponse(200, nil, nil)
if err := pg.ModifyResponse(res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if got, want := order, []string{"tm100b", "tm100a", "tm50"}; !reflect.DeepEqual(got, want) {
t.Fatalf("reflect.DeepEqual(%v, %v): got false, want true", got, want)
}
}
示例6: TestProxyAuth
func TestProxyAuth(t *testing.T) {
m := NewModifier()
tm := martiantest.NewModifier()
m.SetRequestModifier(tm)
m.SetResponseModifier(tm)
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
req.Header.Set("Proxy-Authorization", "Basic "+encode("user:pass"))
ctx, err := session.FromContext(nil)
if err != nil {
t.Fatalf("session.FromContext(): got %v, want no error", err)
}
martian.SetContext(req, ctx)
defer martian.RemoveContext(req)
if err := m.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
actx := auth.FromContext(ctx)
if got, want := actx.ID(), "user:pass"; got != want {
t.Fatalf("actx.ID(): got %q, want %q", got, want)
}
if err := m.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if !tm.RequestModified() {
t.Error("tm.RequestModified(): got false, want true")
}
res := proxyutil.NewResponse(200, nil, req)
if err := m.ModifyResponse(res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if err := m.ModifyResponse(res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if !tm.ResponseModified() {
t.Error("tm.ResponseModified(): got false, want true")
}
if got, want := res.StatusCode, 200; got != want {
t.Errorf("res.StatusCode: got %d, want %d", got, want)
}
if got, want := res.Header.Get("Proxy-Authenticate"), ""; got != want {
t.Errorf("res.Header.Get(%q): got %q, want %q", "Proxy-Authenticate", got, want)
}
}
示例7: TestIntegrationHTTPDownstreamProxyError
func TestIntegrationHTTPDownstreamProxyError(t *testing.T) {
t.Parallel()
l, err := net.Listen("tcp", "[::1]:0")
if err != nil {
t.Fatalf("net.Listen(): got %v, want no error", err)
}
p := NewProxy()
defer p.Close()
// Set proxy's downstream proxy to invalid host:port to force failure.
p.SetDownstreamProxy(&url.URL{
Host: "[::1]:0",
})
p.SetTimeout(600 * time.Millisecond)
tm := martiantest.NewModifier()
reserr := errors.New("response error")
tm.ResponseError(reserr)
p.SetResponseModifier(tm)
go p.Serve(l)
// Open connection to upstream proxy.
conn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatalf("net.Dial(): got %v, want no error", err)
}
defer conn.Close()
req, err := http.NewRequest("CONNECT", "//example.com:443", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
// CONNECT example.com:443 HTTP/1.1
// Host: example.com
if err := req.Write(conn); err != nil {
t.Fatalf("req.Write(): got %v, want no error", err)
}
// Response from upstream proxy, assuming downstream proxy failed to CONNECT.
res, err := http.ReadResponse(bufio.NewReader(conn), req)
if err != nil {
t.Fatalf("http.ReadResponse(): got %v, want no error", err)
}
if got, want := res.StatusCode, 502; got != want {
t.Fatalf("res.StatusCode: got %d, want %d", got, want)
}
if got, want := res.Header["Warning"][1], reserr.Error(); !strings.Contains(got, want) {
t.Errorf("res.Header.get(%q): got %q, want to contain %q", "Warning", got, want)
}
}
示例8: TestModifyResponse
func TestModifyResponse(t *testing.T) {
m := NewModifier()
m.SetResponseModifier(nil)
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
ctx, err := session.FromContext(nil)
if err != nil {
t.Fatalf("session.FromContext(): got %v, want no error", err)
}
martian.SetContext(req, ctx)
defer martian.RemoveContext(req)
res := proxyutil.NewResponse(200, nil, req)
if err := m.ModifyResponse(res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
// Modifier with error.
tm := martiantest.NewModifier()
reserr := errors.New("response error")
tm.ResponseError(reserr)
m.SetResponseModifier(tm)
if err := m.ModifyResponse(res); err != reserr {
t.Fatalf("ModifyResponse(): got %v, want %v", err, reserr)
}
// Modifier with auth error.
tm.ResponseError(nil)
autherr := errors.New("auth error")
tm.ResponseFunc(func(res *http.Response) {
ctx := martian.Context(res.Request)
actx := auth.FromContext(ctx)
actx.SetError(autherr)
})
actx := auth.FromContext(ctx)
actx.SetID("bad-auth")
if err := m.ModifyResponse(res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if got, want := res.StatusCode, 403; got != want {
t.Errorf("res.StatusCode: got %d, want %d", got, want)
}
if got, want := actx.Error(), autherr; got != want {
t.Errorf("actx.Error(): got %v, want %v", got, want)
}
}
示例9: TestModifyResponse
func TestModifyResponse(t *testing.T) {
f := NewFilter()
tm := martiantest.NewModifier()
f.SetResponseModifier("id", tm)
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
res := proxyutil.NewResponse(200, nil, req)
// No ID, auth required.
f.SetAuthRequired(true)
ctx, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
if err := f.ModifyResponse(res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
actx := FromContext(ctx)
if actx.Error() == nil {
t.Error("actx.Error(): got nil, want error")
}
if tm.ResponseModified() {
t.Error("tm.RequestModified(): got true, want false")
}
// No ID, no auth required.
f.SetAuthRequired(false)
actx.SetError(nil)
if err := f.ModifyResponse(res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if tm.ResponseModified() {
t.Error("tm.ResponseModified(): got true, want false")
}
// Valid ID.
actx.SetID("id")
if err := f.ModifyResponse(res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if !tm.ResponseModified() {
t.Error("tm.ResponseModified(): got false, want true")
}
}
示例10: TestIntegrationSkipRoundTrip
func TestIntegrationSkipRoundTrip(t *testing.T) {
t.Parallel()
l, err := net.Listen("tcp", "[::1]:0")
if err != nil {
t.Fatalf("net.Listen(): got %v, want no error", err)
}
p := NewProxy()
defer p.Close()
// Transport will be skipped, no 500.
tr := martiantest.NewTransport()
tr.Respond(500)
p.SetRoundTripper(tr)
p.SetTimeout(200 * time.Millisecond)
tm := martiantest.NewModifier()
tm.RequestFunc(func(req *http.Request) {
ctx := Context(req)
ctx.SkipRoundTrip()
})
p.SetRequestModifier(tm)
go p.Serve(l)
conn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatalf("net.Dial(): got %v, want no error", err)
}
defer conn.Close()
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
// GET http://example.com/ HTTP/1.1
// Host: example.com
if err := req.WriteProxy(conn); err != nil {
t.Fatalf("req.WriteProxy(): got %v, want no error", err)
}
// Response from skipped round trip.
res, err := http.ReadResponse(bufio.NewReader(conn), req)
if err != nil {
t.Fatalf("http.ReadResponse(): got %v, want no error", err)
}
defer res.Body.Close()
if got, want := res.StatusCode, 200; got != want {
t.Errorf("res.StatusCode: got %d, want %d", got, want)
}
}
示例11: TestModifyResponseHaltsOnError
func TestModifyResponseHaltsOnError(t *testing.T) {
fg := NewGroup()
reserr := errors.New("request error")
tm := martiantest.NewModifier()
tm.ResponseError(reserr)
fg.AddResponseModifier(tm)
tm2 := martiantest.NewModifier()
fg.AddResponseModifier(tm2)
res := proxyutil.NewResponse(200, nil, nil)
if err := fg.ModifyResponse(res); err != reserr {
t.Fatalf("fg.ModifyResponse(): got %v, want %v", err, reserr)
}
if tm2.ResponseModified() {
t.Error("tm2.ResponseModified(): got true, want false")
}
}
示例12: TestIntegrationTransparentHTTP
func TestIntegrationTransparentHTTP(t *testing.T) {
t.Parallel()
l, err := net.Listen("tcp", "[::1]:0")
if err != nil {
t.Fatalf("net.Listen(): got %v, want no error", err)
}
p := NewProxy()
defer p.Close()
tr := martiantest.NewTransport()
p.SetRoundTripper(tr)
p.SetTimeout(200 * time.Millisecond)
tm := martiantest.NewModifier()
p.SetRequestModifier(tm)
p.SetResponseModifier(tm)
go p.Serve(l)
conn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatalf("net.Dial(): got %v, want no error", err)
}
defer conn.Close()
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
// GET / HTTP/1.1
// Host: www.example.com
if err := req.Write(conn); err != nil {
t.Fatalf("req.Write(): got %v, want no error", err)
}
res, err := http.ReadResponse(bufio.NewReader(conn), req)
if err != nil {
t.Fatalf("http.ReadResponse(): got %v, want no error", err)
}
if got, want := res.StatusCode, 200; got != want {
t.Fatalf("res.StatusCode: got %d, want %d", got, want)
}
if !tm.RequestModified() {
t.Error("tm.RequestModified(): got false, want true")
}
if !tm.ResponseModified() {
t.Error("tm.ResponseModified(): got false, want true")
}
}
示例13: TestPriorityGroupModifyResponseHaltsOnError
func TestPriorityGroupModifyResponseHaltsOnError(t *testing.T) {
pg := NewGroup()
reserr := errors.New("response error")
tm := martiantest.NewModifier()
tm.ResponseError(reserr)
pg.AddResponseModifier(tm, 100)
tm2 := martiantest.NewModifier()
pg.AddResponseModifier(tm2, 75)
res := proxyutil.NewResponse(200, nil, nil)
if err := pg.ModifyResponse(res); err != reserr {
t.Fatalf("ModifyRequest(): got %v, want %v", err, reserr)
}
if tm2.ResponseModified() {
t.Error("tm2.ResponseModified(): got true, want false")
}
}
示例14: TestNewStack
func TestNewStack(t *testing.T) {
stack, fg := NewStack("martian")
tm := martiantest.NewModifier()
fg.AddRequestModifier(tm)
fg.AddResponseModifier(tm)
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
// Hop-by-hop header to be removed.
req.Header.Set("Hop-By-Hop", "true")
req.Header.Set("Connection", "Hop-By-Hop")
req.RemoteAddr = "10.0.0.1:5000"
if err := stack.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if got, want := req.Header.Get("Hop-By-Hop"), ""; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", "Hop-By-Hop", got, want)
}
if got, want := req.Header.Get("X-Forwarded-For"), "10.0.0.1"; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", "X-Forwarded-For", got, want)
}
if got, want := req.Header.Get("Via"), "1.1 martian"; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", "Via", got, want)
}
res := proxyutil.NewResponse(200, nil, req)
// Hop-by-hop header to be removed.
res.Header.Set("Hop-By-Hop", "true")
res.Header.Set("Connection", "Hop-By-Hop")
if err := stack.ModifyResponse(res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if got, want := res.Header.Get("Hop-By-Hop"), ""; got != want {
t.Errorf("res.Header.Get(%q): got %q, want %q", "Hop-By-Hop", got, want)
}
}
示例15: TestModifyRequestHaltsOnError
func TestModifyRequestHaltsOnError(t *testing.T) {
fg := NewGroup()
reqerr := errors.New("request error")
tm := martiantest.NewModifier()
tm.RequestError(reqerr)
fg.AddRequestModifier(tm)
tm2 := martiantest.NewModifier()
fg.AddRequestModifier(tm2)
req, err := http.NewRequest("GET", "http://example.com/", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
if err := fg.ModifyRequest(req); err != reqerr {
t.Fatalf("fg.ModifyRequest(): got %v, want %v", err, reqerr)
}
if tm2.RequestModified() {
t.Error("tm2.RequestModified(): got true, want false")
}
}