3 use std
::cell
::{RefCell, Cell}
;
7 use std
::time
::Duration
;
10 ($e
:expr
) => (match $e
{
12 Err(e
) => panic
!("{} failed with {:?}", stringify
!($e
), e
),
16 use curl
::easy
::{Easy, List, WriteError, ReadError, Transfer}
;
22 let mut e
= Easy
::new();
23 t
!(e
.timeout(Duration
::new(20, 0)));
27 fn sink(data
: &[u8]) -> Result
<usize, WriteError
> {
33 let s
= Server
::new();
36 Host: 127.0.0.1:$PORT\r\n\
39 s
.send("HTTP/1.1 200 OK\r\n\r\n");
41 let mut handle
= handle();
42 t
!(handle
.url(&s
.url("/")));
48 let s
= Server
::new();
50 GET /foo HTTP/1.1\r\n\
51 Host: 127.0.0.1:$PORT\r\n\
54 s
.send("HTTP/1.1 200 OK\r\n\r\n");
56 let mut handle
= handle();
57 t
!(handle
.url(&s
.url("/foo")));
63 let s
= Server
::new();
66 Host: 127.0.0.1:$PORT\r\n\
69 s
.send("HTTP/1.1 200 OK\r\n\r\nhello!");
71 let mut all
= Vec
::<u8>::new();
73 let mut handle
= handle();
74 t
!(handle
.url(&s
.url("/")));
75 let mut handle
= handle
.transfer();
76 t
!(handle
.write_function(|data
| {
82 assert_eq
!(all
, b
"hello!");
87 let s
= Server
::new();
89 GET /foo HTTP/1.1\r\n\
90 Host: 127.0.0.1:$PORT\r\n\
93 s
.send("HTTP/1.1 200 OK\r\n\r\nHello!");
98 let mut handle
= handle();
99 t
!(handle
.url(&s
.url("/foo")));
100 t
!(handle
.progress(true));
101 t
!(handle
.write_function(sink
));
103 let mut handle
= handle
.transfer();
104 t
!(handle
.progress_function(|_
, a
, _
, _
| {
109 t
!(handle
.perform());
117 let s
= Server
::new();
120 Host: 127.0.0.1:$PORT\r\n\
130 let mut headers
= Vec
::new();
132 let mut handle
= handle();
133 t
!(handle
.url(&s
.url("/")));
135 let mut handle
= handle
.transfer();
136 t
!(handle
.header_function(|h
| {
137 headers
.push(str::from_utf8(h
).unwrap().to_string());
140 t
!(handle
.write_function(sink
));
141 t
!(handle
.perform());
143 assert_eq
!(headers
, vec
![
144 "HTTP/1.1 200 OK\r\n".to_string(),
145 "Foo: bar\r\n".to_string(),
146 "Bar: baz\r\n".to_string(),
153 let s
= Server
::new();
156 Host: 127.0.0.1:$PORT\r\n\
160 HTTP/1.1 401 Not so good\r\n\
163 let mut h
= handle();
164 t
!(h
.url(&s
.url("/")));
165 t
!(h
.fail_on_error(true));
166 assert
!(h
.perform().is_err());
168 let s
= Server
::new();
171 Host: 127.0.0.1:$PORT\r\n\
175 HTTP/1.1 401 Not so good\r\n\
178 let mut h
= handle();
179 t
!(h
.url(&s
.url("/")));
180 t
!(h
.fail_on_error(false));
186 let s
= Server
::new();
189 Host: localhost:$PORT\r\n\
196 let mut h
= handle();
197 t
!(h
.url("http://localhost/"));
198 t
!(h
.port(s
.addr().port()));
204 let s
= Server
::new();
206 GET http://example.com/ HTTP/1.1\r\n\
207 Host: example.com\r\n\
214 let mut h
= handle();
215 t
!(h
.url("http://example.com/"));
216 t
!(h
.proxy(&s
.url("/")));
222 let s
= Server
::new();
225 Host: 127.0.0.1:$PORT\r\n\
232 let mut h
= handle();
233 t
!(h
.url(&s
.url("/")));
234 t
!(h
.proxy(&s
.url("/")));
235 t
!(h
.noproxy("127.0.0.1"));
241 let mut h
= handle();
242 t
!(h
.tcp_nodelay(true));
243 // t!(h.tcp_keepalive(true));
244 // t!(h.tcp_keepidle(Duration::new(3, 0)));
245 // t!(h.tcp_keepintvl(Duration::new(3, 0)));
246 t
!(h
.buffer_size(10));
247 t
!(h
.dns_cache_timeout(Duration
::new(1, 0)));
252 let s
= Server
::new();
255 Authorization: Basic YmFyOg==\r\n\
256 Host: 127.0.0.1:$PORT\r\n\
263 let mut h
= handle();
264 t
!(h
.url(&s
.url("/")));
265 t
!(h
.username("foo"));
266 t
!(h
.username("bar"));
271 fn accept_encoding() {
272 let s
= Server
::new();
275 Host: 127.0.0.1:$PORT\r\n\
277 Accept-Encoding: gzip\r\n\
283 let mut h
= handle();
284 t
!(h
.url(&s
.url("/")));
285 t
!(h
.accept_encoding("gzip"));
290 fn follow_location() {
291 let s1
= Server
::new();
292 let s2
= Server
::new();
295 Host: 127.0.0.1:$PORT\r\n\
299 HTTP/1.1 301 Moved Permanently\r\n\
300 Location: http://{}/foo\r\n\
304 GET /foo HTTP/1.1\r\n\
305 Host: 127.0.0.1:$PORT\r\n\
312 let mut h
= handle();
313 t
!(h
.url(&s1
.url("/")));
314 t
!(h
.follow_location(true));
320 let s
= Server
::new();
323 Host: 127.0.0.1:$PORT\r\n\
325 Content-Length: 5\r\n\
332 let mut data
= "data\n".as_bytes();
333 let mut list
= List
::new();
334 t
!(list
.append("Expect:"));
335 let mut h
= handle();
336 t
!(h
.url(&s
.url("/")));
338 t
!(h
.in_filesize(5));
340 t
!(h
.http_headers(list
));
341 let mut h
= h
.transfer();
342 t
!(h
.read_function(|buf
| {
343 Ok(data
.read(buf
).unwrap())
350 let s
= Server
::new();
353 Host: 127.0.0.1:$PORT\r\n\
355 Content-Length: 5\r\n\
356 Content-Type: application/x-www-form-urlencoded\r\n\
363 let mut h
= handle();
364 t
!(h
.url(&s
.url("/")));
366 t
!(h
.post_fields_copy(b
"data\n"));
372 let s
= Server
::new();
375 Host: 127.0.0.1:$PORT\r\n\
377 Content-Length: 5\r\n\
378 Content-Type: application/x-www-form-urlencoded\r\n\
385 let mut h
= handle();
386 t
!(h
.url(&s
.url("/")));
388 t
!(h
.post_fields_copy(b
"data\n"));
389 t
!(h
.write_function(sink
));
395 let s
= Server
::new();
398 Host: 127.0.0.1:$PORT\r\n\
400 Content-Length: 5\r\n\
401 Content-Type: application/x-www-form-urlencoded\r\n\
408 let mut data
= "data\n".as_bytes();
409 let mut h
= handle();
410 t
!(h
.url(&s
.url("/")));
412 t
!(h
.post_field_size(5));
413 let mut h
= h
.transfer();
414 t
!(h
.read_function(|buf
| {
415 Ok(data
.read(buf
).unwrap())
422 let s
= Server
::new();
425 Host: 127.0.0.1:$PORT\r\n\
433 let mut h
= handle();
434 t
!(h
.url(&s
.url("/")));
435 t
!(h
.referer("foo"));
441 let s
= Server
::new();
445 Host: 127.0.0.1:$PORT\r\n\
452 let mut h
= handle();
453 t
!(h
.url(&s
.url("/")));
454 t
!(h
.useragent("foo"));
459 fn custom_headers() {
460 let s
= Server
::new();
463 Host: 127.0.0.1:$PORT\r\n\
470 let mut custom
= List
::new();
471 t
!(custom
.append("Foo: bar"));
472 t
!(custom
.append("Accept:"));
473 let mut h
= handle();
474 t
!(h
.url(&s
.url("/")));
475 t
!(h
.http_headers(custom
));
481 let s
= Server
::new();
484 Host: 127.0.0.1:$PORT\r\n\
492 let mut h
= handle();
493 t
!(h
.url(&s
.url("/")));
500 let mut h
= handle();
501 assert_eq
!(h
.url_encode(b
"foo"), "foo");
502 assert_eq
!(h
.url_encode(b
"foo bar"), "foo%20bar");
503 assert_eq
!(h
.url_encode(b
"foo bar\xff"), "foo%20bar%FF");
504 assert_eq
!(h
.url_encode(b
""), "");
505 assert_eq
!(h
.url_decode("foo"), b
"foo");
506 assert_eq
!(h
.url_decode("foo%20bar"), b
"foo bar");
507 assert_eq
!(h
.url_decode("foo%2"), b
"foo%2");
508 assert_eq
!(h
.url_decode("foo%xx"), b
"foo%xx");
509 assert_eq
!(h
.url_decode("foo%ff"), b
"foo\xff");
510 assert_eq
!(h
.url_decode(""), b
"");
515 let s
= Server
::new();
518 Host: 127.0.0.1:$PORT\r\n\
525 let mut h
= handle();
526 t
!(h
.url(&s
.url("/")));
527 t
!(h
.cookie_file("/dev/null"));
529 assert_eq
!(t
!(h
.response_code()), 200);
530 assert_eq
!(t
!(h
.redirect_count()), 0);
531 assert_eq
!(t
!(h
.redirect_url()), None
);
532 assert_eq
!(t
!(h
.content_type()), None
);
534 let addr
= format
!("http://{}/", s
.addr());
535 assert_eq
!(t
!(h
.effective_url()), Some(&addr
[..]));
538 // let cookies = t!(h.cookies()).iter()
539 // .map(|s| s.to_vec())
540 // .collect::<Vec<_>>();
541 // assert_eq!(cookies.len(), 1);
546 fn panic_in_callback() {
547 let s
= Server
::new();
550 Host: 127.0.0.1:$PORT\r\n\
557 let mut h
= handle();
558 t
!(h
.url(&s
.url("/")));
559 t
!(h
.header_function(|_
| panic
!()));
565 let s
= Server
::new();
568 Host: 127.0.0.1:$PORT\r\n\
570 Content-Length: 2\r\n\
576 let mut h
= handle();
577 t
!(h
.url(&s
.url("/")));
578 t
!(h
.read_function(|_
| Err(ReadError
::Abort
)));
580 t
!(h
.in_filesize(2));
581 let mut list
= List
::new();
582 t
!(list
.append("Expect:"));
583 t
!(h
.http_headers(list
));
584 let err
= h
.perform().unwrap_err();
585 assert
!(err
.is_aborted_by_callback());
589 fn pause_write_then_resume() {
590 let s
= Server
::new();
593 Host: 127.0.0.1:$PORT\r\n\
602 let mut h
= handle();
603 t
!(h
.url(&s
.url("/")));
604 t
!(h
.progress(true));
606 struct State
<'a
, 'b
> {
608 unpaused
: Cell
<bool
>,
609 transfer
: RefCell
<Transfer
<'a
, 'b
>>,
612 let h
= Rc
::new(State
{
613 paused
: Cell
::new(false),
614 unpaused
: Cell
::new(false),
615 transfer
: RefCell
::new(h
.transfer()),
619 t
!(h
.transfer
.borrow_mut().write_function(move |data
| {
620 if h2
.unpaused
.get() {
621 h2
.unpaused
.set(false);
625 Err(WriteError
::Pause
)
629 t
!(h
.transfer
.borrow_mut().progress_function(move |_
, _
, _
, _
| {
631 h2
.paused
.set(false);
632 h2
.unpaused
.set(true);
633 t
!(h2
.transfer
.borrow().unpause_write());
637 t
!(h
.transfer
.borrow().perform());
641 fn perform_in_perform_is_bad() {
642 let s
= Server
::new();
645 Host: 127.0.0.1:$PORT\r\n\
654 let mut h
= handle();
655 t
!(h
.url(&s
.url("/")));
656 t
!(h
.progress(true));
658 let h
= Rc
::new(RefCell
::new(h
.transfer()));
661 t
!(h
.borrow_mut().write_function(move |data
| {
662 assert
!(h2
.borrow().perform().is_err());
665 t
!(h
.borrow().perform());