Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the behavior of default params if they are present #60

Merged
merged 4 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/handler/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ImageHandler(deps *service.Dependencies) http.HandlerFunc {

params := make(map[string]string)
values := r.URL.Query()
if len(values) > 0 {
if len(values) > 0 || len(deps.DefaultParams) > 0 {
vivekmittal marked this conversation as resolved.
Show resolved Hide resolved
for v := range values {
if len(values.Get(v)) != 0 {
params[v] = values.Get(v)
Expand All @@ -58,7 +58,7 @@ func ImageHandler(deps *service.Dependencies) http.HandlerFunc {
w.Header().Set(CacheControlHeader, fmt.Sprintf("public,max-age=%d", config.CacheTime()))
// Ref to Google CDN we support: https://cloud.google.com/cdn/docs/caching#cacheability
w.Header().Set(VaryHeader, "Accept")

cl, _ := w.Write(data)
w.Header().Set(ContentLengthHeader, fmt.Sprintf("%d", cl))
}
Expand Down
27 changes: 24 additions & 3 deletions internal/handler/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"errors"
"fmt"
"github.com/gojek/darkroom/pkg/metrics"
"net/http"
"net/http/httptest"
"testing"

"github.com/gojek/darkroom/pkg/metrics"

"github.com/gojek/darkroom/pkg/config"
"github.com/gojek/darkroom/pkg/service"
"github.com/gojek/darkroom/pkg/storage"
Expand Down Expand Up @@ -37,11 +38,31 @@ func (s *ImageHandlerTestSuite) SetupTest() {
MetricService: s.mockMetricService}
}

func (s *ImageHandlerTestSuite) TestImageHandler() {
func (s *ImageHandlerTestSuite) TestImageHandlerWithoutDefaultParams() {
s.deps.DefaultParams = make(map[string]string)

r, _ := http.NewRequest(http.MethodGet, "/image-valid", nil)
rr := httptest.NewRecorder()
data := []byte("validData")

s.storage.On("Get", mock.Anything, "/image-valid").Return([]byte("validData"), http.StatusOK, nil)
s.storage.On("Get", mock.Anything, "/image-valid").Return(data, http.StatusOK, nil)
ImageHandler(s.deps).ServeHTTP(rr, r)

assert.Equal(s.T(), "validData", rr.Body.String())
assert.Equal(s.T(), http.StatusOK, rr.Code)
}

func (s *ImageHandlerTestSuite) TestImageHandlerWithDefaultParams() {
s.deps.DefaultParams = map[string]string{
"auto": "compress",
}

r, _ := http.NewRequest(http.MethodGet, "/image-valid", nil)
rr := httptest.NewRecorder()
data := []byte("validData")

s.storage.On("Get", mock.Anything, "/image-valid").Return(data, http.StatusOK, nil)
s.manipulator.On("Process", mock.AnythingOfType("service.processSpec")).Return(data, nil)

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

Expand Down
15 changes: 11 additions & 4 deletions pkg/service/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package service

import (
"errors"
"strings"
"time"

"github.com/gojek/darkroom/pkg/logger"
"github.com/gojek/darkroom/pkg/metrics"
"github.com/gojek/darkroom/pkg/regex"
"github.com/prometheus/client_golang/prometheus"
"strings"
"time"

"github.com/gojek/darkroom/pkg/config"
"github.com/gojek/darkroom/pkg/processor/native"
Expand All @@ -25,6 +26,7 @@ type Dependencies struct {
Storage base.Storage
Manipulator Manipulator
MetricService metrics.MetricService
DefaultParams map[string]string
}

// NewDependencies constructs new Dependencies based on the config.DataSource().Kind
Expand All @@ -40,8 +42,13 @@ func NewDependencies(registry *prometheus.Registry) (*Dependencies, error) {
metricService = metrics.NoOpMetricService{}
logger.Warn("NoOpMetricService is being used since metric system is not specified")
}
deps := &Dependencies{Manipulator: NewManipulator(native.NewBildProcessor(), getDefaultParams(), metricService),
MetricService: metricService}
defaultParams := getDefaultParams()
vivekmittal marked this conversation as resolved.
Show resolved Hide resolved
deps := &Dependencies{
Manipulator: NewManipulator(native.NewBildProcessor(), defaultParams, metricService),
MetricService: metricService,
DefaultParams: defaultParams,
}

s := config.DataSource()
if regex.WebFolderMatcher.MatchString(s.Kind) {
deps.Storage = NewWebFolderStorage(s.Value.(config.WebFolder), s.HystrixCommand)
Expand Down