Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/easy/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,12 @@ impl Easy {
pub fn download_size(&mut self) -> Result<f64, Error> {
self.inner.download_size()
}

/// Same as [`Easy2::upload_size`](struct.Easy2.html#method.upload_size)
pub fn upload_size(&mut self) -> Result<f64, Error> {
self.inner.upload_size()
}

/// Same as [`Easy2::content_length_download`](struct.Easy2.html#method.content_length_download)
pub fn content_length_download(&mut self) -> Result<f64, Error> {
self.inner.content_length_download()
Expand Down
11 changes: 11 additions & 0 deletions src/easy/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,17 @@ impl<H> Easy2<H> {
.map(|r| r as f64)
}

/// Get the number of uploaded bytes
///
/// Returns the total amount of bytes that were uploaded.
///
/// This corresponds to `CURLINFO_SIZE_UPLOAD` and may return an error if the
/// option is not supported
pub fn upload_size(&mut self) -> Result<f64, Error> {
self.getopt_double(curl_sys::CURLINFO_SIZE_UPLOAD)
.map(|r| r as f64)
}

/// Get the content-length of the download
///
/// Returns the content-length of the download.
Expand Down
90 changes: 90 additions & 0 deletions tests/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,96 @@ fn get_smoke() {
t!(handle.perform());
}

#[test]
fn download_zero_size() {
let s = Server::new();
s.receive(
"\
GET / HTTP/1.1\r\n\
Host: 127.0.0.1:$PORT\r\n\
Accept: */*\r\n\
\r\n",
);
s.send("HTTP/1.1 200 OK\r\n\r\n");

let mut handle = handle();
t!(handle.url(&s.url("/")));
t!(handle.perform());
assert_eq!(handle.download_size().unwrap(), 0_f64);
}

#[test]
fn download_nonzero_size() {
let s = Server::new();
s.receive(
"\
GET / HTTP/1.1\r\n\
Host: 127.0.0.1:$PORT\r\n\
Accept: */*\r\n\
\r\n",
);
s.send("HTTP/1.1 200 OK\r\n\r\nHello!");

let mut handle = handle();
t!(handle.url(&s.url("/")));
t!(handle.perform());
assert_eq!(handle.download_size().unwrap(), 6_f64);
}

#[test]
fn upload_zero_size() {
let s = Server::new();
s.receive(
"\
GET / HTTP/1.1\r\n\
Host: 127.0.0.1:$PORT\r\n\
Accept: */*\r\n\
\r\n",
);
s.send("HTTP/1.1 200 OK\r\n\r\n");

let mut handle = handle();
t!(handle.url(&s.url("/")));
t!(handle.perform());
assert_eq!(handle.upload_size().unwrap(), 0_f64);
}

#[test]
fn upload_nonzero_size() {
let s = Server::new();
s.receive(
"\
PUT / HTTP/1.1\r\n\
Host: 127.0.0.1:$PORT\r\n\
Accept: */*\r\n\
Content-Length: 5\r\n\
\r\n\
data\n",
);
s.send(
"\
HTTP/1.1 200 OK\r\n\
\r\n",
);

let mut data = "data\n".as_bytes();
let mut list = List::new();
t!(list.append("Expect:"));
let mut h = handle();
t!(h.url(&s.url("/")));
t!(h.put(true));
t!(h.in_filesize(5));
t!(h.upload(true));
t!(h.http_headers(list));
{
let mut h = h.transfer();
t!(h.read_function(|buf| Ok(data.read(buf).unwrap())));
t!(h.perform());
}

assert_eq!(h.upload_size().unwrap(), 5_f64);
}

#[test]
fn get_path() {
let s = Server::new();
Expand Down