Skip to content

Commit 43246b9

Browse files
vivekmittalVivek Mittal
and
Vivek Mittal
authored
Fixed the behavior of default params if they are present (#60)
* Fixed the behavior of default params if they are present * Moved the default params check to manipulator * Removed unnecessary imports * Fixed doc comment Co-authored-by: Vivek Mittal <[email protected]>
1 parent 06805b3 commit 43246b9

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

internal/handler/image.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func ImageHandler(deps *service.Dependencies) http.HandlerFunc {
4040

4141
params := make(map[string]string)
4242
values := r.URL.Query()
43-
if len(values) > 0 {
43+
if len(values) > 0 || deps.Manipulator.HasDefaultParams() {
4444
for v := range values {
4545
if len(values.Get(v)) != 0 {
4646
params[v] = values.Get(v)
@@ -58,7 +58,7 @@ func ImageHandler(deps *service.Dependencies) http.HandlerFunc {
5858
w.Header().Set(CacheControlHeader, fmt.Sprintf("public,max-age=%d", config.CacheTime()))
5959
// Ref to Google CDN we support: https://cloud.google.com/cdn/docs/caching#cacheability
6060
w.Header().Set(VaryHeader, "Accept")
61-
61+
6262
cl, _ := w.Write(data)
6363
w.Header().Set(ContentLengthHeader, fmt.Sprintf("%d", cl))
6464
}

internal/handler/image_test.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,28 @@ func (s *ImageHandlerTestSuite) SetupTest() {
3737
MetricService: s.mockMetricService}
3838
}
3939

40-
func (s *ImageHandlerTestSuite) TestImageHandler() {
40+
func (s *ImageHandlerTestSuite) TestImageHandlerWithoutDefaultParams() {
4141
r, _ := http.NewRequest(http.MethodGet, "/image-valid", nil)
4242
rr := httptest.NewRecorder()
43+
data := []byte("validData")
4344

44-
s.storage.On("Get", mock.Anything, "/image-valid").Return([]byte("validData"), http.StatusOK, nil)
45+
s.storage.On("Get", mock.Anything, "/image-valid").Return(data, http.StatusOK, nil)
46+
s.manipulator.On("HasDefaultParams").Return(false)
47+
48+
ImageHandler(s.deps).ServeHTTP(rr, r)
49+
50+
assert.Equal(s.T(), "validData", rr.Body.String())
51+
assert.Equal(s.T(), http.StatusOK, rr.Code)
52+
}
53+
54+
func (s *ImageHandlerTestSuite) TestImageHandlerWithDefaultParams() {
55+
r, _ := http.NewRequest(http.MethodGet, "/image-valid", nil)
56+
rr := httptest.NewRecorder()
57+
data := []byte("validData")
58+
59+
s.storage.On("Get", mock.Anything, "/image-valid").Return(data, http.StatusOK, nil)
60+
s.manipulator.On("HasDefaultParams").Return(true)
61+
s.manipulator.On("Process", mock.AnythingOfType("service.processSpec")).Return(data, nil)
4562

4663
ImageHandler(s.deps).ServeHTTP(rr, r)
4764

pkg/service/dependencies.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33

44
import (
55
"errors"
6+
67
"github.com/gojek/darkroom/pkg/logger"
78
"github.com/gojek/darkroom/pkg/metrics"
89
"github.com/gojek/darkroom/pkg/regex"
@@ -40,8 +41,11 @@ func NewDependencies(registry *prometheus.Registry) (*Dependencies, error) {
4041
metricService = metrics.NoOpMetricService{}
4142
logger.Warn("NoOpMetricService is being used since metric system is not specified")
4243
}
43-
deps := &Dependencies{Manipulator: NewManipulator(native.NewBildProcessor(), getDefaultParams(), metricService),
44-
MetricService: metricService}
44+
deps := &Dependencies{
45+
Manipulator: NewManipulator(native.NewBildProcessor(), getDefaultParams(), metricService),
46+
MetricService: metricService,
47+
}
48+
4549
s := config.DataSource()
4650
if regex.WebFolderMatcher.MatchString(s.Kind) {
4751
deps.Storage = NewWebFolderStorage(s.Value.(config.WebFolder), s.HystrixCommand)

pkg/service/manipulator.go

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const (
4444
type Manipulator interface {
4545
// Process takes ProcessSpec as an argument and returns []byte, error
4646
Process(spec processSpec) ([]byte, error)
47+
48+
// HasDefaultParams returns true if defaultParams are present, returns false otherwise
49+
HasDefaultParams() bool
4750
}
4851

4952
type manipulator struct {
@@ -126,6 +129,11 @@ func (m *manipulator) Process(spec processSpec) ([]byte, error) {
126129
return src, err
127130
}
128131

132+
// HasDefaultParams returns true if defaultParams are present, returns false otherwise
133+
func (m *manipulator) HasDefaultParams() bool {
134+
return len(m.defaultParams) > 0
135+
}
136+
129137
func joinParams(params map[string]string, defaultParams map[string]string) map[string]string {
130138
fp := make(map[string]string)
131139
for p := range defaultParams {

pkg/service/manipulator_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"errors"
5+
56
"github.com/gojek/darkroom/pkg/metrics"
67
"github.com/gojek/darkroom/pkg/processor"
78
"github.com/gojek/darkroom/pkg/processor/native"
@@ -181,6 +182,14 @@ func TestCleanInt(t *testing.T) {
181182
assert.Equal(t, 0, CleanInt("-234"))
182183
}
183184

185+
func TestManipulator_HasDefaultParams(t *testing.T) {
186+
manipulatorWithDefaultParams := NewManipulator(nil, map[string]string{"auto": "compress"}, nil)
187+
manipulatorWithoutDefaultParams := NewManipulator(nil, map[string]string{}, nil)
188+
189+
assert.Equal(t, true, manipulatorWithDefaultParams.HasDefaultParams())
190+
assert.Equal(t, false, manipulatorWithoutDefaultParams.HasDefaultParams())
191+
}
192+
184193
type mockProcessor struct {
185194
mock.Mock
186195
}

pkg/service/mock.go

+5
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ func (m *MockManipulator) Process(spec processSpec) ([]byte, error) {
1212
args := m.Called(spec)
1313
return args.Get(0).([]byte), args.Error(1)
1414
}
15+
16+
func (m *MockManipulator) HasDefaultParams() bool {
17+
args := m.Called()
18+
return args.Get(0).(bool)
19+
}

0 commit comments

Comments
 (0)