Skip to content

Commit

Permalink
Only close if connection returns fewer bytes than requested (#525)
Browse files Browse the repository at this point in the history
Fixes #524
  • Loading branch information
hadley authored Sep 3, 2024
1 parent 8c8abd1 commit 5237e77
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# httr2 (development version)

* `req_body_file()` now works with files >64kb once more (#524).
* New `req_perform_connection()` for working with streaming data. Unlike `req_perform_stream()` which uses callbacks, `req_perform_connection()` returns a regular response object with a connection as the body. It's paired with `resp_stream_bytes()`, `resp_stream_lines()`, and `resp_stream_sse()` that allows you to stream chunks as you want them. Unlike `req_perform_stream()` it supports `req_retry()` (with @jcheng5, #519).

# httr2 1.0.3
Expand Down
2 changes: 1 addition & 1 deletion R/req-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ req_body_apply <- function(req) {
return(raw())
}
out <- readBin(con, "raw", nbytes)
if (length(out) <= nbytes) {
if (length(out) < nbytes) {
close(con)
done <<- TRUE
con <<- NULL
Expand Down
11 changes: 6 additions & 5 deletions tests/testthat/test-req-body.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
test_that("can send file", {
skip_on_os("windows") # fails due to line ending difference

path <- tempfile()
writeLines("this is a test", path)
path <- withr::local_tempfile()
# curl requests in 64kb chunks so this will hopefully illustrate
# any subtle problems
x <- strrep("x", 128 * 1024)
writeChar(x, path, nchar(x))

resp <- request_test("/post") %>%
req_body_file(path, type = "text/plain") %>%
req_perform()

json <- resp_body_json(resp)
expect_equal(json$headers$`Content-Type`, "text/plain")
expect_equal(json$data, "this is a test\n")
expect_equal(json$data, x)
})

test_that("can send file with redirect", {
Expand Down

0 comments on commit 5237e77

Please sign in to comment.