Ejemplo n.º 1
0
func TestRotateParserParse(t *testing.T) {
	type TC struct {
		query              url.Values
		expectedParams     imageserver.Params
		expectedParamError string
	}
	for _, tc := range []TC{
		{},
		{
			query: url.Values{"rotation": {"90"}},
			expectedParams: imageserver.Params{rotateParam: imageserver.Params{
				"rotation": 90.0,
			}},
		},
		{
			query: url.Values{"interpolation": {"cubic"}},
			expectedParams: imageserver.Params{rotateParam: imageserver.Params{
				"interpolation": "cubic",
			}},
		},
		{
			query: url.Values{"background": {"FF0000"}},
			expectedParams: imageserver.Params{rotateParam: imageserver.Params{
				"background": "FF0000",
			}},
		},
		{
			query:              url.Values{"rotation": {"invalid"}},
			expectedParamError: rotateParam + ".rotation",
		},
	} {
		func() {
			defer func() {
				if t.Failed() {
					t.Logf("%#v", tc)
				}
			}()
			u := &url.URL{
				Scheme:   "http",
				Host:     "localhost",
				RawQuery: tc.query.Encode(),
			}
			req, err := http.NewRequest("GET", u.String(), nil)
			if err != nil {
				t.Fatal(err)
			}
			prc := &RotateParser{}
			params := imageserver.Params{}
			err = prc.Parse(req, params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && tc.expectedParamError == err.Param {
					return
				}
				t.Fatal(err)
			}
			if params.String() != tc.expectedParams.String() {
				t.Fatalf("unexpected params: got %s, want %s", params, tc.expectedParams)
			}
		}()
	}
}
Ejemplo n.º 2
0
func TestParse(t *testing.T) {
	p := &Parser{}
	for _, tc := range []struct {
		name               string
		query              url.Values
		expectedParams     imageserver.Params
		expectedParamError string
	}{
		{
			name: "Empty",
		},
		{
			name:  "Width",
			query: url.Values{"width": {"100"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"width": 100,
			}},
		},
		{
			name:  "Height",
			query: url.Values{"height": {"100"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"height": 100,
			}},
		},
		{
			name:  "Interpolation",
			query: url.Values{"interpolation": {"lanczos3"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"interpolation": "lanczos3",
			}},
		},
		{
			name:  "Mode",
			query: url.Values{"mode": {"resize"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"mode": "resize",
			}},
		},
		{
			name:               "WidthInvalid",
			query:              url.Values{"width": {"invalid"}},
			expectedParamError: globalParam + ".width",
		},
		{
			name:               "HeightInvalid",
			query:              url.Values{"height": {"invalid"}},
			expectedParamError: globalParam + ".height",
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			u := &url.URL{
				Scheme:   "http",
				Host:     "localhost",
				RawQuery: tc.query.Encode(),
			}
			req, err := http.NewRequest("GET", u.String(), nil)
			if err != nil {
				t.Fatal(err)
			}
			params := imageserver.Params{}
			err = p.Parse(req, params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && tc.expectedParamError == err.Param {
					return
				}
				t.Fatal(err)
			}
			if params.String() != tc.expectedParams.String() {
				t.Fatalf("unexpected params: got %s, want %s", params, tc.expectedParams)
			}
		})
	}
}
Ejemplo n.º 3
0
func TestParse(t *testing.T) {
	p := &Parser{}
	for _, tc := range []struct {
		name               string
		query              url.Values
		expectedParams     imageserver.Params
		expectedParamError string
	}{
		{
			name: "Empty",
		},
		{
			name:  "Width",
			query: url.Values{"width": {"100"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"width": 100,
			}},
		},
		{
			name:  "Height",
			query: url.Values{"height": {"100"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"height": 100,
			}},
		},
		{
			name:  "Fill",
			query: url.Values{"fill": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"fill": true,
			}},
		},
		{
			name:  "IgnoreRatio",
			query: url.Values{"ignore_ratio": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"ignore_ratio": true,
			}},
		},
		{
			name:  "OnlyShrinkLarger",
			query: url.Values{"only_shrink_larger": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"only_shrink_larger": true,
			}},
		},
		{
			name:  "OnlyEnlargeSmaller",
			query: url.Values{"only_enlarge_smaller": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"only_enlarge_smaller": true,
			}},
		},
		{
			name:  "Background",
			query: url.Values{"background": {"123abc"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"background": "123abc",
			}},
		},
		{
			name:  "Extent",
			query: url.Values{"extent": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"extent": true,
			}},
		},
		{
			name:  "Format",
			query: url.Values{"format": {"jpeg"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"format": "jpeg",
			}},
		},
		{
			name:  "Quality",
			query: url.Values{"quality": {"75"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"quality": 75,
			}},
		},
		{
			name:               "WidthInvalid",
			query:              url.Values{"width": {"invalid"}},
			expectedParamError: globalParam + ".width",
		},
		{
			name:               "HeightInvalid",
			query:              url.Values{"height": {"invalid"}},
			expectedParamError: globalParam + ".height",
		},
		{
			name:               "FillInvalid",
			query:              url.Values{"fill": {"invalid"}},
			expectedParamError: globalParam + ".fill",
		},
		{
			name:               "IgnoreRatioInvalid",
			query:              url.Values{"ignore_ratio": {"invalid"}},
			expectedParamError: globalParam + ".ignore_ratio",
		},
		{
			name:               "OnlyShrinkLargerInvalid",
			query:              url.Values{"only_shrink_larger": {"invalid"}},
			expectedParamError: globalParam + ".only_shrink_larger",
		},
		{
			name:               "OnlyEnlargeSmallerInvalid",
			query:              url.Values{"only_enlarge_smaller": {"invalid"}},
			expectedParamError: globalParam + ".only_enlarge_smaller",
		},
		{
			name:               "ExtentInvalid",
			query:              url.Values{"extent": {"invalid"}},
			expectedParamError: globalParam + ".extent",
		},
		{
			name:               "QualityInvalid",
			query:              url.Values{"quality": {"invalid"}},
			expectedParamError: globalParam + ".quality",
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			u := &url.URL{
				Scheme:   "http",
				Host:     "localhost",
				RawQuery: tc.query.Encode(),
			}
			req, err := http.NewRequest("GET", u.String(), nil)
			if err != nil {
				t.Fatal(err)
			}
			params := imageserver.Params{}
			err = p.Parse(req, params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && tc.expectedParamError == err.Param {
					return
				}
				t.Fatal(err)
			}
			if params.String() != tc.expectedParams.String() {
				t.Fatalf("unexpected params: got %s, want %s", params, tc.expectedParams)
			}
		})
	}
}
Ejemplo n.º 4
0
func TestParse(t *testing.T) {
	type TC struct {
		query              url.Values
		expectedParams     imageserver.Params
		expectedParamError string
	}
	for _, tc := range []TC{
		{},
		{
			query: url.Values{"width": {"100"}},
			expectedParams: imageserver.Params{Param: imageserver.Params{
				"width": 100,
			}},
		},
		{
			query: url.Values{"height": {"100"}},
			expectedParams: imageserver.Params{Param: imageserver.Params{
				"height": 100,
			}},
		},
		{
			query: url.Values{"resampling": {"lanczos"}},
			expectedParams: imageserver.Params{Param: imageserver.Params{
				"resampling": "lanczos",
			}},
		},
		{
			query: url.Values{"mode": {"fit"}},
			expectedParams: imageserver.Params{Param: imageserver.Params{
				"mode": "fit",
			}},
		},
		{
			query:              url.Values{"width": {"invalid"}},
			expectedParamError: Param + ".width",
		},
		{
			query:              url.Values{"height": {"invalid"}},
			expectedParamError: Param + ".height",
		},
	} {
		func() {
			defer func() {
				if t.Failed() {
					t.Logf("%#v", tc)
				}
			}()
			u := &url.URL{
				Scheme:   "http",
				Host:     "localhost",
				RawQuery: tc.query.Encode(),
			}
			req, err := http.NewRequest("GET", u.String(), nil)
			if err != nil {
				t.Fatal(err)
			}
			p := &Parser{}
			params := imageserver.Params{}
			err = p.Parse(req, params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && tc.expectedParamError == err.Param {
					return
				}
				t.Fatal(err)
			}
			if params.String() != tc.expectedParams.String() {
				t.Fatalf("unexpected params: got %s, want %s", params, tc.expectedParams)
			}
		}()
	}
}
Ejemplo n.º 5
0
func TestRotateParserParse(t *testing.T) {
	prs := &RotateParser{}
	for _, tc := range []struct {
		name               string
		query              url.Values
		expectedParams     imageserver.Params
		expectedParamError string
	}{
		{
			name: "Empty",
		},
		{
			name:  "Rotation",
			query: url.Values{"rotation": {"90"}},
			expectedParams: imageserver.Params{rotateParam: imageserver.Params{
				"rotation": 90.0,
			}},
		},
		{
			name:  "Interpolation",
			query: url.Values{"interpolation": {"cubic"}},
			expectedParams: imageserver.Params{rotateParam: imageserver.Params{
				"interpolation": "cubic",
			}},
		},
		{
			name:  "Background",
			query: url.Values{"background": {"FF0000"}},
			expectedParams: imageserver.Params{rotateParam: imageserver.Params{
				"background": "FF0000",
			}},
		},
		{
			name:               "RotationInvalid",
			query:              url.Values{"rotation": {"invalid"}},
			expectedParamError: rotateParam + ".rotation",
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			u := &url.URL{
				Scheme:   "http",
				Host:     "localhost",
				RawQuery: tc.query.Encode(),
			}
			req, err := http.NewRequest("GET", u.String(), nil)
			if err != nil {
				t.Fatal(err)
			}
			params := imageserver.Params{}
			err = prs.Parse(req, params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && tc.expectedParamError == err.Param {
					return
				}
				t.Fatal(err)
			}
			if params.String() != tc.expectedParams.String() {
				t.Fatalf("unexpected params: got %s, want %s", params, tc.expectedParams)
			}
		})
	}
}
func TestParse(t *testing.T) {
	type TC struct {
		query              url.Values
		expectedParams     imageserver.Params
		expectedParamError string
	}
	for _, tc := range []TC{
		{},
		{
			query: url.Values{"ignore_ratio": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"ignore_ratio": true,
			}},
		},
		{
			query: url.Values{"fill": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"fill": true,
			}},
		},
		{
			query: url.Values{"only_shrink_larger": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"only_shrink_larger": true,
			}},
		},
		{
			query: url.Values{"only_enlarge_smaller": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"only_enlarge_smaller": true,
			}},
		},
		{
			query: url.Values{"extent": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"extent": true,
			}},
		},

		{
			query: url.Values{"monochrome": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"monochrome": true,
			}},
		},
		{
			query: url.Values{"grey": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"grey": true,
			}},
		},

		{
			query: url.Values{"no_strip": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"no_strip": true,
			}},
		},
		{
			query: url.Values{"trim": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"trim": true,
			}},
		},
		{
			query: url.Values{"no_interlace": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"no_interlace": true,
			}},
		},
		{
			query: url.Values{"flip": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"flip": true,
			}},
		},
		{
			query: url.Values{"flop": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"flop": true,
			}},
		},
		{
			query: url.Values{"w": {"100"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"w": 100,
			}},
		},
		{
			query: url.Values{"h": {"100"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"h": 100,
			}},
		},
		{
			query: url.Values{"rotate": {"90"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"rotate": 90,
			}},
		},
		{
			query: url.Values{"q": {"75"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"q": 75,
			}},
		},
		{
			query: url.Values{"bg": {"fafafa"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"bg": "fafafa",
			}},
		},
		{
			query: url.Values{"gravity": {"se"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"gravity": "se",
			}},
		},
		{
			query: url.Values{"crop": {"200,200,0,0"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"crop": "200,200,0,0",
			}},
		},

		{
			query: url.Values{"format": {"jpeg"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"format": "jpeg",
			}},
		},
		{
			query:              url.Values{"ignore_ratio": {"invalid"}},
			expectedParamError: globalParam + ".ignore_ratio",
		},

		{
			query:              url.Values{"fill": {"invalid"}},
			expectedParamError: globalParam + ".fill",
		},
		{
			query:              url.Values{"only_shrink_larger": {"invalid"}},
			expectedParamError: globalParam + ".only_shrink_larger",
		},
		{
			query:              url.Values{"only_enlarge_smaller": {"invalid"}},
			expectedParamError: globalParam + ".only_enlarge_smaller",
		},
		{
			query:              url.Values{"extent": {"invalid"}},
			expectedParamError: globalParam + ".extent",
		},
		{
			query:              url.Values{"monochrome": {"invalid"}},
			expectedParamError: globalParam + ".monochrome",
		},
		{
			query:              url.Values{"grey": {"invalid"}},
			expectedParamError: globalParam + ".grey",
		},
		{
			query:              url.Values{"no_strip": {"invalid"}},
			expectedParamError: globalParam + ".no_strip",
		},
		{
			query:              url.Values{"trim": {"invalid"}},
			expectedParamError: globalParam + ".trim",
		},
		{
			query:              url.Values{"no_interlace": {"invalid"}},
			expectedParamError: globalParam + ".no_interlace",
		},
		{
			query:              url.Values{"flip": {"invalid"}},
			expectedParamError: globalParam + ".flip",
		},
		{
			query:              url.Values{"flop": {"invalid"}},
			expectedParamError: globalParam + ".flop",
		},

		{
			query:              url.Values{"w": {"invalid"}},
			expectedParamError: globalParam + ".w",
		},
		{
			query:              url.Values{"h": {"invalid"}},
			expectedParamError: globalParam + ".h",
		},
		{
			query:              url.Values{"rotate": {"invalid"}},
			expectedParamError: globalParam + ".rotate",
		},
		{
			query:              url.Values{"density": {"invalid"}},
			expectedParamError: globalParam + ".density",
		},

		{
			query:              url.Values{"q": {"invalid"}},
			expectedParamError: globalParam + ".q",
		},
	} {
		func() {
			defer func() {
				if t.Failed() {
					t.Logf("%#v", tc)
				}
			}()
			u := &url.URL{
				Scheme:   "http",
				Host:     "localhost",
				RawQuery: tc.query.Encode(),
			}
			req, err := http.NewRequest("GET", u.String(), nil)
			if err != nil {
				t.Fatal(err)
			}
			p := &Parser{}
			params := imageserver.Params{}
			err = p.Parse(req, params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && tc.expectedParamError == err.Param {
					return
				}
				t.Fatal(err)
			}
			if params.String() != tc.expectedParams.String() {
				t.Fatalf("unexpected params: got %s, want %s", params, tc.expectedParams)
			}
		}()
	}
}
Ejemplo n.º 7
0
func TestParse(t *testing.T) {
	type TC struct {
		query              url.Values
		expectedParams     imageserver.Params
		expectedParamError string
	}
	for _, tc := range []TC{
		{},
		{
			query: url.Values{"width": {"100"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"width": 100,
			}},
		},
		{
			query: url.Values{"height": {"100"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"height": 100,
			}},
		},
		{
			query: url.Values{"fill": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"fill": true,
			}},
		},
		{
			query: url.Values{"ignore_ratio": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"ignore_ratio": true,
			}},
		},
		{
			query: url.Values{"only_shrink_larger": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"only_shrink_larger": true,
			}},
		},
		{
			query: url.Values{"only_enlarge_smaller": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"only_enlarge_smaller": true,
			}},
		},
		{
			query: url.Values{"background": {"123abc"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"background": "123abc",
			}},
		},
		{
			query: url.Values{"extent": {"true"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"extent": true,
			}},
		},
		{
			query: url.Values{"format": {"jpeg"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"format": "jpeg",
			}},
		},
		{
			query: url.Values{"quality": {"75"}},
			expectedParams: imageserver.Params{globalParam: imageserver.Params{
				"quality": 75,
			}},
		},
		{
			query:              url.Values{"width": {"invalid"}},
			expectedParamError: globalParam + ".width",
		},
		{
			query:              url.Values{"height": {"invalid"}},
			expectedParamError: globalParam + ".height",
		},
		{
			query:              url.Values{"fill": {"invalid"}},
			expectedParamError: globalParam + ".fill",
		},
		{
			query:              url.Values{"ignore_ratio": {"invalid"}},
			expectedParamError: globalParam + ".ignore_ratio",
		},
		{
			query:              url.Values{"only_shrink_larger": {"invalid"}},
			expectedParamError: globalParam + ".only_shrink_larger",
		},
		{
			query:              url.Values{"only_enlarge_smaller": {"invalid"}},
			expectedParamError: globalParam + ".only_enlarge_smaller",
		},
		{
			query:              url.Values{"extent": {"invalid"}},
			expectedParamError: globalParam + ".extent",
		},
		{
			query:              url.Values{"quality": {"invalid"}},
			expectedParamError: globalParam + ".quality",
		},
	} {
		func() {
			defer func() {
				if t.Failed() {
					t.Logf("%#v", tc)
				}
			}()
			u := &url.URL{
				Scheme:   "http",
				Host:     "localhost",
				RawQuery: tc.query.Encode(),
			}
			req, err := http.NewRequest("GET", u.String(), nil)
			if err != nil {
				t.Fatal(err)
			}
			p := &Parser{}
			params := imageserver.Params{}
			err = p.Parse(req, params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && tc.expectedParamError == err.Param {
					return
				}
				t.Fatal(err)
			}
			if params.String() != tc.expectedParams.String() {
				t.Fatalf("unexpected params: got %s, want %s", params, tc.expectedParams)
			}
		}()
	}
}