Fix HTTP HEAD

This commit is contained in:
Fabrice Reix 2022-10-17 14:23:59 +02:00
parent e85354f0eb
commit 6472866c22
No known key found for this signature in database
GPG Key ID: BF5213154B2E7155
8 changed files with 64 additions and 1 deletions

View File

@ -0,0 +1 @@
curl 'http://localhost:8000/head' --head

View File

@ -0,0 +1,9 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"><span class="method">HEAD</span> <span class="url">http://localhost:8000/head</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="line"><span class="string">Content-Type</span><span>:</span> <span class="string">text/html; charset=utf-8</span></span>
<span class="line"><span class="string">Content-Length</span><span>:</span> <span class="string">10</span></span>
<span class="line"><span class="string">Server</span><span>:</span> <span class="string">Flask Server</span></span>
<span class="line section-header">[Asserts]</span>
<span class="line"><span class="query-type">bytes</span> <span class="subquery-type">count</span> <span class="predicate-type">==</span> <span class="number">0</span></span>
</span></span><span class="line"></span>
</code></pre>

View File

@ -0,0 +1,8 @@
HEAD http://localhost:8000/head
HTTP/1.0 200
Content-Type: text/html; charset=utf-8
Content-Length: 10
Server: Flask Server
[Asserts]
bytes count == 0

View File

@ -0,0 +1 @@
{"entries":[{"request":{"method":"HEAD","url":"http://localhost:8000/head"},"response":{"version":"HTTP/1.0","status":200,"headers":[{"name":"Content-Type","value":"text/html; charset=utf-8"},{"name":"Content-Length","value":"10"},{"name":"Server","value":"Flask Server"}],"asserts":[{"query":{"type":"bytes","subquery":{"type":"count"}},"predicate":{"type":"equal","value":0}}]}}]}

View File

@ -0,0 +1,6 @@
from app import app
@app.route("/head")
def head():
return "Hello Head"

View File

@ -182,6 +182,10 @@ impl Client {
// of key-value.
let mut request_body = Vec::<u8>::new();
let mut response_body = Vec::<u8>::new();
if *method == Method::Head {
self.handle.nobody(true).unwrap();
}
{
let mut transfer = self.handle.transfer();
if !request_spec_body.is_empty() {
@ -189,6 +193,7 @@ impl Client {
.read_function(|buf| Ok(request_spec_body.read(buf).unwrap_or(0)))
.unwrap();
}
transfer
.debug_function(|info_type, data| match info_type {
// Return all request headers (not one by one)

View File

@ -118,7 +118,7 @@ impl Method {
vec![]
}
}
Method::Head => vec!["-X".to_string(), "HEAD".to_string()],
Method::Head => vec!["--head".to_string()],
Method::Post => {
if data {
vec![]

View File

@ -1132,6 +1132,39 @@ fn test_insecure() {
assert_eq!(response.status, 200);
}
#[test]
fn test_head() {
let options = ClientOptions {
..Default::default()
};
let context_dir = ContextDir::default();
let mut client = Client::new(None);
let logger = Logger::new(false, false, "", "");
let request_spec = RequestSpec {
method: Method::Head,
url: "http://localhost:8000/head".to_string(),
headers: vec![],
querystring: vec![],
form: vec![],
multipart: vec![],
cookies: vec![],
body: Body::Binary(vec![]),
content_type: None,
};
assert_eq!(
client.curl_command_line(&request_spec, &context_dir, &options),
"curl 'http://localhost:8000/head' --head".to_string()
);
let (request, response) = client.execute(&request_spec, &options, &logger).unwrap();
assert_eq!(request.url, "http://localhost:8000/head");
assert_eq!(response.status, 200);
assert!(response.headers.contains(&Header {
name: "Content-Length".to_string(),
value: "10".to_string(),
}));
}
#[test]
fn test_version() {
// This test if only informative for the time-being