예제 #1
0
파일: auth_test.go 프로젝트: eolexe/martian
func TestFilter(t *testing.T) {
	f := NewFilter()
	if reqmod := f.RequestModifier("id"); reqmod != nil {
		t.Fatalf("f.RequestModifier(%q): got reqmod, want no reqmod", "id")
	}
	if resmod := f.ResponseModifier("id"); resmod != nil {
		t.Fatalf("f.ResponseModifier(%q): got resmod, want no resmod", "id")
	}

	f.SetRequestModifier("id", martian.RequestModifierFunc(
		func(*martian.Context, *http.Request) error {
			return nil
		}))

	f.SetResponseModifier("id", martian.ResponseModifierFunc(
		func(*martian.Context, *http.Response) error {
			return nil
		}))

	if reqmod := f.RequestModifier("id"); reqmod == nil {
		t.Errorf("f.RequestModifier(%q): got no reqmod, want reqmod", "id")
	}
	if resmod := f.ResponseModifier("id"); resmod == nil {
		t.Errorf("f.ResponseModifier(%q): got no resmod, want resmod", "id")
	}

	f.SetRequestModifier("id", nil)
	f.SetResponseModifier("id", nil)
	if reqmod := f.RequestModifier("id"); reqmod != nil {
		t.Fatalf("f.RequestModifier(%q): got reqmod, want no reqmod", "id")
	}
	if resmod := f.ResponseModifier("id"); resmod != nil {
		t.Fatalf("f.ResponseModifier(%q): got resmod, want no resmod", "id")
	}
}
예제 #2
0
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)
	}
}
예제 #3
0
func TestModifyResponseHaltsOnError(t *testing.T) {
	mg := NewGroup()
	errHalt := errors.New("halt modifier chain")
	f := func(*martian.Context, *http.Response) error {
		return errHalt
	}
	mg.AddResponseModifier(martian.ResponseModifierFunc(f))

	f = func(*martian.Context, *http.Response) error {
		t.Fatal("ModifyResponse(): got called, want skipped")
		return nil
	}
	mg.AddResponseModifier(martian.ResponseModifierFunc(f))

	res := proxyutil.NewResponse(200, nil, nil)
	if err := mg.ModifyResponse(martian.NewContext(), res); err != errHalt {
		t.Fatalf("mg.ModifyResponse(): got %v, want %v", err, errHalt)
	}
}
예제 #4
0
func TestPriorityGroupModifyResponseHaltsOnError(t *testing.T) {
	pg := NewGroup()

	errHalt := errors.New("modifier chain halted")
	f := func(*martian.Context, *http.Response) error {
		return errHalt
	}
	pg.AddResponseModifier(martian.ResponseModifierFunc(f), 100)

	f = func(*martian.Context, *http.Response) error {
		t.Fatal("ModifyResponse(): got called, want skipped")
		return nil
	}
	pg.AddResponseModifier(martian.ResponseModifierFunc(f), 75)

	res := proxyutil.NewResponse(200, nil, nil)
	if err := pg.ModifyResponse(martian.NewContext(), res); err != errHalt {
		t.Fatalf("ModifyResponse(): got %v, want errHalt", err)
	}
}
예제 #5
0
파일: auth_test.go 프로젝트: eolexe/martian
func TestModifyResponse(t *testing.T) {
	f := NewFilter()

	modifierRun := false
	f.SetResponseModifier("id", martian.ResponseModifierFunc(
		func(*martian.Context, *http.Response) error {
			modifierRun = true
			return nil
		}))

	res := proxyutil.NewResponse(200, nil, nil)
	ctx := martian.NewContext()

	// No ID, auth required.
	f.SetAuthRequired(true)

	if err := f.ModifyResponse(ctx, res); err != nil {
		t.Fatalf("ModifyResponse(): 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, no auth required.
	f.SetAuthRequired(false)
	ctx.Auth.Error = nil

	if err := f.ModifyResponse(ctx, res); err != nil {
		t.Fatalf("ModifyResponse(): 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 true, want false")
	}

	// Valid ID.
	ctx.Auth.ID = "id"
	ctx.Auth.Error = nil

	if err := f.ModifyResponse(ctx, res); err != nil {
		t.Fatalf("ModifyResponse(): 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")
	}
}
예제 #6
0
func TestPriorityGroupModifyResponse(t *testing.T) {
	var priorities []int64

	pg := NewGroup()
	f := func(*martian.Context, *http.Response) error {
		priorities = append(priorities, 50)
		return nil
	}
	pg.AddResponseModifier(martian.ResponseModifierFunc(f), 50)

	f = func(*martian.Context, *http.Response) error {
		priorities = append(priorities, 100)
		return nil
	}
	pg.AddResponseModifier(martian.ResponseModifierFunc(f), 100)

	f = func(*martian.Context, *http.Response) error {
		priorities = append(priorities, 75)
		return nil
	}

	// Functions are not directly comparable, so we must wrap in a
	// type that is.
	m := &struct{ martian.ResponseModifier }{martian.ResponseModifierFunc(f)}
	if err := pg.RemoveResponseModifier(m); err != ErrModifierNotFound {
		t.Fatalf("RemoveResponseModifier(): got %v, want ErrModifierNotFound", err)
	}
	pg.AddResponseModifier(m, 75)
	if err := pg.RemoveResponseModifier(m); err != nil {
		t.Fatalf("RemoveResponseModifier(): got %v, want no error", err)
	}

	res := proxyutil.NewResponse(200, nil, nil)
	if err := pg.ModifyResponse(martian.NewContext(), res); err != nil {
		t.Fatalf("ModifyResponse(): got %v, want no error", err)
	}
	if got, want := priorities, []int64{100, 50}; !reflect.DeepEqual(got, want) {
		t.Fatalf("reflect.DeepEqual(%v, %v): got false, want true", got, want)
	}
}
예제 #7
0
func TestModifyResponse(t *testing.T) {
	m := NewModifier()

	var modRun bool
	m.resmod = martian.ResponseModifierFunc(
		func(*martian.Context, *http.Response) error {
			modRun = true
			return nil
		})

	res := proxyutil.NewResponse(200, nil, nil)
	if err := m.ModifyResponse(martian.NewContext(), res); err != nil {
		t.Fatalf("ModifyResponse(): got %v, want no error", err)
	}
	if !modRun {
		t.Error("modRun: got false, want true")
	}
}
예제 #8
0
func TestModifyResponse(t *testing.T) {
	mg := NewGroup()

	modifierRun := false
	f := func(*martian.Context, *http.Response) error {
		modifierRun = true
		return nil
	}
	mg.AddResponseModifier(martian.ResponseModifierFunc(f))

	res := proxyutil.NewResponse(200, nil, nil)
	if err := mg.ModifyResponse(martian.NewContext(), res); err != nil {
		t.Fatalf("mg.ModifyResponse(): got %v, want no error", err)
	}
	if !modifierRun {
		t.Error("modifierRun: got false, want true")
	}
}
예제 #9
0
func TestResultRequestResponseModifierCorrectScope(t *testing.T) {
	mod := struct {
		martian.RequestModifier
		martian.ResponseModifier
	}{
		RequestModifier: martian.RequestModifierFunc(
			func(*martian.Context, *http.Request) error {
				return nil
			}),
		ResponseModifier: martian.ResponseModifierFunc(
			func(*martian.Context, *http.Response) error {
				return nil
			}),
	}
	result := &Result{
		reqmod: mod,
		resmod: nil,
	}
	reqmod := result.RequestModifier()
	if reqmod == nil {
		t.Error("result.RequestModifier: got nil, want not nil")
	}

	resmod := result.ResponseModifier()
	if resmod != nil {
		t.Errorf("result.ResponseModifier: got %v, want nil", resmod)
	}

	result = &Result{
		reqmod: nil,
		resmod: mod,
	}
	reqmod = result.RequestModifier()
	if reqmod != nil {
		t.Errorf("result.RequestModifier: got %v, want nil", reqmod)
	}

	resmod = result.ResponseModifier()
	if resmod == nil {
		t.Error("result.ResponseModifier: got nil, want not nil")
	}
}
예제 #10
0
func TestModifyResponse(t *testing.T) {
	tt := []struct {
		name   string
		values []string
		want   bool
	}{
		{
			name:   "Martian-Production",
			values: []string{"true"},
			want:   false,
		},
		{
			name:   "Martian-Testing",
			values: []string{"see-next-value", "true"},
			want:   true,
		},
	}

	for i, tc := range tt {
		var got bool

		f := NewFilter("mARTian-teSTInG", "true")
		f.SetResponseModifier(martian.ResponseModifierFunc(
			func(*martian.Context, *http.Response) error {
				got = true
				return nil
			}))

		res := proxyutil.NewResponse(200, nil, nil)
		res.Header[tc.name] = tc.values

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

		if got != tc.want {
			t.Errorf("%d. modifier run: got %t, want %t", i, got, tc.want)
		}
	}
}
예제 #11
0
func TestNewResultMismatchedScopes(t *testing.T) {
	reqmod := martian.RequestModifierFunc(
		func(*http.Request) error {
			return nil
		})
	resmod := martian.ResponseModifierFunc(
		func(*http.Response) error {
			return nil
		})

	if _, err := NewResult(reqmod, []ModifierType{Response}); err == nil {
		t.Error("NewResult(reqmod, RESPONSE): got nil, want error")
	}

	if _, err := NewResult(resmod, []ModifierType{Request}); err == nil {
		t.Error("NewResult(resmod, REQUEST): got nil, want error")
	}

	if _, err := NewResult(reqmod, []ModifierType{ModifierType("unknown")}); err == nil {
		t.Error("NewResult(resmod, REQUEST): got nil, want error")
	}
}