1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
use std::time::Duration;
macro_rules! t {
($e:expr) => {
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
}
};
}
use curl::easy::{Easy, Form, List};
use crate::server::Server;
mod server;
fn handle() -> Easy {
let mut e = Easy::new();
t!(e.timeout(Duration::new(20, 0)));
let mut list = List::new();
t!(list.append("Expect:"));
t!(e.http_headers(list));
e
}
#[test]
fn custom() {
let s = Server::new();
s.receive(
"\
POST / HTTP/1.1\r\n\
Host: 127.0.0.1:$PORT\r\n\
Accept: */*\r\n\
Content-Length: 142\r\n\
Content-Type: multipart/form-data; boundary=--[..]\r\n\
\r\n\
--[..]\r\n\
Content-Disposition: form-data; name=\"foo\"\r\n\
\r\n\
1234\r\n\
--[..]\r\n",
);
s.send("HTTP/1.1 200 OK\r\n\r\n");
let mut handle = handle();
let mut form = Form::new();
t!(form.part("foo").contents(b"1234").add());
t!(handle.url(&s.url("/")));
t!(handle.httppost(form));
t!(handle.perform());
}
#[test]
fn buffer() {
let s = Server::new();
s.receive(
"\
POST / HTTP/1.1\r\n\
Host: 127.0.0.1:$PORT\r\n\
Accept: */*\r\n\
Content-Length: 181\r\n\
Content-Type: multipart/form-data; boundary=--[..]\r\n\
\r\n\
--[..]\r\n\
Content-Disposition: form-data; name=\"foo\"; filename=\"bar\"\r\n\
Content-Type: foo/bar\r\n\
\r\n\
1234\r\n\
--[..]\r\n",
);
s.send("HTTP/1.1 200 OK\r\n\r\n");
let mut handle = handle();
let mut form = Form::new();
t!(form
.part("foo")
.buffer("bar", b"1234".to_vec())
.content_type("foo/bar")
.add());
t!(handle.url(&s.url("/")));
t!(handle.httppost(form));
t!(handle.perform());
}
#[test]
fn file() {
let s = Server::new();
let formdata = include_str!("formdata");
s.receive(
format!(
"\
POST / HTTP/1.1\r\n\
Host: 127.0.0.1:$PORT\r\n\
Accept: */*\r\n\
Content-Length: {}\r\n\
Content-Type: multipart/form-data; boundary=--[..]\r\n\
\r\n\
--[..]\r\n\
Content-Disposition: form-data; name=\"foo\"; filename=\"formdata\"\r\n\
Content-Type: application/octet-stream\r\n\
\r\n\
{}\
\r\n\
--[..]\r\n",
199 + formdata.len(),
formdata
)
.as_str(),
);
s.send("HTTP/1.1 200 OK\r\n\r\n");
let mut handle = handle();
let mut form = Form::new();
t!(form.part("foo").file("tests/formdata").add());
t!(handle.url(&s.url("/")));
t!(handle.httppost(form));
t!(handle.perform());
}
|