Failing tests for the missing validation on the encrypted-URL route and the token generator: an encrypted token carrying an over-limit dimension or an unrecognized fit mode must be rejected with 400; POST /generate with a bad width, height, quality, ttl or fit must return 400, mint no token, and name the field on the page; an empty quality takes 85 and the largest accepted values still give an expiry in the future. Also covers the shared imgcache validator and the plain image route's invalid fit mode. Model: opus-4-8 (implementation); opus-5-5 (rework)
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package imgcache
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateImageRequest(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
req ImageRequest
|
|
wantErr error
|
|
}{
|
|
{
|
|
name: "within bounds",
|
|
req: ImageRequest{Size: Size{Width: 800, Height: 600}, FitMode: FitCover},
|
|
},
|
|
{
|
|
name: "original size and empty fit",
|
|
req: ImageRequest{Size: Size{Width: 0, Height: 0}},
|
|
},
|
|
{
|
|
name: "width over limit",
|
|
req: ImageRequest{Size: Size{Width: MaxDimension + 1, Height: 600}},
|
|
wantErr: ErrDimensionTooLarge,
|
|
},
|
|
{
|
|
name: "height over limit",
|
|
req: ImageRequest{Size: Size{Width: 800, Height: MaxDimension + 1}},
|
|
wantErr: ErrDimensionTooLarge,
|
|
},
|
|
{
|
|
name: "negative width",
|
|
req: ImageRequest{Size: Size{Width: -1, Height: 600}},
|
|
wantErr: ErrInvalidSize,
|
|
},
|
|
{
|
|
name: "invalid fit mode",
|
|
req: ImageRequest{Size: Size{Width: 800, Height: 600}, FitMode: "bogus"},
|
|
wantErr: ErrInvalidFitMode,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := ValidateImageRequest(&tt.req)
|
|
if tt.wantErr == nil {
|
|
if err != nil {
|
|
t.Fatalf("ValidateImageRequest() error = %v, want nil", err)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if !errors.Is(err, tt.wantErr) {
|
|
t.Fatalf("ValidateImageRequest() error = %v, want %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|