Skip to content

Commit

Permalink
Support template in option values
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed Oct 18, 2023
1 parent 78ac032 commit 5be7eb9
Show file tree
Hide file tree
Showing 21 changed files with 567 additions and 188 deletions.
7 changes: 7 additions & 0 deletions integration/tests_failed/options_template.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Invalid variable type
--> tests_failed/options_template.hurl:6:13
|
6 | location: {{redirect}}
| ^^^^^^^^ expecting boolean, actual value is <10>
|

1 change: 1 addition & 0 deletions integration/tests_failed/options_template.exit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
6 changes: 6 additions & 0 deletions integration/tests_failed/options_template.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Invalid type at runtime

GET http://localhost:8000/unused
[Options]
variable: redirect=10
location: {{redirect}}
3 changes: 3 additions & 0 deletions integration/tests_failed/options_template.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_failed/options_template.hurl
3 changes: 3 additions & 0 deletions integration/tests_failed/options_template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_failed/options_template.hurl
23 changes: 23 additions & 0 deletions integration/tests_ok/options_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"></span><span class="comment"># Reuse existing tests with options</span>
<span class="line"></span><span class="comment"># using template</span>
<span class="line"></span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/redirect-absolute</span></span>
<span class="line"><span class="section-header">[Options]</span></span>
<span class="line"><span class="string">variable</span>: redirect=<span class="boolean">true</span></span>
<span class="line"><span class="string">location</span>: <span class="expr">{{redirect}}</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/delay-init</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP</span> <span class="number">200</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/delay</span></span> <span class="comment"># This request must reach the server at least 1000ms after the previous request</span>
<span class="line"><span class="section-header">[Options]</span></span>
<span class="line"><span class="string">variable</span>: duration=<span class="number">1000</span></span>
<span class="line"><span class="string">delay</span>: <span class="expr">{{duration}}</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP</span> <span class="number">200</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"><span class="method">GET</span> <span class="url">http://bar.com:8000/resolve</span></span>
<span class="line"><span class="section-header">[Options]</span></span>
<span class="line"><span class="string">variable</span>: port=<span class="number">8000</span></span>
<span class="line"><span class="string">resolve</span>: <span class="string">bar.com:{{port}}:127.0.0.1</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP</span> <span class="number">200</span></span>
<span class="line"><span class="string">`Hello World!`</span></span>
</span></span></code></pre>
22 changes: 22 additions & 0 deletions integration/tests_ok/options_template.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Reuse existing tests with options
# using template

GET http://localhost:8000/redirect-absolute
[Options]
variable: redirect=true
location: {{redirect}}

GET http://localhost:8000/delay-init
HTTP 200
GET http://localhost:8000/delay # This request must reach the server at least 1000ms after the previous request
[Options]
variable: duration=1000
delay: {{duration}}
HTTP 200

GET http://bar.com:8000/resolve
[Options]
variable: port=8000
resolve: bar.com:{{port}}:127.0.0.1
HTTP 200
`Hello World!`
1 change: 1 addition & 0 deletions integration/tests_ok/options_template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/redirect-absolute","options":[{"name":"variable","value":"redirect=true"},{"name":"location","value":"{{redirect}}"}]}},{"request":{"method":"GET","url":"http://localhost:8000/delay-init"},"response":{"status":200}},{"request":{"method":"GET","url":"http://localhost:8000/delay","options":[{"name":"variable","value":"duration=1000"},{"name":"delay","value":"{{duration}}"}]},"response":{"status":200}},{"request":{"method":"GET","url":"http://bar.com:8000/resolve","options":[{"name":"variable","value":"port=8000"},{"name":"resolve","value":"bar.com:{{port}}:127.0.0.1"}]},"response":{"status":200,"body":{"type":"text","value":"Hello World!"}}}]}
3 changes: 3 additions & 0 deletions integration/tests_ok/options_template.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_ok/options_template.hurl
4 changes: 4 additions & 0 deletions integration/tests_ok/options_template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_ok/options_template.hurl

5 changes: 5 additions & 0 deletions packages/hurl/src/runner/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ pub enum RunnerError {
TemplateVariableNotDefined {
name: String,
},
TemplateVariableInvalidType {
name: String,
value: String,
expecting: String,
},
InvalidJson {
value: String,
},
Expand Down
6 changes: 6 additions & 0 deletions packages/hurl/src/runner/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Error for runner::Error {
RunnerError::InvalidUrl(..) => "Invalid URL".to_string(),
RunnerError::InvalidUrlPrefix(..) => "Invalid URL".to_string(),
RunnerError::TemplateVariableNotDefined { .. } => "Undefined variable".to_string(),
RunnerError::TemplateVariableInvalidType { .. } => "Invalid variable type".to_string(),
RunnerError::HttpConnection { .. } => "HTTP connection".to_string(),
RunnerError::CouldNotResolveProxyName => "HTTP connection".to_string(),
RunnerError::CouldNotResolveHost(_) => "HTTP connection".to_string(),
Expand Down Expand Up @@ -82,6 +83,11 @@ impl Error for runner::Error {
RunnerError::TemplateVariableNotDefined { name } => {
format!("you must set the variable {name}")
}
RunnerError::TemplateVariableInvalidType {
value, expecting, ..
} => {
format!("expecting {expecting}, actual value is <{value}>")
}
RunnerError::HttpConnection(message) => message.to_string(),
RunnerError::CouldNotResolveProxyName => "could not resolve proxy name".to_string(),
RunnerError::CouldNotResolveHost(host) => format!("could not resolve host <{host}>"),
Expand Down
16 changes: 11 additions & 5 deletions packages/hurl/src/runner/hurl_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ use hurl_core::ast::*;
use hurl_core::error::Error;
use hurl_core::parser;

use crate::http;
use crate::http::Call;
use crate::runner::core::*;
use crate::runner::runner_options::RunnerOptions;
use crate::runner::{entry, options, Value};
use crate::util::logger::{ErrorFormat, Logger, LoggerOptions, LoggerOptionsBuilder};
use crate::{http, runner};

/// Runs a Hurl `content` and returns a [`HurlResult`] upon completion.
///
Expand Down Expand Up @@ -114,7 +114,8 @@ pub fn run(
// We compute the new logger for this entry, before entering into the `run`
// function because entry options can modify the logger and we want the preamble
// "Executing entry..." to be displayed based on the entry level verbosity.
let logger = get_entry_logger(entry, logger_options);
let logger =
get_entry_logger(entry, logger_options, &variables).map_err(|e| e.description())?;
if let Some(pre_entry) = runner_options.pre_entry {
let exit = pre_entry(entry.clone());
if exit {
Expand Down Expand Up @@ -425,8 +426,13 @@ fn log_errors(entry_result: &EntryResult, content: &str, retry: bool, logger: &L
/// Creates a new logger for this entry.
/// Verbosity can be overridden at entry level with an Options section so each
/// entry has its own logger.
fn get_entry_logger(entry: &Entry, logger_options: &LoggerOptions) -> Logger {
let entry_verbosity = options::get_entry_verbosity(entry, &logger_options.verbosity);
fn get_entry_logger(
entry: &Entry,
logger_options: &LoggerOptions,
variables: &HashMap<String, Value>,
) -> Result<Logger, runner::Error> {
let entry_verbosity =
options::get_entry_verbosity(entry, &logger_options.verbosity, variables)?;
let entry_logger_options = LoggerOptionsBuilder::new()
.color(logger_options.color)
.filename(&logger_options.filename)
Expand All @@ -435,7 +441,7 @@ fn get_entry_logger(entry: &Entry, logger_options: &LoggerOptions) -> Logger {
.verbosity(entry_verbosity)
.test(logger_options.test)
.build();
Logger::from(&entry_logger_options)
Ok(Logger::from(&entry_logger_options))
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 5be7eb9

Please sign in to comment.