Skip to content

Conversation

@derekmpage
Copy link
Contributor

Fixes #228

Changes the /bytes and /stream-bytes endpoints to honor the MAX_BODY_SIZE configuration variable instead of using a hardcoded 100KB limit.

Changes:

  • Modified handleBytes to be a method on *HTTPBin to access MaxBodySize
  • Replaced hardcoded 100KB limit with h.MaxBodySize check
  • Returns HTTP 400 with descriptive error when requested size exceeds limit
  • Updated tests to expect 400 for oversized requests

Testing:

  • All existing tests pass
  • Manual testing verified with custom MAX_BODY_SIZE values

Test Honoring MAX_BODY_SIZE

$ curl -v http://localhost:18080/bytes/10485760 -o /tmp/bigfile 2>&1 | head -30
* Host localhost:18080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying [::1]:18080...
* Connected to localhost (::1) port 18080
> GET /bytes/10485760 HTTP/1.1
> Host: localhost:18080
> User-Agent: curl/8.6.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Origin: *
< Content-Length: 10485760
< Content-Type: application/octet-stream
< Date: Wed, 07 Jan 2026 16:24:32 GMT
< 
{ [3902 bytes data]
100 10.0M  100 10.0M    0     0  79.3M      0 --:--:-- --:--:-- --:--:-- 80.0M
* Connection #0 to host localhost left intact

File larger then 100k -> honor MAX_BODY_SIZE

$ dpage@MA-DPAGE  ~/pgit/go-httpbin   main ± $ ls -lh /tmp/bigfile
-rw-r--r-- 1 dpage dpage 10M Jan  7 11:24 /tmp/bigfile

Test exceeding MAX_BODY_SIZE

$ curl -v http://localhost:18080/bytes/10485761 2>&1 | head -30
* Host localhost:18080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
 0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying [::1]:18080...
* Connected to localhost (::1) port 18080
> GET /bytes/10485761 HTTP/1.1
> Host: localhost:18080
> User-Agent: curl/8.6.0
> Accept: */*
> 
< HTTP/1.1 400 Bad Request
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Origin: *
< Content-Type: application/json; charset=utf-8
< Date: Wed, 07 Jan 2026 16:24:39 GMT
< Content-Length: 122
< 
{ [122 bytes data]
100   122  100   122    0     0   161k      0 --:--:-- --:--:-- --:--:--  119k
* Connection #0 to host localhost left intact
{
 "status_code": 400,
 "error": "Bad Request",
 "detail": "invalid byte count: 10485761 not in range [1, 10485760]"
}

@mccutchen mccutchen changed the title Honor MAX_BODY_SIZE in /bytes and /stream-bytes endpoints fixes #228 fix: respect max body size config in /bytes and /stream-bytes endpoints Jan 7, 2026
@codecov
Copy link

codecov bot commented Jan 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.13%. Comparing base (1b709cc) to head (9b54746).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #229   +/-   ##
=======================================
  Coverage   95.12%   95.13%           
=======================================
  Files          10       10           
  Lines        1827     1828    +1     
=======================================
+ Hits         1738     1739    +1     
  Misses         52       52           
  Partials       37       37           
Files with missing lines Coverage Δ
httpbin/handlers.go 99.25% <100.00%> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mccutchen mccutchen changed the title fix: respect max body size config in /bytes and /stream-bytes endpoints fix: respect MAX_BODY_SIZE in /bytes and /stream-bytes endpoints Jan 7, 2026
@mccutchen mccutchen merged commit 200ff7d into mccutchen:main Jan 7, 2026
5 checks passed
@mccutchen
Copy link
Owner

Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: MAX_BODY_SIZE ignored for /bytes and /stream-bytes

2 participants