Fix URL runtime evaluation.

This commit is contained in:
jcamiel 2023-07-06 13:04:17 +02:00
parent 264c6f290b
commit 4f6a00170a
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
11 changed files with 45 additions and 11 deletions

View File

@ -0,0 +1,7 @@
error: Invalid URL
--> tests_failed/invalid_url_1.hurl:3:5
|
3 | GET {{host}}
| ^^^^^^^^ URL <localhost:8000> must start with http:// or https://
|

View File

@ -0,0 +1,2 @@
3

View File

@ -0,0 +1,5 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"></span><span class="comment"># We test that an URL should be valid (starts with http:// or https://)</span>
<span class="line"></span><span class="comment"># even if injected through variable.</span>
<span class="line"><span class="method">GET</span> <span class="url">{{host}}</span></span>
</span></span><span class="line"></span>
</code></pre>

View File

@ -0,0 +1,4 @@
# We test that an URL should be valid (starts with http:// or https://)
# even if injected through variable.
GET {{host}}

View File

@ -0,0 +1 @@
{"entries":[{"request":{"method":"GET","url":"{{host}}"}}]}

View File

@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl --variable host=localhost:8000 tests_failed/invalid_url_1.hurl

View File

@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl --variable host=localhost:8000 tests_failed/invalid_url_1.hurl

View File

@ -41,6 +41,7 @@ pub enum HttpError {
description: String, description: String,
}, },
InvalidUrl(String), InvalidUrl(String),
InvalidUrlPrefix(String),
} }
impl From<curl::Error> for HttpError { impl From<curl::Error> for HttpError {

View File

@ -68,17 +68,19 @@ impl Request {
Ok(url) => url, Ok(url) => url,
Err(_) => return Err(HttpError::InvalidUrl(self.url.clone())), Err(_) => return Err(HttpError::InvalidUrl(self.url.clone())),
}; };
let base_url = format!( let scheme = url.scheme();
"{}://{}{}", if scheme != "http" && scheme != "https" {
url.scheme(), return Err(HttpError::InvalidUrlPrefix(self.url.clone()));
url.host().unwrap(), }
if let Some(port) = url.port() { let host = match url.host() {
format!(":{port}") Some(host) => host,
} else { None => return Err(HttpError::InvalidUrl(self.url.clone())),
"".to_string() };
} let port = match url.port() {
); Some(port) => format!(":{port}"),
Ok(base_url) None => "".to_string(),
};
Ok(format!("{scheme}://{host}{port}"))
} }
} }

View File

@ -119,6 +119,7 @@ pub enum RunnerError {
value: String, value: String,
}, },
InvalidUrl(String), InvalidUrl(String),
InvalidUrlPrefix(String),
HttpConnection { HttpConnection {
url: String, url: String,

View File

@ -31,6 +31,7 @@ impl Error for runner::Error {
fn description(&self) -> String { fn description(&self) -> String {
match &self.inner { match &self.inner {
RunnerError::InvalidUrl(..) => "Invalid URL".to_string(), RunnerError::InvalidUrl(..) => "Invalid URL".to_string(),
RunnerError::InvalidUrlPrefix(..) => "Invalid URL".to_string(),
RunnerError::TemplateVariableNotDefined { .. } => "Undefined variable".to_string(), RunnerError::TemplateVariableNotDefined { .. } => "Undefined variable".to_string(),
RunnerError::VariableNotDefined { .. } => "Undefined variable".to_string(), RunnerError::VariableNotDefined { .. } => "Undefined variable".to_string(),
RunnerError::HttpConnection { .. } => "HTTP connection".to_string(), RunnerError::HttpConnection { .. } => "HTTP connection".to_string(),
@ -75,6 +76,9 @@ impl Error for runner::Error {
fn fixme(&self) -> String { fn fixme(&self) -> String {
match &self.inner { match &self.inner {
RunnerError::InvalidUrl(url) => format!("invalid URL <{url}>"), RunnerError::InvalidUrl(url) => format!("invalid URL <{url}>"),
RunnerError::InvalidUrlPrefix(url) => {
format!("URL <{url}> must start with http:// or https://")
}
RunnerError::TemplateVariableNotDefined { name } => { RunnerError::TemplateVariableNotDefined { name } => {
format!("you must set the variable {name}") format!("you must set the variable {name}")
} }
@ -203,6 +207,7 @@ impl From<HttpError> for RunnerError {
RunnerError::UnsupportedContentEncoding(description) RunnerError::UnsupportedContentEncoding(description)
} }
HttpError::InvalidUrl(url) => RunnerError::InvalidUrl(url), HttpError::InvalidUrl(url) => RunnerError::InvalidUrl(url),
HttpError::InvalidUrlPrefix(url) => RunnerError::InvalidUrlPrefix(url),
} }
} }
} }