Skip to content

Commit

Permalink
test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
cshum committed Mar 14, 2024
1 parent 419a20e commit 45be35e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestBasic(t *testing.T) {
"-imagor-cache-header-ttl", "169h",
"-imagor-cache-header-swr", "167h",
"-http-loader-insecure-skip-verify-transport",
"-http-loader-override-response-headers", "cache-control,content-type",
"-http-loader-base-url", "https://www.example.com/foo.org",
})
app := srv.App.(*imagor.Imagor)
Expand All @@ -85,6 +86,7 @@ func TestBasic(t *testing.T) {
httpLoader := app.Loaders[0].(*httploader.HTTPLoader)
assert.True(t, httpLoader.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify)
assert.Equal(t, "https://www.example.com/foo.org", httpLoader.BaseURL.String())
assert.Equal(t, []string{"cache-control", "content-type"}, httpLoader.OverrideResponseHeaders)
}

func TestVersion(t *testing.T) {
Expand Down
25 changes: 25 additions & 0 deletions imagor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,31 @@ func TestWithRaw(t *testing.T) {
assert.Equal(t, "bar", w.Header().Get("Content-Type"))
}

func TestWithOverrideHeader(t *testing.T) {
app := New(
WithDebug(true),
WithUnsafe(true),
WithLogger(zap.NewExample()),
WithLoaders(loaderFunc(func(r *http.Request, image string) (*Blob, error) {
blob := NewBlobFromBytes([]byte("foo"))
blob.SetContentType("bar")
blob.Header = make(http.Header)
blob.Header.Set("Content-Type", "tada")
blob.Header.Set("Foo", "bar")
blob.Header.Set("asdf", "fghj")
return blob, nil
})),
)
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/filters:fill(red):raw()/gopher.png", nil))
assert.Equal(t, 200, w.Code)
assert.Equal(t, "foo", w.Body.String())
assert.Equal(t, "script-src 'none'", w.Header().Get("Content-Security-Policy"))
assert.Equal(t, "tada", w.Header().Get("Content-Type"))
assert.Equal(t, "fghj", w.Header().Get("ASDF"))
}

func TestNewBlobFromPathNotFound(t *testing.T) {
loader := loaderFunc(func(r *http.Request, image string) (*Blob, error) {
return NewBlobFromFile("./non-exists-path"), nil
Expand Down
33 changes: 32 additions & 1 deletion loader/httploader/httploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (t testTransport) RoundTrip(r *http.Request) (w *http.Response, err error)
w = &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(res)),
Header: map[string][]string{},
Header: make(http.Header),
}
w.Header.Set("Content-Type", "image/jpeg")
return
Expand All @@ -48,6 +48,7 @@ type test struct {
name string
target string
result string
header map[string]string
err string
}

Expand Down Expand Up @@ -80,6 +81,11 @@ func doTests(t *testing.T, loader imagor.Loader, tests []test) {
}
assert.Equal(t, tt.err, msg)
}
if tt.header != nil {
for key, val := range tt.header {
assert.Equal(t, val, b.Header.Get(key))
}
}
})
}
}
Expand Down Expand Up @@ -492,6 +498,31 @@ func TestWithForwardHeadersOverrideUserAgent(t *testing.T) {
})
}

func TestWithOverrideResponseHeader(t *testing.T) {
doTests(t, New(
WithTransport(roundTripFunc(func(r *http.Request) (w *http.Response, err error) {
res := &http.Response{
StatusCode: http.StatusOK,
Header: map[string][]string{},
Body: io.NopCloser(strings.NewReader("ok")),
}
res.Header.Set("Content-Type", "image/jpeg")
res.Header.Set("Foo", "Bar")
return res, nil
})),
WithOverrideResponseHeaders("foo"),
), []test{
{
name: "user agent",
target: "https://foo.bar/baz",
result: "ok",
header: map[string]string{
"Foo": "Bar",
},
},
})
}

func TestWithForwardClientHeaders(t *testing.T) {
doTests(t, New(
WithTransport(roundTripFunc(func(r *http.Request) (w *http.Response, err error) {
Expand Down

0 comments on commit 45be35e

Please sign in to comment.