本文整理汇总了Golang中github.com/google/martian.TestContext函数的典型用法代码示例。如果您正苦于以下问题:Golang TestContext函数的具体用法?Golang TestContext怎么用?Golang TestContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TestContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestOptionRequestPostDataLogging
func TestOptionRequestPostDataLogging(t *testing.T) {
logger := NewLogger()
logger.SetOption(PostDataLoggingForContentTypes("application/x-www-form-urlencoded"))
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")
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
if err := logger.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
log := logger.Export().Log
if got, want := string(log.Entries[0].Request.PostData.Params[0].Value), "true"; got != want {
t.Fatalf("log.Entries[0].Request.PostData.Params[0].Value: got %s, want %s", got, want)
}
logger = NewLogger()
logger.SetOption(SkipPostDataLoggingForContentTypes("application/x-www-form-urlencoded"))
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")
_, remove, err = martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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[0].Request.PostData.Params), 0; got != want {
t.Fatalf("len(log.Entries[0].Request.PostData.Params): got %v, want %v", got, want)
}
}
示例2: TestRoundTrip
func TestRoundTrip(t *testing.T) {
m := NewRoundTrip()
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
ctx, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
if ctx.SkippingRoundTrip() {
t.Fatal("ctx.SkippingRoundTrip(): got true, want false")
}
if err := m.ModifyRequest(req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if !ctx.SkippingRoundTrip() {
t.Fatal("ctx.SkippingRoundTrip(): got false, want true")
}
}
示例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)
}
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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)
}
}
示例4: 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)
}
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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)
}
}
示例5: 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)
}
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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)
}
}
}
示例6: 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)
}
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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)
}
}
示例7: 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, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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)
}
}
示例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, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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.NewContext(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: 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)
}
}
示例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, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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\talpha\t(martian)")
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\talpha\t(martian), 1.1 martian"; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", "Via", got, want)
}
req.Header.Set("Via", "1.0\talpha\t(martian), 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)
}
}
示例12: 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")
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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)
}
}
}
示例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)
}
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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)
}
}
示例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)
}
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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)
}
}
示例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())
_, remove, err := martian.TestContext(req)
if err != nil {
t.Fatalf("martian.TestContext(): got %v, want no error", err)
}
defer remove()
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)
}
}
}