@@ -10,6 +10,7 @@ import (
1010 "encoding/json"
1111 "errors"
1212 "fmt"
13+ "io"
1314 "net/http"
1415 "net/url"
1516 "strings"
@@ -205,6 +206,16 @@ func (b *badReader) Read([]byte) (int, error) {
205206
206207func (b * badReader ) Close () error { return errors .New ("bad reader" ) }
207208
209+ // infiniteReader is an io.Reader that returns zeros indefinitely.
210+ type infiniteReader struct {}
211+
212+ func (infiniteReader ) Read (p []byte ) (int , error ) {
213+ for i := range p {
214+ p [i ] = 0
215+ }
216+ return len (p ), nil
217+ }
218+
208219func TestValidatePayload_BadRequestBody (t * testing.T ) {
209220 t .Parallel ()
210221 tests := []struct {
@@ -228,6 +239,35 @@ func TestValidatePayload_BadRequestBody(t *testing.T) {
228239 }
229240}
230241
242+ func TestValidatePayload_OversizedBody (t * testing.T ) {
243+ t .Parallel ()
244+ tests := []struct {
245+ contentType string
246+ }{
247+ {contentType : "application/json" },
248+ {contentType : "application/x-www-form-urlencoded" },
249+ }
250+
251+ for i , tt := range tests {
252+ t .Run (fmt .Sprintf ("test #%v" , i ), func (t * testing.T ) {
253+ t .Parallel ()
254+ // Simulate a reader that reports more than maxPayloadSize bytes.
255+ oversized := io .LimitReader (infiniteReader {}, maxPayloadSize + 1 )
256+ req := & http.Request {
257+ Header : http.Header {"Content-Type" : []string {tt .contentType }},
258+ Body : io .NopCloser (oversized ),
259+ }
260+ _ , err := ValidatePayload (req , nil )
261+ if err == nil {
262+ t .Fatal ("ValidatePayload returned nil; want error for oversized body" )
263+ }
264+ if want := "webhook payload exceeds maximum allowed size" ; err .Error () != want {
265+ t .Errorf ("ValidatePayload error = %q, want %q" , err .Error (), want )
266+ }
267+ })
268+ }
269+ }
270+
231271func TestValidatePayload_InvalidContentTypeParams (t * testing.T ) {
232272 t .Parallel ()
233273 req , err := http .NewRequest ("POST" , "http://localhost/event" , nil )
0 commit comments