Skip to content

Commit d83da84

Browse files
committed
Testing if an ImageOption false value, was in fact requested
1 parent 7190e85 commit d83da84

5 files changed

Lines changed: 100 additions & 21 deletions

File tree

‎image.go‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ func Resize(buf []byte, o ImageOptions) (Image, error) {
9393
opts := BimgOptions(o)
9494
opts.Embed = true
9595

96-
if !o.NoCrop {
97-
opts.Crop = true
96+
if o.IsDefinedField.NoCrop {
97+
opts.Crop = !o.NoCrop
9898
}
9999

100100
return Process(buf, opts)
@@ -171,9 +171,8 @@ func Enlarge(buf []byte, o ImageOptions) (Image, error) {
171171
opts := BimgOptions(o)
172172
opts.Enlarge = true
173173

174-
if !o.NoCrop {
175-
opts.Crop = true
176-
}
174+
// Since both width & height is required, we allow cropping by default.
175+
opts.Crop = !o.NoCrop
177176

178177
return Process(buf, opts)
179178
}
@@ -259,8 +258,8 @@ func Zoom(buf []byte, o ImageOptions) (Image, error) {
259258
opts.AreaWidth = o.AreaWidth
260259
opts.AreaHeight = o.AreaHeight
261260

262-
if o.NoCrop == false {
263-
opts.Crop = true
261+
if o.IsDefinedField.NoCrop {
262+
opts.Crop = !o.NoCrop
264263
}
265264
}
266265

‎image_test.go‎

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,74 @@ import (
66
)
77

88
func TestImageResize(t *testing.T) {
9-
opts := ImageOptions{Width: 300, Height: 300}
10-
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
9+
t.Run("Width and Height defined", func(t *testing.T) {
10+
opts := ImageOptions{Width: 300, Height: 300}
11+
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
12+
13+
img, err := Resize(buf, opts)
14+
if err != nil {
15+
t.Errorf("Cannot process image: %s", err)
16+
}
17+
if img.Mime != "image/jpeg" {
18+
t.Error("Invalid image MIME type")
19+
}
20+
if assertSize(img.Body, opts.Width, opts.Height) != nil {
21+
t.Errorf("Invalid image size, expected: %dx%d", opts.Width, opts.Height)
22+
}
23+
})
24+
25+
t.Run("Width defined", func(t *testing.T) {
26+
opts := ImageOptions{Width: 300}
27+
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
28+
29+
img, err := Resize(buf, opts)
30+
if err != nil {
31+
t.Errorf("Cannot process image: %s", err)
32+
}
33+
if img.Mime != "image/jpeg" {
34+
t.Error("Invalid image MIME type")
35+
}
36+
if err := assertSize(img.Body, 300, 404); err != nil {
37+
t.Error(err)
38+
}
39+
})
40+
41+
t.Run("Width defined with NoCrop=false", func(t *testing.T) {
42+
opts := ImageOptions{Width: 300, NoCrop: false, IsDefinedField: IsDefinedField{NoCrop: true}}
43+
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
44+
45+
img, err := Resize(buf, opts)
46+
if err != nil {
47+
t.Errorf("Cannot process image: %s", err)
48+
}
49+
if img.Mime != "image/jpeg" {
50+
t.Error("Invalid image MIME type")
51+
}
52+
53+
// The original image is 550x740
54+
if err := assertSize(img.Body, 300, 740); err != nil {
55+
t.Error(err)
56+
}
57+
})
58+
59+
t.Run("Width defined with NoCrop=true", func(t *testing.T) {
60+
opts := ImageOptions{Width: 300, NoCrop: true, IsDefinedField: IsDefinedField{NoCrop: true}}
61+
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
62+
63+
img, err := Resize(buf, opts)
64+
if err != nil {
65+
t.Errorf("Cannot process image: %s", err)
66+
}
67+
if img.Mime != "image/jpeg" {
68+
t.Error("Invalid image MIME type")
69+
}
70+
71+
// The original image is 550x740
72+
if err := assertSize(img.Body, 300, 404); err != nil {
73+
t.Error(err)
74+
}
75+
})
1176

12-
img, err := Resize(buf, opts)
13-
if err != nil {
14-
t.Errorf("Cannot process image: %s", err)
15-
}
16-
if img.Mime != "image/jpeg" {
17-
t.Error("Invalid image MIME type")
18-
}
19-
if assertSize(img.Body, opts.Width, opts.Height) != nil {
20-
t.Errorf("Invalid image size, expected: %dx%d", opts.Width, opts.Height)
21-
}
2277
}
2378

2479
func TestImageFit(t *testing.T) {

‎options.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import "gopkg.in/h2non/bimg.v1"
44

55
// ImageOptions represent all the supported image transformation params as first level members
66
type ImageOptions struct {
7+
IsDefinedField
8+
79
Width int
810
Height int
911
AreaWidth int
@@ -41,6 +43,20 @@ type ImageOptions struct {
4143
Operations PipelineOperations
4244
}
4345

46+
// IsDefinedField holds boolean ImageOptions fields. If true it means the field was specified in the request. This
47+
// metadata allows for sane usage of default (false) values.
48+
type IsDefinedField struct {
49+
Flip bool
50+
Flop bool
51+
Force bool
52+
Embed bool
53+
NoCrop bool
54+
NoReplicate bool
55+
NoRotation bool
56+
NoProfile bool
57+
StripMetadata bool
58+
}
59+
4460
// PipelineOperation represents the structure for an operation field.
4561
type PipelineOperation struct {
4662
Name string `json:"operation"`

‎params.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,46 +190,55 @@ func coerceOpacity(io *ImageOptions, param interface{}) (err error) {
190190

191191
func coerceFlip(io *ImageOptions, param interface{}) (err error) {
192192
io.Flip, err = coerceTypeBool(param)
193+
io.IsDefinedField.Flip = true
193194
return err
194195
}
195196

196197
func coerceFlop(io *ImageOptions, param interface{}) (err error) {
197198
io.Flop, err = coerceTypeBool(param)
199+
io.IsDefinedField.Flop = true
198200
return err
199201
}
200202

201203
func coerceNoCrop(io *ImageOptions, param interface{}) (err error) {
202204
io.NoCrop, err = coerceTypeBool(param)
205+
io.IsDefinedField.NoCrop = true
203206
return err
204207
}
205208

206209
func coerceNoProfile(io *ImageOptions, param interface{}) (err error) {
207210
io.NoProfile, err = coerceTypeBool(param)
211+
io.IsDefinedField.NoProfile = true
208212
return err
209213
}
210214

211215
func coerceNoRotation(io *ImageOptions, param interface{}) (err error) {
212216
io.NoRotation, err = coerceTypeBool(param)
217+
io.IsDefinedField.NoRotation = true
213218
return err
214219
}
215220

216221
func coerceNoReplicate(io *ImageOptions, param interface{}) (err error) {
217222
io.NoReplicate, err = coerceTypeBool(param)
223+
io.IsDefinedField.NoReplicate = true
218224
return err
219225
}
220226

221227
func coerceForce(io *ImageOptions, param interface{}) (err error) {
222228
io.Force, err = coerceTypeBool(param)
229+
io.IsDefinedField.Force = true
223230
return err
224231
}
225232

226233
func coerceEmbed(io *ImageOptions, param interface{}) (err error) {
227234
io.Embed, err = coerceTypeBool(param)
235+
io.IsDefinedField.Embed = true
228236
return err
229237
}
230238

231239
func coerceStripMeta(io *ImageOptions, param interface{}) (err error) {
232240
io.StripMetadata, err = coerceTypeBool(param)
241+
io.IsDefinedField.StripMetadata = true
233242
return err
234243
}
235244

‎server_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestCrop(t *testing.T) {
7878
func TestResize(t *testing.T) {
7979
ts := testServer(controller(Resize))
8080
buf := readFile("large.jpg")
81-
url := ts.URL + "?width=300"
81+
url := ts.URL + "?width=300&nocrop=false"
8282
defer ts.Close()
8383

8484
res, err := http.Post(url, "image/jpeg", buf)
@@ -426,7 +426,7 @@ func assertSize(buf []byte, width, height int) error {
426426
return err
427427
}
428428
if size.Width != width || size.Height != height {
429-
return fmt.Errorf("Invalid image size: %dx%d", size.Width, size.Height)
429+
return fmt.Errorf("Invalid image size: %dx%d, expected: %dx%d", size.Width, size.Height, width, height)
430430
}
431431
return nil
432432
}

0 commit comments

Comments
 (0)
⌂ 换网址