Skip to content

Commit d006f00

Browse files
authored
Add upload_size (#443)
* Add integration tests for download_size * Add support for upload_size
1 parent 0985c30 commit d006f00

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

src/easy/handle.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,12 @@ impl Easy {
11641164
pub fn download_size(&mut self) -> Result<f64, Error> {
11651165
self.inner.download_size()
11661166
}
1167+
1168+
/// Same as [`Easy2::upload_size`](struct.Easy2.html#method.upload_size)
1169+
pub fn upload_size(&mut self) -> Result<f64, Error> {
1170+
self.inner.upload_size()
1171+
}
1172+
11671173
/// Same as [`Easy2::content_length_download`](struct.Easy2.html#method.content_length_download)
11681174
pub fn content_length_download(&mut self) -> Result<f64, Error> {
11691175
self.inner.content_length_download()

src/easy/handler.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,6 +2644,17 @@ impl<H> Easy2<H> {
26442644
.map(|r| r as f64)
26452645
}
26462646

2647+
/// Get the number of uploaded bytes
2648+
///
2649+
/// Returns the total amount of bytes that were uploaded.
2650+
///
2651+
/// This corresponds to `CURLINFO_SIZE_UPLOAD` and may return an error if the
2652+
/// option is not supported
2653+
pub fn upload_size(&mut self) -> Result<f64, Error> {
2654+
self.getopt_double(curl_sys::CURLINFO_SIZE_UPLOAD)
2655+
.map(|r| r as f64)
2656+
}
2657+
26472658
/// Get the content-length of the download
26482659
///
26492660
/// Returns the content-length of the download.

tests/easy.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,96 @@ fn get_smoke() {
4646
t!(handle.perform());
4747
}
4848

49+
#[test]
50+
fn download_zero_size() {
51+
let s = Server::new();
52+
s.receive(
53+
"\
54+
GET / HTTP/1.1\r\n\
55+
Host: 127.0.0.1:$PORT\r\n\
56+
Accept: */*\r\n\
57+
\r\n",
58+
);
59+
s.send("HTTP/1.1 200 OK\r\n\r\n");
60+
61+
let mut handle = handle();
62+
t!(handle.url(&s.url("/")));
63+
t!(handle.perform());
64+
assert_eq!(handle.download_size().unwrap(), 0_f64);
65+
}
66+
67+
#[test]
68+
fn download_nonzero_size() {
69+
let s = Server::new();
70+
s.receive(
71+
"\
72+
GET / HTTP/1.1\r\n\
73+
Host: 127.0.0.1:$PORT\r\n\
74+
Accept: */*\r\n\
75+
\r\n",
76+
);
77+
s.send("HTTP/1.1 200 OK\r\n\r\nHello!");
78+
79+
let mut handle = handle();
80+
t!(handle.url(&s.url("/")));
81+
t!(handle.perform());
82+
assert_eq!(handle.download_size().unwrap(), 6_f64);
83+
}
84+
85+
#[test]
86+
fn upload_zero_size() {
87+
let s = Server::new();
88+
s.receive(
89+
"\
90+
GET / HTTP/1.1\r\n\
91+
Host: 127.0.0.1:$PORT\r\n\
92+
Accept: */*\r\n\
93+
\r\n",
94+
);
95+
s.send("HTTP/1.1 200 OK\r\n\r\n");
96+
97+
let mut handle = handle();
98+
t!(handle.url(&s.url("/")));
99+
t!(handle.perform());
100+
assert_eq!(handle.upload_size().unwrap(), 0_f64);
101+
}
102+
103+
#[test]
104+
fn upload_nonzero_size() {
105+
let s = Server::new();
106+
s.receive(
107+
"\
108+
PUT / HTTP/1.1\r\n\
109+
Host: 127.0.0.1:$PORT\r\n\
110+
Accept: */*\r\n\
111+
Content-Length: 5\r\n\
112+
\r\n\
113+
data\n",
114+
);
115+
s.send(
116+
"\
117+
HTTP/1.1 200 OK\r\n\
118+
\r\n",
119+
);
120+
121+
let mut data = "data\n".as_bytes();
122+
let mut list = List::new();
123+
t!(list.append("Expect:"));
124+
let mut h = handle();
125+
t!(h.url(&s.url("/")));
126+
t!(h.put(true));
127+
t!(h.in_filesize(5));
128+
t!(h.upload(true));
129+
t!(h.http_headers(list));
130+
{
131+
let mut h = h.transfer();
132+
t!(h.read_function(|buf| Ok(data.read(buf).unwrap())));
133+
t!(h.perform());
134+
}
135+
136+
assert_eq!(h.upload_size().unwrap(), 5_f64);
137+
}
138+
49139
#[test]
50140
fn get_path() {
51141
let s = Server::new();

0 commit comments

Comments
 (0)