Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a0c6412587 | ||
|
|
7fb3569029 |
@@ -31,11 +31,11 @@ exhaustion
|
||||
# Completed Steps
|
||||
|
||||
- 2026-09-28 refuse an invalid `q` on `/v1/image/` (closes #134): a `q`
|
||||
that is not a whole number from 1 to 100, an empty `q` included, is a
|
||||
400 naming `q` and the value, instead of being served at the default
|
||||
85; the route reads `q` with the generator's quality check
|
||||
(`parseFormInt` with `minQuality` and `maxQuality`); only a `q` missing
|
||||
from the URL is still 85; `README.md` states the range.
|
||||
that is not a whole number from 1 to 100 is a 400 naming `q` and the
|
||||
value, instead of being served at the default 85; the route reads `q`
|
||||
with the generator's quality check (`parseFormInt` with `minQuality`
|
||||
and `maxQuality`); an absent or empty `q` is still 85; `README.md`
|
||||
states the range.
|
||||
- 2026-09-28 unknown `PIXA_` environment variables abort startup (closes
|
||||
#133): a variable whose name starts with `PIXA_` but is neither a
|
||||
setting's variable nor `PIXA_CONFIG_PATH` aborts startup naming it, as
|
||||
|
||||
@@ -18,9 +18,9 @@ import (
|
||||
"sneak.berlin/go/pixa/internal/templates"
|
||||
)
|
||||
|
||||
// errInvalidFormField reports a generator form field, or the q parameter of
|
||||
// /v1/image/, whose value is non-numeric or out of range. The offending field
|
||||
// name is wrapped in so the response can name it.
|
||||
// errInvalidFormField reports a generator form field whose value is
|
||||
// non-numeric or out of range. The offending field name is wrapped in so the
|
||||
// response can name it.
|
||||
var errInvalidFormField = errors.New("invalid")
|
||||
|
||||
// Bounds for the generator's quality and ttl fields; the quality bounds also
|
||||
|
||||
@@ -294,24 +294,21 @@ func TestHandleImage_InvalidFitMode_Returns400(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestHandleImage_InvalidQuality_Returns400 verifies that the plain image
|
||||
// route answers a q that is not a whole number from 1 to 100, an empty one
|
||||
// included, with 400 naming q and the value, instead of serving the image at
|
||||
// the default quality 85.
|
||||
// route answers a q that is not a whole number from 1 to 100 with 400 naming
|
||||
// q and the value, instead of serving the image at the default quality 85.
|
||||
func TestHandleImage_InvalidQuality_Returns400(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
query, wantError string
|
||||
q, wantError string
|
||||
}{
|
||||
{"q=banana", `invalid q: not a number, got "banana"`},
|
||||
{"q=0", `invalid q: must be from 1 to 100, got "0"`},
|
||||
{"q=101", `invalid q: must be from 1 to 100, got "101"`},
|
||||
{"q=", `invalid q: not a number, got ""`},
|
||||
{"q=&q=500", `invalid q: not a number, got ""`},
|
||||
{"banana", `invalid q: not a number, got "banana"`},
|
||||
{"0", `invalid q: must be from 1 to 100, got "0"`},
|
||||
{"101", `invalid q: must be from 1 to 100, got "101"`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.query, func(t *testing.T) {
|
||||
t.Run("q="+tt.q, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fix := setupTestHandler(t)
|
||||
@@ -320,7 +317,7 @@ func TestHandleImage_InvalidQuality_Returns400(t *testing.T) {
|
||||
r.Get("/v1/image/*", fix.handler.HandleImage())
|
||||
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
|
||||
"/v1/image/"+fix.goodHost+"/images/photo.jpg/50x50.jpeg?"+tt.query, nil)
|
||||
"/v1/image/"+fix.goodHost+"/images/photo.jpg/50x50.jpeg?q="+tt.q, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(rec, req)
|
||||
|
||||
@@ -102,22 +102,13 @@ func (s *Handlers) parseImageRequest(
|
||||
}
|
||||
}
|
||||
|
||||
// Parse optional quality and fit params. Only a q missing from the URL is
|
||||
// 85. A q in the URL that is not a whole number from 1 to 100, an empty
|
||||
// one included, is refused, checked as the generator checks its quality
|
||||
// field; that check alone would take an empty q as missing.
|
||||
qStr := query.Get("q")
|
||||
if query.Has("q") && qStr == "" {
|
||||
s.respondError(w, `invalid q: not a number, got ""`,
|
||||
http.StatusBadRequest)
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Parse optional quality and fit params. An absent q is 85; a q that is
|
||||
// not a whole number from 1 to 100 is refused, checked as the generator
|
||||
// checks its quality field.
|
||||
req.Quality, err = parseFormInt(query, "q",
|
||||
encurl.DefaultQuality, minQuality, maxQuality)
|
||||
if err != nil {
|
||||
s.respondError(w, fmt.Sprintf("%v, got %q", err, qStr),
|
||||
s.respondError(w, fmt.Sprintf("%v, got %q", err, query.Get("q")),
|
||||
http.StatusBadRequest)
|
||||
|
||||
return nil, false
|
||||
|
||||
Reference in New Issue
Block a user