本文整理汇总了Golang中github.com/google/martian.NewContext函数的典型用法代码示例。如果您正苦于以下问题:Golang NewContext函数的具体用法?Golang NewContext怎么用?Golang NewContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewContext函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestSetForwardHeaders
func TestSetForwardHeaders(t *testing.T) {
xfp := "X-Forwarded-Proto"
xff := "X-Forwarded-For"
m := NewForwardedModifier()
req, err := http.NewRequest("GET", "http://martian.local", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
req.RemoteAddr = "10.0.0.1:8112"
if m.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if got, want := req.Header.Get(xfp), "http"; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", xfp, got, want)
}
if got, want := req.Header.Get(xff), "10.0.0.1"; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", xff, got, want)
}
// Test with existing X-Forwarded-For.
req.RemoteAddr = "12.12.12.12"
if m.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if got, want := req.Header.Get(xff), "10.0.0.1, 12.12.12.12"; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", xff, got, want)
}
}
开发者ID:hanscj1,项目名称:martian,代码行数:33,代码来源:forwarded_modifier_test.go
示例2: TestModifierFromJSON
func TestModifierFromJSON(t *testing.T) {
msg := []byte(`{
"cookie.Modifier": {
"scope": ["request", "response"],
"name": "martian",
"value": "value"
}
}`)
r, err := parse.FromJSON(msg)
if err != nil {
t.Fatalf("parse.FromJSON(): got %v, want no error", err)
}
req, err := http.NewRequest("GET", "http://example.com/path/", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
reqmod := r.RequestModifier()
if reqmod == nil {
t.Fatal("reqmod: got nil, want not nil")
}
if err := reqmod.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
}
if got, want := len(req.Cookies()), 1; got != want {
t.Fatalf("len(req.Cookies): got %v, want %v", got, want)
}
if got, want := req.Cookies()[0].Name, "martian"; got != want {
t.Errorf("req.Cookies()[0].Name: got %v, want %v", got, want)
}
if got, want := req.Cookies()[0].Value, "value"; got != want {
t.Errorf("req.Cookies()[0].Value: got %v, want %v", got, want)
}
resmod := r.ResponseModifier()
if resmod == nil {
t.Fatal("resmod: got nil, want not nil")
}
res := proxyutil.NewResponse(200, nil, req)
if err := resmod.ModifyResponse(martian.NewContext(), res); err != nil {
t.Fatalf("resmod.ModifyResponse(): got %v, want no error", err)
}
if got, want := len(res.Cookies()), 1; got != want {
t.Fatalf("len(res.Cookies): got %v, want %v", got, want)
}
if got, want := res.Cookies()[0].Name, "martian"; got != want {
t.Errorf("res.Cookies()[0].Name: got %v, want %v", got, want)
}
if got, want := res.Cookies()[0].Value, "value"; got != want {
t.Errorf("res.Cookies()[0].Value: got %v, want %v", got, want)
}
}
开发者ID:eolexe,项目名称:martian,代码行数:60,代码来源:cookie_modifier_test.go
示例3: TestRemoveHopByHopHeaders
func TestRemoveHopByHopHeaders(t *testing.T) {
m := NewHopByHopModifier()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
hs := http.Header{
// Additional hop-by-hop headers are listed in the
// Connection header.
"Connection": []string{
"X-Connection",
"X-Hop-By-Hop, close",
},
// RFC hop-by-hop headers.
"Keep-Alive": []string{},
"Proxy-Authenticate": []string{},
"Proxy-Authorization": []string{},
"Te": []string{},
"Trailer": []string{},
"Transfer-Encoding": []string{},
"Upgrade": []string{},
// Hop-by-hop headers listed in the Connection header.
"X-Connection": []string{},
"X-Hop-By-Hop": []string{},
// End-to-end header that should not be removed.
"X-End-To-End": []string{},
}
req.Header = hs
if err := m.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if got, want := len(req.Header), 1; got != want {
t.Fatalf("len(req.Header): got %d, want %d", got, want)
}
if _, ok := req.Header["X-End-To-End"]; !ok {
t.Errorf("req.Header[%q]: got !ok, want ok", "X-End-To-End")
}
res := proxyutil.NewResponse(200, nil, req)
res.Header = hs
if err := m.ModifyResponse(martian.NewContext(), res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if got, want := len(res.Header), 1; got != want {
t.Fatalf("len(res.Header): got %d, want %d", got, want)
}
if _, ok := res.Header["X-End-To-End"]; !ok {
t.Errorf("res.Header[%q]: got !ok, want ok", "X-End-To-End")
}
}
开发者ID:hanscj1,项目名称:martian,代码行数:56,代码来源:hopbyhop_modifier_test.go
示例4: TestFilterWithQueryStringNameAndValue
func TestFilterWithQueryStringNameAndValue(t *testing.T) {
name, value := "name", "value"
nameMatcher, err := regexp.Compile(name)
if err != nil {
t.Fatalf("regexp.Compile(%q): got %v, want no error", name, err)
}
valueMatcher, err := regexp.Compile(value)
if err != nil {
t.Fatalf("regexp.Compile(%q): got %v, want no error", value, err)
}
modifierRun := false
f := func(*martian.Context, *http.Request) error {
modifierRun = true
return nil
}
filter, err := NewFilter(nameMatcher, valueMatcher)
if err != nil {
t.Fatalf("NewFilter(): got %v, want no error", err)
}
filter.SetRequestModifier(martian.RequestModifierFunc(f))
v := url.Values{}
v.Add("nomatch", "value")
req, err := http.NewRequest("POST", "http://martian.local?name=value", strings.NewReader(v.Encode()))
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
if err := filter.ModifyRequest(martian.NewContext(), req); err != nil {
t.Errorf("ModifyRequest(): got %v, want no error", err)
}
if !modifierRun {
t.Error("modifierRun: got false, want true")
}
v = url.Values{}
req, err = http.NewRequest("POST", "http://martian.local", strings.NewReader(v.Encode()))
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
modifierRun = false
if err := filter.ModifyRequest(martian.NewContext(), req); err != nil {
t.Errorf("ModifyRequest(): got %v, want no error", err)
}
if modifierRun {
t.Error("modifierRun: got true, want false")
}
}
开发者ID:eolexe,项目名称:martian,代码行数:52,代码来源:query_string_filter_test.go
示例5: TestFilterFromJSON
func TestFilterFromJSON(t *testing.T) {
msg := []byte(`{
"header.Filter": {
"scope": ["request", "response"],
"name": "Martian-Passthru",
"value": "true",
"modifier": {
"header.Modifier" : {
"scope": ["request", "response"],
"name": "Martian-Testing",
"value": "true"
}
}
}
}`)
r, err := parse.FromJSON(msg)
if err != nil {
t.Fatalf("parse.FromJSON(): got %v, want no error", err)
}
reqmod := r.RequestModifier()
if reqmod == nil {
t.Fatal("reqmod: got nil, want not nil")
}
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("Martian-Passthru", "true")
if err := reqmod.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if got, want := req.Header.Get("Martian-Testing"), "true"; got != want {
t.Fatalf("req.Header.Get(%q): got %q, want %q", "Martian-Testing", got, want)
}
resmod := r.ResponseModifier()
if resmod == nil {
t.Fatal("resmod: got nil, want not nil")
}
res := proxyutil.NewResponse(200, nil, nil)
res.Header.Set("Martian-Passthru", "true")
if err := resmod.ModifyResponse(martian.NewContext(), res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if got, want := res.Header.Get("Martian-Testing"), "true"; got != want {
t.Fatalf("res.Header.Get(%q): got %q, want %q", "Martian-Testing", got, want)
}
}
开发者ID:eolexe,项目名称:martian,代码行数:51,代码来源:header_filter_test.go
示例6: TestVerifierFromJSON
func TestVerifierFromJSON(t *testing.T) {
msg := []byte(`{
"header.Verifier": {
"scope": ["request", "response"],
"name": "Martian-Test",
"value": "true"
}
}`)
r, err := parse.FromJSON(msg)
if err != nil {
t.Fatalf("parse.FromJSON(): got %v, want no error", err)
}
reqmod := r.RequestModifier()
if reqmod == nil {
t.Fatal("reqmod: got nil, want not nil")
}
reqv, ok := reqmod.(verify.RequestVerifier)
if !ok {
t.Fatal("reqmod.(verify.RequestVerifier): got !ok, want ok")
}
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
if err := reqv.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if err := reqv.VerifyRequests(); err == nil {
t.Error("VerifyRequests(): got nil, want not nil")
}
resmod := r.ResponseModifier()
if resmod == nil {
t.Fatal("resmod: got nil, want not nil")
}
resv, ok := resmod.(verify.ResponseVerifier)
if !ok {
t.Fatal("resmod.(verify.ResponseVerifier): got !ok, want ok")
}
res := proxyutil.NewResponse(200, nil, req)
if err := resv.ModifyResponse(martian.NewContext(), res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if err := resv.VerifyResponses(); err == nil {
t.Error("VerifyResponses(): got nil, want not nil")
}
}
开发者ID:eolexe,项目名称:martian,代码行数:50,代码来源:header_verifier_test.go
示例7: TestFromJSON
func TestFromJSON(t *testing.T) {
msg := []byte(`{
"status.Modifier": {
"scope": ["response"],
"statusCode": 400
}
}`)
r, err := parse.FromJSON(msg)
if err != nil {
t.Fatalf("parse.FromJSON(): got %v, want no error", err)
}
resmod := r.ResponseModifier()
if resmod == nil {
t.Fatal("resmod: got nil, want not nil")
}
res := proxyutil.NewResponse(200, nil, nil)
if err := resmod.ModifyResponse(martian.NewContext(), res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if got, want := res.StatusCode, 400; got != want {
t.Errorf("res.StatusCode: got %d, want %d", got, want)
}
}
开发者ID:eolexe,项目名称:martian,代码行数:25,代码来源:status_modifier_test.go
示例8: TestModifierFromJSON
func TestModifierFromJSON(t *testing.T) {
msg := []byte(`
{
"querystring.Modifier": {
"scope": ["request"],
"name": "param",
"value": "true"
}
}`)
r, err := parse.FromJSON(msg)
if err != nil {
t.Fatalf("parse.FromJSON(): got %v, want no error", err)
}
req, err := http.NewRequest("GET", "http://martian.test", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %q, want no error", err)
}
reqmod := r.RequestModifier()
if reqmod == nil {
t.Fatalf("reqmod: got nil, want not nil")
}
ctx := martian.NewContext()
if err := reqmod.ModifyRequest(ctx, req); err != nil {
t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
}
if got, want := req.URL.Query().Get("param"), "true"; got != want {
t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "param", got, want)
}
}
开发者ID:eolexe,项目名称:martian,代码行数:35,代码来源:query_string_modifier_test.go
示例9: TestIntegration
func TestIntegration(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
r.URL.Scheme = "http"
r.URL.Host = r.Host
w.Header().Set("Martian-URL", r.URL.String())
}))
defer server.Close()
u := &url.URL{
Scheme: "http",
Host: server.Listener.Addr().String(),
}
m := NewModifier(u)
req, err := http.NewRequest("GET", "https://example.com/test", nil)
if err != nil {
t.Fatalf("http.NewRequest(%q, %q, nil): got %v, want no error", "GET", "http://example.com/test", err)
}
if err := m.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("http.DefaultClient.Do(): got %v, want no error", err)
}
want := "http://example.com/test"
if got := res.Header.Get("Martian-URL"); got != want {
t.Errorf("res.Header.Get(%q): got %q, want %q", "Martian-URL", got, want)
}
}
开发者ID:hanscj1,项目名称:martian,代码行数:34,代码来源:url_modifier_test.go
示例10: TestVerifierFromJSON
func TestVerifierFromJSON(t *testing.T) {
msg := []byte(`{
"url.Verifier": {
"scope": ["request"],
"scheme": "https",
"host": "www.martian.proxy",
"path": "/testing",
"query": "test=true"
}
}`)
r, err := parse.FromJSON(msg)
if err != nil {
t.Fatalf("parse.FromJSON(): got %v, want no error", err)
}
reqmod := r.RequestModifier()
if reqmod == nil {
t.Fatal("reqmod: got nil, want not nil")
}
reqv, ok := reqmod.(verify.RequestVerifier)
if !ok {
t.Fatal("reqmod.(verify.RequestVerifier): got !ok, want ok")
}
req, err := http.NewRequest("GET", "https://www.martian.proxy/testing?test=false", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
if err := reqv.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if err := reqv.VerifyRequests(); err == nil {
t.Error("VerifyRequests(): got nil, want not nil")
}
}
开发者ID:hanscj1,项目名称:martian,代码行数:35,代码来源:url_verifier_test.go
示例11: TestFailureWithMissingKey
func TestFailureWithMissingKey(t *testing.T) {
v, err := NewVerifier("foo", "bar")
if err != nil {
t.Fatalf("NewVerifier(%q, %q): got %v, want no error", "foo", "bar", err)
}
req, err := http.NewRequest("GET", "http://www.google.com?fizz=bar", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
if err := v.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
merr, ok := v.VerifyRequests().(*verify.MultiError)
if !ok {
t.Fatal("VerifyRequests(): got nil, want *verify.MultiError")
}
errs := merr.Errors()
if len(errs) != 1 {
t.Fatalf("len(merr.Errors()): got %d, want 1", len(errs))
}
expectErr := "request(http://www.google.com?fizz=bar) param verification error: key foo not found"
for i := range errs {
if got, want := errs[i].Error(), expectErr; got != want {
t.Errorf("%d. err.Error(): mismatched error output\ngot: %s\nwant: %s", i, got, want)
}
}
}
开发者ID:eolexe,项目名称:martian,代码行数:29,代码来源:query_string_verifier_test.go
示例12: TestModifyRequest
func TestModifyRequest(t *testing.T) {
m := NewModifier()
ctx := martian.NewContext()
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"))
if err := m.ModifyRequest(ctx, req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if got, want := ctx.Auth.ID, "user:pass"; got != want {
t.Fatalf("ctx.Auth.ID: got %q, want %q", got, want)
}
modifierRun := false
m.SetRequestModifier(martian.RequestModifierFunc(
func(*martian.Context, *http.Request) error {
modifierRun = true
return nil
}))
if err := m.ModifyRequest(ctx, req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if !modifierRun {
t.Error("modifierRun: got false, want true")
}
}
开发者ID:eolexe,项目名称:martian,代码行数:31,代码来源:proxyauth_test.go
示例13: TestStatusModifierOnResponse
func TestStatusModifierOnResponse(t *testing.T) {
for i, status := range []int{
http.StatusForbidden,
http.StatusOK,
http.StatusTemporaryRedirect,
} {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatalf("NewRequest(): got %v, want no error", err)
}
res := proxyutil.NewResponse(200, nil, req)
mod := NewModifier(status)
if err := mod.ModifyResponse(martian.NewContext(), res); err != nil {
t.Fatalf("%d. ModifyResponse(): got %v, want no error", i, err)
}
if got, want := res.StatusCode, status; got != want {
t.Errorf("%d. res.StatusCode: got %v, want %v", i, got, want)
}
if got, want := res.Status, http.StatusText(status); got != want {
t.Errorf("%d. res.Status: got %q, want %q", i, got, want)
}
}
}
开发者ID:eolexe,项目名称:martian,代码行数:27,代码来源:status_modifier_test.go
示例14: TestModifyResponse
func TestModifyResponse(t *testing.T) {
m := NewModifier()
ctx := martian.NewContext()
res := proxyutil.NewResponse(200, nil, nil)
if err := m.ModifyResponse(ctx, res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
m.SetResponseModifier(martian.ResponseModifierFunc(
func(*martian.Context, *http.Response) error {
ctx.Auth.Error = errors.New("auth is required")
return nil
}))
if err := m.ModifyResponse(ctx, res); err != nil {
t.Fatalf("ModifyResponse(): got %v, want no error", err)
}
if got, want := res.StatusCode, http.StatusProxyAuthRequired; got != want {
t.Errorf("res.StatusCode: got %d, want %d", got, want)
}
if got, want := res.Header.Get("Proxy-Authenticate"), "Basic"; got != want {
t.Errorf("res.Header.Get(%q): got %q, want %q", "Proxy-Authenticate", got, want)
}
}
开发者ID:eolexe,项目名称:martian,代码行数:25,代码来源:proxyauth_test.go
示例15: ExampleLogger
func ExampleLogger() {
l := NewLogger()
l.SetLogFunc(func(line string) {
// Remove \r to make it easier to test with examples.
fmt.Print(strings.Replace(line, "\r", "", -1))
})
req, err := http.NewRequest("GET", "http://example.com/path?querystring", strings.NewReader("body"))
if err != nil {
fmt.Println(err)
return
}
req.RequestURI = req.URL.RequestURI()
req.Header.Set("Other-Header", "values")
req.Close = true
if err := l.ModifyRequest(martian.NewContext(), req); err != nil {
fmt.Println(err)
return
}
res := proxyutil.NewResponse(200, strings.NewReader("body"), req)
res.Header.Set("Date", "Tue, 15 Nov 1994 08:12:31 GMT")
res.Header.Set("Other-Header", "values")
if err := l.ModifyResponse(martian.NewContext(), res); err != nil {
fmt.Println(err)
return
}
// Output:
// --------------------------------------------------------------------------------
// Request to http://example.com/path?querystring
// --------------------------------------------------------------------------------
// GET /path?querystring HTTP/1.1
// Host: example.com
// Connection: close
// Other-Header: values
// --------------------------------------------------------------------------------
//
// --------------------------------------------------------------------------------
// Response from http://example.com/path?querystring
// --------------------------------------------------------------------------------
// HTTP/1.1 200 OK
// Date: Tue, 15 Nov 1994 08:12:31 GMT
// Other-Header: values
// --------------------------------------------------------------------------------
}
开发者ID:hanscj1,项目名称:martian,代码行数:47,代码来源:logger_test.go
示例16: TestModifyResponseNoModifier
func TestModifyResponseNoModifier(t *testing.T) {
m := NewModifier()
res := proxyutil.NewResponse(200, nil, nil)
if err := m.ModifyResponse(martian.NewContext(), res); err != nil {
t.Errorf("ModifyResponse(): got %v, want no error", err)
}
}
开发者ID:eolexe,项目名称:martian,代码行数:8,代码来源:martianhttp_test.go
示例17: TestFilterFromJSON
func TestFilterFromJSON(t *testing.T) {
msg := []byte(`
{
"querystring.Filter": {
"scope": ["request", "response"],
"name": "param",
"value": "true",
"modifier": {
"header.Modifier": {
"scope": ["request", "response"],
"name": "Mod-Run",
"value": "true"
}
}
}
}`)
r, err := parse.FromJSON(msg)
if err != nil {
t.Fatalf("parse.FromJSON(): got %v, want no error", err)
}
reqmod := r.RequestModifier()
if reqmod == nil {
t.Fatal("reqmod: got nil, want not nil")
}
req, err := http.NewRequest("GET", "https://martian.test?param=true", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
ctx := martian.NewContext()
if err := reqmod.ModifyRequest(ctx, req); err != nil {
t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
}
if got, want := req.Header.Get("Mod-Run"), "true"; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", "Mod-Run", got, want)
}
resmod := r.ResponseModifier()
if resmod == nil {
t.Fatalf("resmod: got nil, want not nil")
}
res := proxyutil.NewResponse(200, nil, req)
if err := resmod.ModifyResponse(ctx, res); err != nil {
t.Fatalf("resmod.ModifyResponse(): got %v, want no error", err)
}
if got, want := res.Header.Get("Mod-Run"), "true"; got != want {
t.Errorf("res.Header.Get(%q): got %q, want %q", "Mod-Run", got, want)
}
}
开发者ID:eolexe,项目名称:martian,代码行数:57,代码来源:query_string_filter_test.go
示例18: TestFilterWithQueryParamNameAndNoValue
func TestFilterWithQueryParamNameAndNoValue(t *testing.T) {
name := "name"
nameMatcher, err := regexp.Compile(name)
if err != nil {
t.Fatalf("regexp.Compile(%q): got %v, want no error", name, err)
}
modifierRun := false
f := func(*martian.Context, *http.Request) error {
modifierRun = true
return nil
}
filter, err := NewFilter(nameMatcher, nil)
if err != nil {
t.Fatalf("NewFilter(): got %v, want no error", err)
}
filter.SetRequestModifier(martian.RequestModifierFunc(f))
req, err := http.NewRequest("GET", "http://martian.local?name", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
if err := filter.ModifyRequest(martian.NewContext(), req); err != nil {
t.Errorf("ModifyRequest(): got %v, want no error", err)
}
if !modifierRun {
t.Error("modifierRun: got false, want true")
}
req, err = http.NewRequest("GET", "http://martian.local?test", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
modifierRun = false
if err := filter.ModifyRequest(martian.NewContext(), req); err != nil {
t.Errorf("ModifyRequest(): got %v, want no error", err)
}
if modifierRun {
t.Error("modifierRun: got true, want false")
}
}
开发者ID:eolexe,项目名称:martian,代码行数:44,代码来源:query_string_filter_test.go
示例19: TestServeHTTP
func TestServeHTTP(t *testing.T) {
m := NewModifier()
msg := []byte(`
{
"header.Modifier": {
"scope": ["request", "response"],
"name": "Martian-Test",
"value": "true"
}
}`)
req, err := http.NewRequest("POST", "/martian/modifiers?id=id", bytes.NewBuffer(msg))
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
req.Header.Set("Content-Type", "application/json")
rw := httptest.NewRecorder()
m.ServeHTTP(rw, req)
if got, want := rw.Code, 200; got != want {
t.Errorf("rw.Code: got %d, want %d", got, want)
}
req, err = http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
if err := m.ModifyRequest(martian.NewContext(), req); err != nil {
t.Fatalf("m.ModifyRequest(): got %v, want no error", err)
}
if got, want := req.Header.Get("Martian-Test"), "true"; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", "Martian-Test", got, want)
}
res := proxyutil.NewResponse(200, nil, req)
if err := m.ModifyResponse(martian.NewContext(), res); err != nil {
t.Fatalf("m.ModifyResponse(): got %v, want no error", err)
}
if got, want := res.Header.Get("Martian-Test"), "true"; got != want {
t.Errorf("res.Header.Get(%q): got %q, want %q", "Martian-Test", got, want)
}
}
开发者ID:eolexe,项目名称:martian,代码行数:44,代码来源:martianhttp_test.go
示例20: TestModifyRequest
func TestModifyRequest(t *testing.T) {
f := NewFilter()
modifierRun := false
f.SetRequestModifier("id", martian.RequestModifierFunc(
func(*martian.Context, *http.Request) error {
modifierRun = true
return nil
}))
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatalf("NewRequest(): got %v, want no error", err)
}
ctx := martian.NewContext()
// No ID, auth required.
f.SetAuthRequired(true)
if err := f.ModifyRequest(ctx, req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if ctx.Auth.Error == nil {
t.Error("ctx.Auth.Error: got nil, want error")
}
if modifierRun {
t.Error("modifierRun: got true, want false")
}
// No ID, auth not required.
f.SetAuthRequired(false)
ctx.Auth.Error = nil
if err := f.ModifyRequest(ctx, req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if ctx.Auth.Error != nil {
t.Errorf("ctx.Auth.Error: got %v, want no error", err)
}
if modifierRun {
t.Error("modifierRun: got true, want false")
}
// Valid ID.
ctx.Auth.ID = "id"
ctx.Auth.Error = nil
if err := f.ModifyRequest(ctx, req); err != nil {
t.Fatalf("ModifyRequest(): got %v, want no error", err)
}
if ctx.Auth.Error != nil {
t.Errorf("ctx.Auth.Error: got %v, want no error", ctx.Auth.Error)
}
if !modifierRun {
t.Error("modifierRun: got false, want true")
}
}
开发者ID:eolexe,项目名称:martian,代码行数:56,代码来源:auth_test.go
注:本文中的github.com/google/martian.NewContext函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论