Initialise Error Handler with a native "silent" callback

The callback can not be implemented because it needs to be variadic
This commit is contained in:
Fabrice Reix 2021-05-26 10:06:29 +02:00
parent b9f6032c56
commit 2f18340f00
10 changed files with 55 additions and 8 deletions

5
Cargo.lock generated
View File

@ -333,6 +333,7 @@ dependencies = [
"atty",
"base64",
"brotli",
"cc",
"chrono",
"clap",
"colored",
@ -419,9 +420,9 @@ checksum = "3286f09f7d4926fc486334f28d8d2e6ebe4f7f9994494b6dab27ddfad2c9b11b"
[[package]]
name = "libxml"
version = "0.2.15"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13d529280405174bd6f363d0f8a3086c27e6e60cc0efbc18cf94338f002f461b"
checksum = "496410f67173a59f4e98954c4e7fabe96f6707d46c5f781eb01d96b2703ea6fc"
dependencies = [
"libc",
"pkg-config",

View File

@ -0,0 +1 @@
4

View File

@ -0,0 +1 @@
<div class="hurl-file"><div class="hurl-entry"><div class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/error-assert-xpath</span></span></div><div class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="status">200</span></span><span class="line section-header">[Asserts]</span></span><span class="line"><span class="query-type">xpath</span> <span class="string">"strong(//head/title)"</span> <span class="predicate-type">equals</span> <span class="string">"Welcome to Quiz!"</span></span></div></div></div>

View File

@ -0,0 +1,4 @@
GET http://localhost:8000/error-assert-xpath
HTTP/1.0 200
[Asserts]
xpath "strong(//head/title)" equals "Welcome to Quiz!"

View File

@ -0,0 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/error-assert-xpath"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"xpath","expr":"strong(//head/title)"},"predicate":{"type":"equal","value":"Welcome to Quiz!"}}]}}]}

View File

@ -0,0 +1,6 @@
from tests import app
@app.route("/error-assert-xpath")
def error_assert_xpath():
return '<html><head><title>Test</title></head></html>'

View File

@ -28,7 +28,7 @@ encoding = "0.2"
float-cmp = "0.6.0"
hurl_core = { version = "1.1.0", path = "../hurl_core" }
libflate = "1.0.2"
libxml = "0.2.12"
libxml = "0.2.17"
percent-encoding = "2.1.0"
regex = "1.1.0"
serde = "1.0.104"
@ -38,6 +38,8 @@ url = "2.1.0"
[target.'cfg(unix)'.dependencies]
termion = "1.5.5"
[build-dependencies]
cc = "1.0.52"

10
packages/hurl/build.rs Normal file
View File

@ -0,0 +1,10 @@
use cc::Build;
use std::path::Path;
fn main() {
let project_root = Path::new(env!("CARGO_MANIFEST_DIR"));
let native_src = project_root.join("native");
Build::new()
.file(native_src.join("libxml.c"))
.compile("mylib");
}

View File

@ -0,0 +1,5 @@
// This callback will prevent from outputting error messages
// It could not be implemented in Rust, because the function is variadic
void silentErrorFunc(void *ctx, const char * msg, ...)
{
}

View File

@ -17,7 +17,6 @@
*/
// unique entry point to libxml
extern crate libxml;
use std::ffi::CStr;
@ -61,14 +60,24 @@ pub fn eval_html(html: String, expr: String) -> Result<Value, XpathError> {
}
}
extern "C" {
pub fn silentErrorFunc(
ctx: *mut ::std::os::raw::c_void,
msg: *const ::std::os::raw::c_char,
...
);
}
pub fn eval(doc: libxml::tree::Document, expr: String) -> Result<Value, XpathError> {
let context = match libxml::xpath::Context::new(&doc) {
Ok(context) => context,
_ => panic!("error setting context in xpath module"),
};
unsafe {
libxml::bindings::initGenericErrorDefaultFunc(&mut None);
libxml::bindings::initGenericErrorDefaultFunc(&mut Some(silentErrorFunc));
}
let result = match context.evaluate(expr.as_str()) {
Ok(object) => object,
Err(_) => return Err(XpathError::Eval {}),
@ -123,7 +132,7 @@ mod tests {
assert_eq!(eval_xml(xml.clone(), xpath).unwrap(), Value::from_f64(2.0));
let xpath = String::from("number(//food/banana/@price)");
assert_eq!(eval_xml(xml.clone(), xpath).unwrap(), Value::from_f64(1.1));
assert_eq!(eval_xml(xml, xpath).unwrap(), Value::from_f64(1.1));
}
#[test]
@ -181,7 +190,7 @@ mod tests {
);
let xpath = String::from("normalize-space(/html/head/meta/@charset)");
assert_eq!(
eval_html(html.clone(), xpath).unwrap(),
eval_html(html, xpath).unwrap(),
Value::String(String::from("UTF-8"))
);
}
@ -191,6 +200,13 @@ mod tests {
let html = String::from(r#"<html></html>"#);
//let xpath = String::from("boolean(count(//a[contains(@href,'xxx')]))");
let xpath = String::from("boolean(count(//a[contains(@href,'xxx')]))");
assert_eq!(eval_html(html.clone(), xpath).unwrap(), Value::Bool(false));
assert_eq!(eval_html(html, xpath).unwrap(), Value::Bool(false));
}
#[test]
fn test_unregistered_function() {
let html = String::from(r#"<html></html>"#);
let xpath = String::from("strong(//head/title)");
assert_eq!(eval_html(html, xpath).err().unwrap(), XpathError::Eval);
}
}