Repository navigation
Expand file tree
/
Copy pathmaxbytes_test.go
More file actions
61 lines (52 loc) · 1.76 KB
/
Copy pathmaxbytes_test.go
File metadata and controls
61 lines (52 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2026 Alvaro J. Genial. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package form
import (
"errors"
"math"
"strings"
"testing"
)
// MaxBytes completes the resource-bounding triad (with MaxSize and MaxDepth):
// it caps how much raw input Decode will buffer before parsing.
func TestMaxBytes(t *testing.T) {
payload := "a=" + strings.Repeat("x", 98) // 100 bytes total
var dst struct {
A string `form:"a"`
}
// Over the bound: a typed limit error.
err := NewDecoder(strings.NewReader(payload)).MaxBytes(99).Decode(&dst)
var fe *Error
if err == nil || !errors.As(err, &fe) || fe.Kind != KindLimit {
t.Errorf("over bound: got %v, want KindLimit", err)
}
// Exactly at the bound: decodes.
if err := NewDecoder(strings.NewReader(payload)).MaxBytes(100).Decode(&dst); err != nil {
t.Errorf("at bound: %v", err)
} else if len(dst.A) != 98 {
t.Errorf("at bound: got %d bytes", len(dst.A))
}
// Default is unbounded (compatibility): the same input decodes with no
// bound configured.
if err := NewDecoder(strings.NewReader(payload)).Decode(&dst); err != nil {
t.Errorf("default: %v", err)
}
// The bound does not apply to already-materialized input.
d := NewDecoder(nil).MaxBytes(1)
if err := d.DecodeString(&dst, payload); err != nil {
t.Errorf("DecodeString exempt: %v", err)
}
}
func TestMaxBytesMaxInt64(t *testing.T) {
// The bound is unexceedable, so it must behave as if unset — not
// overflow into silently reading nothing.
var dst struct{ A string }
d := NewDecoder(strings.NewReader("A=hello")).MaxBytes(math.MaxInt64)
if err := d.Decode(&dst); err != nil {
t.Fatal(err)
}
if dst.A != "hello" {
t.Errorf("input silently dropped: got %+v", dst)
}
}