Skip to content

Commit

Permalink
Check protocol at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix authored and hurl-bot committed Oct 21, 2024
1 parent 2491637 commit c62fb43
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
7 changes: 7 additions & 0 deletions integration/hurl/tests_failed/invalid_protocol.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Invalid URL
--> tests_failed/invalid_protocol.hurl:1:5
|
1 | GET {{url}}
| ^^^^^^^ invalid URL <file:///tmp/foo.txt> (Only http and https protocols are supported)
|

1 change: 1 addition & 0 deletions integration/hurl/tests_failed/invalid_protocol.exit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
4 changes: 4 additions & 0 deletions integration/hurl/tests_failed/invalid_protocol.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GET {{url}}
[Options]
variable: url=file:///tmp/foo.txt
HTTP 200
3 changes: 3 additions & 0 deletions integration/hurl/tests_failed/invalid_protocol.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_failed/invalid_protocol.hurl
3 changes: 3 additions & 0 deletions integration/hurl/tests_failed/invalid_protocol.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_failed/invalid_protocol.hurl
10 changes: 10 additions & 0 deletions packages/hurl/src/runner/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ pub enum RunnerErrorKind {
value: String,
},
InvalidRegex,
InvalidUrl {
url: String,
message: String,
},
NoQueryResult,
QueryHeaderNotFound,
QueryInvalidJsonpathExpression {
Expand Down Expand Up @@ -138,6 +142,7 @@ impl DisplaySourceError for RunnerError {
RunnerErrorKind::FilterMissingInput => "Filter error".to_string(),
RunnerErrorKind::Http(http_error) => http_error.description(),
RunnerErrorKind::InvalidJson { .. } => "Invalid JSON".to_string(),
RunnerErrorKind::InvalidUrl { .. } => "Invalid URL".to_string(),
RunnerErrorKind::InvalidRegex => "Invalid regex".to_string(),
RunnerErrorKind::NoQueryResult => "No query result".to_string(),
RunnerErrorKind::QueryHeaderNotFound => "Header not found".to_string(),
Expand Down Expand Up @@ -244,6 +249,11 @@ impl DisplaySourceError for RunnerError {
let message = error::add_carets(message, self.source_info, content);
color_red_multiline_string(&message)
}
RunnerErrorKind::InvalidUrl { url, message } => {
let message = &format!("invalid URL <{url}> ({message})");
let message = error::add_carets(message, self.source_info, content);
color_red_multiline_string(&message)
}
RunnerErrorKind::InvalidRegex => {
let message = "regex expression is not valid";
let message = error::add_carets(message, self.source_info, content);
Expand Down
31 changes: 29 additions & 2 deletions packages/hurl/src/runner/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::http;
use crate::http::{HeaderVec, AUTHORIZATION};
use crate::runner::error::RunnerError;
use crate::runner::value::Value;
use crate::runner::{body, multipart, template};
use crate::runner::{body, multipart, template, RunnerErrorKind};
use crate::util::path::ContextDir;

/// Transforms an AST `request` to a spec request given a set of `variables`.
Expand All @@ -35,7 +35,7 @@ pub fn eval_request(
context_dir: &ContextDir,
) -> Result<http::RequestSpec, RunnerError> {
let method = eval_method(&request.method);
let url = template::eval_template(&request.url, variables)?;
let url = eval_url(&request.url, variables)?;

// Headers
let mut headers = HeaderVec::new();
Expand Down Expand Up @@ -143,6 +143,33 @@ pub fn eval_request(
})
}

fn eval_url(
url_template: &Template,
variables: &HashMap<String, Value>,
) -> Result<String, RunnerError> {
let url = template::eval_template(url_template, variables)?;

// Check protocol
let tokens = url.split("://").collect::<Vec<&str>>();
let protocol = if tokens.len() > 1 {
tokens.first().unwrap()
} else {
""
};
if protocol.is_empty() {
let source_info = url_template.source_info;
let message = "Missing protocol http or https".to_string();
let runner_error_kind = RunnerErrorKind::InvalidUrl { url, message };
return Err(RunnerError::new(source_info, runner_error_kind, false));
} else if protocol != "http" && protocol != "https" {
let source_info = url_template.source_info;
let message = "Only http and https protocols are supported".to_string();
let runner_error_kind = RunnerErrorKind::InvalidUrl { url, message };
return Err(RunnerError::new(source_info, runner_error_kind, false));
}
Ok(url)
}

/// Experimental feature
/// @cookie_storage_add
pub fn cookie_storage_set(request: &Request) -> Option<String> {
Expand Down

0 comments on commit c62fb43

Please sign in to comment.