]> git.proxmox.com Git - rustc.git/blob - src/vendor/curl/tests/easy.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / vendor / curl / tests / easy.rs
1 extern crate curl;
2
3 use std::cell::{RefCell, Cell};
4 use std::io::Read;
5 use std::rc::Rc;
6 use std::str;
7 use std::time::Duration;
8
9 macro_rules! t {
10 ($e:expr) => (match $e {
11 Ok(e) => e,
12 Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
13 })
14 }
15
16 use curl::easy::{Easy, List, WriteError, ReadError, Transfer};
17
18 use server::Server;
19 mod server;
20
21 fn handle() -> Easy {
22 let mut e = Easy::new();
23 t!(e.timeout(Duration::new(20, 0)));
24 return e
25 }
26
27 fn sink(data: &[u8]) -> Result<usize, WriteError> {
28 Ok(data.len())
29 }
30
31 #[test]
32 fn get_smoke() {
33 let s = Server::new();
34 s.receive("\
35 GET / HTTP/1.1\r\n\
36 Host: 127.0.0.1:$PORT\r\n\
37 Accept: */*\r\n\
38 \r\n");
39 s.send("HTTP/1.1 200 OK\r\n\r\n");
40
41 let mut handle = handle();
42 t!(handle.url(&s.url("/")));
43 t!(handle.perform());
44 }
45
46 #[test]
47 fn get_path() {
48 let s = Server::new();
49 s.receive("\
50 GET /foo HTTP/1.1\r\n\
51 Host: 127.0.0.1:$PORT\r\n\
52 Accept: */*\r\n\
53 \r\n");
54 s.send("HTTP/1.1 200 OK\r\n\r\n");
55
56 let mut handle = handle();
57 t!(handle.url(&s.url("/foo")));
58 t!(handle.perform());
59 }
60
61 #[test]
62 fn write_callback() {
63 let s = Server::new();
64 s.receive("\
65 GET / HTTP/1.1\r\n\
66 Host: 127.0.0.1:$PORT\r\n\
67 Accept: */*\r\n\
68 \r\n");
69 s.send("HTTP/1.1 200 OK\r\n\r\nhello!");
70
71 let mut all = Vec::<u8>::new();
72 {
73 let mut handle = handle();
74 t!(handle.url(&s.url("/")));
75 let mut handle = handle.transfer();
76 t!(handle.write_function(|data| {
77 all.extend(data);
78 Ok(data.len())
79 }));
80 t!(handle.perform());
81 }
82 assert_eq!(all, b"hello!");
83 }
84
85 #[test]
86 fn progress() {
87 let s = Server::new();
88 s.receive("\
89 GET /foo HTTP/1.1\r\n\
90 Host: 127.0.0.1:$PORT\r\n\
91 Accept: */*\r\n\
92 \r\n");
93 s.send("HTTP/1.1 200 OK\r\n\r\nHello!");
94
95 let mut hits = 0;
96 let mut dl = 0.0;
97 {
98 let mut handle = handle();
99 t!(handle.url(&s.url("/foo")));
100 t!(handle.progress(true));
101 t!(handle.write_function(sink));
102
103 let mut handle = handle.transfer();
104 t!(handle.progress_function(|_, a, _, _| {
105 hits += 1;
106 dl = a;
107 true
108 }));
109 t!(handle.perform());
110 }
111 assert!(hits > 0);
112 assert_eq!(dl, 6.0);
113 }
114
115 #[test]
116 fn headers() {
117 let s = Server::new();
118 s.receive("\
119 GET / HTTP/1.1\r\n\
120 Host: 127.0.0.1:$PORT\r\n\
121 Accept: */*\r\n\
122 \r\n");
123 s.send("\
124 HTTP/1.1 200 OK\r\n\
125 Foo: bar\r\n\
126 Bar: baz\r\n\
127 \r\n
128 Hello!");
129
130 let mut headers = Vec::new();
131 {
132 let mut handle = handle();
133 t!(handle.url(&s.url("/")));
134
135 let mut handle = handle.transfer();
136 t!(handle.header_function(|h| {
137 headers.push(str::from_utf8(h).unwrap().to_string());
138 true
139 }));
140 t!(handle.write_function(sink));
141 t!(handle.perform());
142 }
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(),
147 "\r\n".to_string(),
148 ]);
149 }
150
151 #[test]
152 fn fail_on_error() {
153 let s = Server::new();
154 s.receive("\
155 GET / HTTP/1.1\r\n\
156 Host: 127.0.0.1:$PORT\r\n\
157 Accept: */*\r\n\
158 \r\n");
159 s.send("\
160 HTTP/1.1 401 Not so good\r\n\
161 \r\n");
162
163 let mut h = handle();
164 t!(h.url(&s.url("/")));
165 t!(h.fail_on_error(true));
166 assert!(h.perform().is_err());
167
168 let s = Server::new();
169 s.receive("\
170 GET / HTTP/1.1\r\n\
171 Host: 127.0.0.1:$PORT\r\n\
172 Accept: */*\r\n\
173 \r\n");
174 s.send("\
175 HTTP/1.1 401 Not so good\r\n\
176 \r\n");
177
178 let mut h = handle();
179 t!(h.url(&s.url("/")));
180 t!(h.fail_on_error(false));
181 t!(h.perform());
182 }
183
184 #[test]
185 fn port() {
186 let s = Server::new();
187 s.receive("\
188 GET / HTTP/1.1\r\n\
189 Host: localhost:$PORT\r\n\
190 Accept: */*\r\n\
191 \r\n");
192 s.send("\
193 HTTP/1.1 200 OK\r\n\
194 \r\n");
195
196 let mut h = handle();
197 t!(h.url("http://localhost/"));
198 t!(h.port(s.addr().port()));
199 t!(h.perform());
200 }
201
202 #[test]
203 fn proxy() {
204 let s = Server::new();
205 s.receive("\
206 GET http://example.com/ HTTP/1.1\r\n\
207 Host: example.com\r\n\
208 Accept: */*\r\n\
209 \r\n");
210 s.send("\
211 HTTP/1.1 200 OK\r\n\
212 \r\n");
213
214 let mut h = handle();
215 t!(h.url("http://example.com/"));
216 t!(h.proxy(&s.url("/")));
217 t!(h.perform());
218 }
219
220 #[test]
221 fn noproxy() {
222 let s = Server::new();
223 s.receive("\
224 GET / HTTP/1.1\r\n\
225 Host: 127.0.0.1:$PORT\r\n\
226 Accept: */*\r\n\
227 \r\n");
228 s.send("\
229 HTTP/1.1 200 OK\r\n\
230 \r\n");
231
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"));
236 t!(h.perform());
237 }
238
239 #[test]
240 fn misc() {
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)));
248 }
249
250 #[test]
251 fn userpass() {
252 let s = Server::new();
253 s.receive("\
254 GET / HTTP/1.1\r\n\
255 Authorization: Basic YmFyOg==\r\n\
256 Host: 127.0.0.1:$PORT\r\n\
257 Accept: */*\r\n\
258 \r\n");
259 s.send("\
260 HTTP/1.1 200 OK\r\n\
261 \r\n");
262
263 let mut h = handle();
264 t!(h.url(&s.url("/")));
265 t!(h.username("foo"));
266 t!(h.username("bar"));
267 t!(h.perform());
268 }
269
270 #[test]
271 fn accept_encoding() {
272 let s = Server::new();
273 s.receive("\
274 GET / HTTP/1.1\r\n\
275 Host: 127.0.0.1:$PORT\r\n\
276 Accept: */*\r\n\
277 Accept-Encoding: gzip\r\n\
278 \r\n");
279 s.send("\
280 HTTP/1.1 200 OK\r\n\
281 \r\n");
282
283 let mut h = handle();
284 t!(h.url(&s.url("/")));
285 t!(h.accept_encoding("gzip"));
286 t!(h.perform());
287 }
288
289 #[test]
290 fn follow_location() {
291 let s1 = Server::new();
292 let s2 = Server::new();
293 s1.receive("\
294 GET / HTTP/1.1\r\n\
295 Host: 127.0.0.1:$PORT\r\n\
296 Accept: */*\r\n\
297 \r\n");
298 s1.send(&format!("\
299 HTTP/1.1 301 Moved Permanently\r\n\
300 Location: http://{}/foo\r\n\
301 \r\n", s2.addr()));
302
303 s2.receive("\
304 GET /foo HTTP/1.1\r\n\
305 Host: 127.0.0.1:$PORT\r\n\
306 Accept: */*\r\n\
307 \r\n");
308 s2.send("\
309 HTTP/1.1 200 OK\r\n\
310 \r\n");
311
312 let mut h = handle();
313 t!(h.url(&s1.url("/")));
314 t!(h.follow_location(true));
315 t!(h.perform());
316 }
317
318 #[test]
319 fn put() {
320 let s = Server::new();
321 s.receive("\
322 PUT / HTTP/1.1\r\n\
323 Host: 127.0.0.1:$PORT\r\n\
324 Accept: */*\r\n\
325 Content-Length: 5\r\n\
326 \r\n\
327 data\n");
328 s.send("\
329 HTTP/1.1 200 OK\r\n\
330 \r\n");
331
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("/")));
337 t!(h.put(true));
338 t!(h.in_filesize(5));
339 t!(h.upload(true));
340 t!(h.http_headers(list));
341 let mut h = h.transfer();
342 t!(h.read_function(|buf| {
343 Ok(data.read(buf).unwrap())
344 }));
345 t!(h.perform());
346 }
347
348 #[test]
349 fn post1() {
350 let s = Server::new();
351 s.receive("\
352 POST / HTTP/1.1\r\n\
353 Host: 127.0.0.1:$PORT\r\n\
354 Accept: */*\r\n\
355 Content-Length: 5\r\n\
356 Content-Type: application/x-www-form-urlencoded\r\n\
357 \r\n\
358 data\n");
359 s.send("\
360 HTTP/1.1 200 OK\r\n\
361 \r\n");
362
363 let mut h = handle();
364 t!(h.url(&s.url("/")));
365 t!(h.post(true));
366 t!(h.post_fields_copy(b"data\n"));
367 t!(h.perform());
368 }
369
370 #[test]
371 fn post2() {
372 let s = Server::new();
373 s.receive("\
374 POST / HTTP/1.1\r\n\
375 Host: 127.0.0.1:$PORT\r\n\
376 Accept: */*\r\n\
377 Content-Length: 5\r\n\
378 Content-Type: application/x-www-form-urlencoded\r\n\
379 \r\n\
380 data\n");
381 s.send("\
382 HTTP/1.1 200 OK\r\n\
383 \r\n");
384
385 let mut h = handle();
386 t!(h.url(&s.url("/")));
387 t!(h.post(true));
388 t!(h.post_fields_copy(b"data\n"));
389 t!(h.write_function(sink));
390 t!(h.perform());
391 }
392
393 #[test]
394 fn post3() {
395 let s = Server::new();
396 s.receive("\
397 POST / HTTP/1.1\r\n\
398 Host: 127.0.0.1:$PORT\r\n\
399 Accept: */*\r\n\
400 Content-Length: 5\r\n\
401 Content-Type: application/x-www-form-urlencoded\r\n\
402 \r\n\
403 data\n");
404 s.send("\
405 HTTP/1.1 200 OK\r\n\
406 \r\n");
407
408 let mut data = "data\n".as_bytes();
409 let mut h = handle();
410 t!(h.url(&s.url("/")));
411 t!(h.post(true));
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())
416 }));
417 t!(h.perform());
418 }
419
420 #[test]
421 fn referer() {
422 let s = Server::new();
423 s.receive("\
424 GET / HTTP/1.1\r\n\
425 Host: 127.0.0.1:$PORT\r\n\
426 Accept: */*\r\n\
427 Referer: foo\r\n\
428 \r\n");
429 s.send("\
430 HTTP/1.1 200 OK\r\n\
431 \r\n");
432
433 let mut h = handle();
434 t!(h.url(&s.url("/")));
435 t!(h.referer("foo"));
436 t!(h.perform());
437 }
438
439 #[test]
440 fn useragent() {
441 let s = Server::new();
442 s.receive("\
443 GET / HTTP/1.1\r\n\
444 User-Agent: foo\r\n\
445 Host: 127.0.0.1:$PORT\r\n\
446 Accept: */*\r\n\
447 \r\n");
448 s.send("\
449 HTTP/1.1 200 OK\r\n\
450 \r\n");
451
452 let mut h = handle();
453 t!(h.url(&s.url("/")));
454 t!(h.useragent("foo"));
455 t!(h.perform());
456 }
457
458 #[test]
459 fn custom_headers() {
460 let s = Server::new();
461 s.receive("\
462 GET / HTTP/1.1\r\n\
463 Host: 127.0.0.1:$PORT\r\n\
464 Foo: bar\r\n\
465 \r\n");
466 s.send("\
467 HTTP/1.1 200 OK\r\n\
468 \r\n");
469
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));
476 t!(h.perform());
477 }
478
479 #[test]
480 fn cookie() {
481 let s = Server::new();
482 s.receive("\
483 GET / HTTP/1.1\r\n\
484 Host: 127.0.0.1:$PORT\r\n\
485 Accept: */*\r\n\
486 Cookie: foo\r\n\
487 \r\n");
488 s.send("\
489 HTTP/1.1 200 OK\r\n\
490 \r\n");
491
492 let mut h = handle();
493 t!(h.url(&s.url("/")));
494 t!(h.cookie("foo"));
495 t!(h.perform());
496 }
497
498 #[test]
499 fn url_encoding() {
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"");
511 }
512
513 #[test]
514 fn getters() {
515 let s = Server::new();
516 s.receive("\
517 GET / HTTP/1.1\r\n\
518 Host: 127.0.0.1:$PORT\r\n\
519 Accept: */*\r\n\
520 \r\n");
521 s.send("\
522 HTTP/1.1 200 OK\r\n\
523 \r\n");
524
525 let mut h = handle();
526 t!(h.url(&s.url("/")));
527 t!(h.cookie_file("/dev/null"));
528 t!(h.perform());
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);
533
534 let addr = format!("http://{}/", s.addr());
535 assert_eq!(t!(h.effective_url()), Some(&addr[..]));
536
537 // TODO: test this
538 // let cookies = t!(h.cookies()).iter()
539 // .map(|s| s.to_vec())
540 // .collect::<Vec<_>>();
541 // assert_eq!(cookies.len(), 1);
542 }
543
544 #[test]
545 #[should_panic]
546 fn panic_in_callback() {
547 let s = Server::new();
548 s.receive("\
549 GET / HTTP/1.1\r\n\
550 Host: 127.0.0.1:$PORT\r\n\
551 Accept: */*\r\n\
552 \r\n");
553 s.send("\
554 HTTP/1.1 200 OK\r\n\
555 \r\n");
556
557 let mut h = handle();
558 t!(h.url(&s.url("/")));
559 t!(h.header_function(|_| panic!()));
560 t!(h.perform());
561 }
562
563 #[test]
564 fn abort_read() {
565 let s = Server::new();
566 s.receive("\
567 PUT / HTTP/1.1\r\n\
568 Host: 127.0.0.1:$PORT\r\n\
569 Accept: */*\r\n\
570 Content-Length: 2\r\n\
571 \r\n");
572 s.send("\
573 HTTP/1.1 200 OK\r\n\
574 \r\n");
575
576 let mut h = handle();
577 t!(h.url(&s.url("/")));
578 t!(h.read_function(|_| Err(ReadError::Abort)));
579 t!(h.put(true));
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());
586 }
587
588 #[test]
589 fn pause_write_then_resume() {
590 let s = Server::new();
591 s.receive("\
592 GET / HTTP/1.1\r\n\
593 Host: 127.0.0.1:$PORT\r\n\
594 Accept: */*\r\n\
595 \r\n");
596 s.send("\
597 HTTP/1.1 200 OK\r\n\
598 \r\n
599 a\n
600 b");
601
602 let mut h = handle();
603 t!(h.url(&s.url("/")));
604 t!(h.progress(true));
605
606 struct State<'a, 'b> {
607 paused: Cell<bool>,
608 unpaused: Cell<bool>,
609 transfer: RefCell<Transfer<'a, 'b>>,
610 }
611
612 let h = Rc::new(State {
613 paused: Cell::new(false),
614 unpaused: Cell::new(false),
615 transfer: RefCell::new(h.transfer()),
616 });
617
618 let h2 = h.clone();
619 t!(h.transfer.borrow_mut().write_function(move |data| {
620 if h2.unpaused.get() {
621 h2.unpaused.set(false);
622 Ok(data.len())
623 } else {
624 h2.paused.set(true);
625 Err(WriteError::Pause)
626 }
627 }));
628 let h2 = h.clone();
629 t!(h.transfer.borrow_mut().progress_function(move |_, _, _, _| {
630 if h2.paused.get() {
631 h2.paused.set(false);
632 h2.unpaused.set(true);
633 t!(h2.transfer.borrow().unpause_write());
634 }
635 true
636 }));
637 t!(h.transfer.borrow().perform());
638 }
639
640 #[test]
641 fn perform_in_perform_is_bad() {
642 let s = Server::new();
643 s.receive("\
644 GET / HTTP/1.1\r\n\
645 Host: 127.0.0.1:$PORT\r\n\
646 Accept: */*\r\n\
647 \r\n");
648 s.send("\
649 HTTP/1.1 200 OK\r\n\
650 \r\n
651 a\n
652 b");
653
654 let mut h = handle();
655 t!(h.url(&s.url("/")));
656 t!(h.progress(true));
657
658 let h = Rc::new(RefCell::new(h.transfer()));
659
660 let h2 = h.clone();
661 t!(h.borrow_mut().write_function(move |data| {
662 assert!(h2.borrow().perform().is_err());
663 Ok(data.len())
664 }));
665 t!(h.borrow().perform());
666 }