当前位置: 首页>>代码示例>>Golang>>正文


Golang session.FromContext函数代码示例

本文整理汇总了Golang中github.com/google/martian/session.FromContext函数的典型用法代码示例。如果您正苦于以下问题:Golang FromContext函数的具体用法?Golang FromContext怎么用?Golang FromContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了FromContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestExportIgnoresOrphanedResponse

func TestExportIgnoresOrphanedResponse(t *testing.T) {
	logger := NewLogger("martian", "2.0.0")

	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)

	if err := logger.ModifyRequest(req); err != nil {
		t.Fatalf("ModifyRequest(): got %v, want no error", err)
	}

	// Reset before the response comes back.
	logger.Reset()

	res := proxyutil.NewResponse(200, nil, req)
	if err := logger.ModifyResponse(res); err != nil {
		t.Fatalf("ModifyResponse(): got %v, want no error", err)
	}

	log := logger.Export().Log
	if got, want := len(log.Entries), 0; got != want {
		t.Errorf("len(log.Entries): got %d, want %d", got, want)
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:32,代码来源:har_test.go

示例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")
	}
}
开发者ID:haijianren,项目名称:martian,代码行数:60,代码来源:auth_filter_test.go

示例3: TestReset

func TestReset(t *testing.T) {
	logger := NewLogger("martian", "2.0.0")

	req, err := http.NewRequest("GET", "http://example.com", nil)
	if err != nil {
		t.Fatalf("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)

	if err := logger.ModifyRequest(req); err != nil {
		t.Fatalf("ModifyRequest(): got %v, want no error", err)
	}

	log := logger.Export().Log
	if got, want := len(log.Entries), 1; got != want {
		t.Fatalf("len(log.Entries): got %d, want %d", got, want)
	}

	logger.Reset()

	log = logger.Export().Log
	if got, want := len(log.Entries), 0; got != want {
		t.Errorf("len(log.Entries): got %d, want %d", got, want)
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:31,代码来源:har_test.go

示例4: TestContext

func TestContext(t *testing.T) {
	t.Parallel()

	req, err := http.NewRequest("GET", "http://example.com", nil)
	if err != nil {
		t.Fatalf("http.NewRequest(): got %v, want no error", err)
	}

	want, err := session.FromContext(nil)
	if err != nil {
		t.Fatalf("session.FromContext(): got %v, want no error", err)
	}

	SetContext(req, want)

	if got := Context(req); got != want {
		t.Errorf("Context(req): got %v, want %v", got, want)
	}

	RemoveContext(req)

	if got := Context(req); got != nil {
		t.Errorf("Context(req): got %v, want nil", got)
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:25,代码来源:proxy_test.go

示例5: TestNoModifiers

func TestNoModifiers(t *testing.T) {
	m := NewModifier()
	m.SetRequestModifier(nil)
	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)

	if err := m.ModifyRequest(req); err != nil {
		t.Errorf("ModifyRequest(): got %v, want no error", err)
	}

	res := proxyutil.NewResponse(200, nil, req)
	if err := m.ModifyResponse(res); err != nil {
		t.Errorf("ModifyResponse(): got %v, want no error", err)
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:27,代码来源:proxyauth_test.go

示例6: TestExportSortsEntries

func TestExportSortsEntries(t *testing.T) {
	logger := NewLogger("martian", "2.0.0")
	count := 10

	for i := 0; i < count; i++ {
		req, err := http.NewRequest("GET", "http://example.com", nil)
		if err != nil {
			t.Fatalf("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)

		if err := logger.ModifyRequest(req); err != nil {
			t.Fatalf("ModifyRequest(): got %v, want no error", err)
		}
	}

	log := logger.Export().Log

	for i := 0; i < count-1; i++ {
		first := log.Entries[i]
		second := log.Entries[i+1]

		if got, want := first.StartedDateTime, second.StartedDateTime; got.After(want) {
			t.Errorf("entry.StartedDateTime: got %s, want to be before %s", got, want)
		}
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:33,代码来源:har_test.go

示例7: 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)
	}
}
开发者ID:haijianren,项目名称:martian,代码行数:59,代码来源:ipauth_test.go

示例8: 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)
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:57,代码来源:proxyauth_test.go

示例9: 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)
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:56,代码来源:ipauth_test.go

示例10: TestModifyRequestBodyURLEncoded

func TestModifyRequestBodyURLEncoded(t *testing.T) {
	logger := NewLogger("martian", "2.0.0")

	body := strings.NewReader("first=true&second=false")
	req, err := http.NewRequest("POST", "http://example.com", body)
	if err != nil {
		t.Fatalf("http.NewRequest(): got %v, want no error", err)
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	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 := logger.ModifyRequest(req); err != nil {
		t.Fatalf("ModifyRequest(): got %v, want no error", err)
	}

	log := logger.Export().Log
	if got, want := len(log.Entries), 1; got != want {
		t.Errorf("len(log.Entries): got %v, want %v", got, want)
	}

	pd := log.Entries[0].Request.PostData
	if got, want := pd.MimeType, "application/x-www-form-urlencoded"; got != want {
		t.Errorf("PostData.MimeType: got %v, want %v", got, want)
	}

	if got, want := len(pd.Params), 2; got != want {
		t.Fatalf("len(PostData.Params): got %d, want %d", got, want)
	}

	for _, p := range pd.Params {
		var want string
		switch p.Name {
		case "first":
			want = "true"
		case "second":
			want = "false"
		default:
			t.Errorf("PostData.Params: got %q, want to not be present", p.Name)
			continue
		}

		if got := p.Value; got != want {
			t.Errorf("PostData.Params[%q]: got %q, want %q", p.Name, got, want)
		}
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:52,代码来源:har_test.go

示例11: TestViaModifier

func TestViaModifier(t *testing.T) {
	m := NewViaModifier("martian")
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatalf("http.NewRequest(): got %v, want no error", err)
	}
	res := proxyutil.NewResponse(200, nil, req)

	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)
	}
	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)
	}

	if err := m.ModifyResponse(res); err != nil {
		t.Fatalf("ModifyResponse(): got %v, want no error", err)
	}

	req.Header.Set("Via", "1.0 alpha")
	if err := m.ModifyRequest(req); err != nil {
		t.Fatalf("ModifyRequest(): got %v, want no error", err)
	}
	if got, want := req.Header.Get("Via"), "1.0 alpha, 1.1 martian"; got != want {
		t.Errorf("req.Header.Get(%q): got %q, want %q", "Via", got, want)
	}

	req.Header.Set("Via", "1.0 alpha, 1.1 martian, 1.1 beta")
	if err := m.ModifyRequest(req); err == nil {
		t.Fatal("ModifyRequest(): got nil, want request loop error")
	}
	if !ctx.SkippingRoundTrip() {
		t.Errorf("ctx.SkippingRoundTrip(): got false, want true")
	}

	if err := m.ModifyResponse(res); err == nil {
		t.Fatal("ModifyResponse(): got nil, want request loop error")
	}
	if got, want := res.StatusCode, 400; got != want {
		t.Errorf("res.StatusCode: got %d, want %d", got, want)
	}
	if got, want := res.Status, http.StatusText(400); got != want {
		t.Errorf("res.Status: got %q, want %q", got, want)
	}
}
开发者ID:haijianren,项目名称:martian,代码行数:49,代码来源:via_modifier_test.go

示例12: handleLoop

func (p *Proxy) handleLoop(conn net.Conn) {
	p.conns.Add(1)
	defer p.conns.Done()
	defer conn.Close()

	ctx := session.FromContext(nil)
	brw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))

	for {
		deadline := time.Now().Add(p.timeout)
		conn.SetDeadline(deadline)

		if err := p.handle(ctx, conn, brw); isCloseable(err) {
			Infof("martian: closing connection: %v", conn.RemoteAddr())
			return
		}
	}
}
开发者ID:kleopatra999,项目名称:martian,代码行数:18,代码来源:proxy.go

示例13: TestHARExportsTime

func TestHARExportsTime(t *testing.T) {
	logger := NewLogger("martian", "2.0.0")

	req, err := http.NewRequest("GET", "http://example.com", nil)
	if err != nil {
		t.Fatalf("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)

	if err := logger.ModifyRequest(req); err != nil {
		t.Fatalf("ModifyRequest(): got %v, want no error", err)
	}

	// Simulate fast network round trip.
	time.Sleep(10 * time.Millisecond)

	res := proxyutil.NewResponse(200, nil, req)

	if err := logger.ModifyResponse(res); err != nil {
		t.Fatalf("ModifyResponse(): got %v, want no error", err)
	}

	log := logger.Export().Log
	if got, want := len(log.Entries), 1; got != want {
		t.Fatalf("len(log.Entries): got %v, want %v", got, want)
	}

	entry := log.Entries[0]
	min, max := int64(10), int64(100)
	if got := entry.Time; got < min || got > max {
		t.Errorf("entry.Time: got %dms, want between %dms and %vms", got, min, max)
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:39,代码来源:har_test.go

示例14: TestModifyRequestBodyArbitraryContentType

func TestModifyRequestBodyArbitraryContentType(t *testing.T) {
	logger := NewLogger("martian", "2.0.0")

	body := "arbitrary binary data"
	req, err := http.NewRequest("POST", "http://www.example.com", strings.NewReader(body))
	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)

	if err := logger.ModifyRequest(req); err != nil {
		t.Fatalf("ModifyRequest(): got %v, want no error", err)
	}

	log := logger.Export().Log
	if got, want := len(log.Entries), 1; got != want {
		t.Errorf("len(log.Entries): got %d, want %d", got, want)
	}

	pd := log.Entries[0].Request.PostData
	if got, want := pd.MimeType, ""; got != want {
		t.Errorf("PostData.MimeType: got %q, want %q", got, want)
	}
	if got, want := len(pd.Params), 0; got != want {
		t.Errorf("len(PostData.Params): got %d, want %d", got, want)
	}

	if got, want := pd.Text, body; got != want {
		t.Errorf("PostData.Text: got %q, want %q", got, want)
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:37,代码来源:har_test.go

示例15: TestModifyRequestBodyMultipart

func TestModifyRequestBodyMultipart(t *testing.T) {
	logger := NewLogger("martian", "2.0.0")

	body := new(bytes.Buffer)
	mpw := multipart.NewWriter(body)
	mpw.SetBoundary("boundary")

	if err := mpw.WriteField("key", "value"); err != nil {
		t.Errorf("mpw.WriteField(): got %v, want no error", err)
	}

	w, err := mpw.CreateFormFile("file", "test.txt")
	if _, err = w.Write([]byte("file contents")); err != nil {
		t.Fatalf("Write(): got %v, want no error", err)
	}
	mpw.Close()

	req, err := http.NewRequest("POST", "http://example.com", body)
	if err != nil {
		t.Fatalf("http.NewRequest(): got %v, want no error", err)
	}
	req.Header.Set("Content-Type", mpw.FormDataContentType())

	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 := logger.ModifyRequest(req); err != nil {
		t.Fatalf("ModifyRequest(): got %v, want no error", err)
	}

	log := logger.Export().Log
	if got, want := len(log.Entries), 1; got != want {
		t.Fatalf("len(log.Entries): got %d, want %d", got, want)
	}

	pd := log.Entries[0].Request.PostData
	if got, want := pd.MimeType, "multipart/form-data"; got != want {
		t.Errorf("PostData.MimeType: got %q, want %q", got, want)
	}
	if got, want := len(pd.Params), 2; got != want {
		t.Errorf("PostData.Params: got %d, want %d", got, want)
	}

	for _, p := range pd.Params {
		var want Param

		switch p.Name {
		case "key":
			want = Param{
				Filename:    "",
				ContentType: "",
				Value:       "value",
			}
		case "file":
			want = Param{
				Filename:    "test.txt",
				ContentType: "application/octet-stream",
				Value:       "file contents",
			}
		default:
			t.Errorf("pd.Params: got %q, want not to be present", p.Name)
			continue
		}

		if got, want := p.Filename, want.Filename; got != want {
			t.Errorf("p.Filename: got %q, want %q", got, want)
		}
		if got, want := p.ContentType, want.ContentType; got != want {
			t.Errorf("p.ContentType: got %q, want %q", got, want)
		}
		if got, want := p.Value, want.Value; got != want {
			t.Errorf("p.Value: got %q, want %q", got, want)
		}
	}
}
开发者ID:xichen124,项目名称:martian,代码行数:79,代码来源:har_test.go


注:本文中的github.com/google/martian/session.FromContext函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。