Add integration tests on XPath/namespaces.

This commit is contained in:
jcamiel 2022-03-11 14:20:18 +01:00 committed by Fabrice Reix
parent a3fe70b4c8
commit 80d1e6c726
6 changed files with 156 additions and 8 deletions

View File

@ -1 +1,4 @@
curl 'http://localhost:8000/assert-xpath'
curl 'http://localhost:8000/assert-xpath-simple-namespaces'
curl 'http://localhost:8000/assert-xpath-svg'
curl 'http://localhost:8000/assert-xpath-namespaces'

View File

@ -2,9 +2,49 @@
</span><span class="response"><span class="line"></span>
<span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="line section-header">[Asserts]</span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"normalize-space(//data)"</span> <span class="predicate-type">equals</span> <span class="string">"café"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"normalize-space(//data)"</span> <span class="predicate-type">equals</span> <span class="string">"caf\u{00e9}"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"normalize-space(//data)"</span> <span class="predicate-type">==</span> <span class="string">"café"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"normalize-space(//data)"</span> <span class="predicate-type">==</span> <span class="string">"caf\u{00e9}"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"//toto"</span> <span class="not">not</span> <span class="predicate-type">exists</span></span>
<span class="line"></span>
<span class="xml"><span class="line">&lt;data&gt;café&lt;/data&gt;</span></span>
</span></span></code></pre>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"></span>
<span class="line"></span><span class="comment"># Test XPath assert with XML namespace.</span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/assert-xpath-simple-namespaces</span></span>
</span><span class="response"><span class="line"></span>
<span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="line section-header">[Asserts]</span>
<span class="line"></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"string(//bk:book/bk:title)"</span> <span class="predicate-type">==</span> <span class="string">"Cheaper by the Dozen"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"string(//*[name()='bk:book']/*[name()='bk:title'])"</span> <span class="predicate-type">==</span> <span class="string">"Cheaper by the Dozen"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"string(//*[local-name()='book']/*[local-name()='title'])"</span> <span class="predicate-type">==</span> <span class="string">"Cheaper by the Dozen"</span></span>
<span class="line"></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"string(//bk:book/isbn:number)"</span> <span class="predicate-type">==</span> <span class="string">"1568491379"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"string(//*[name()='bk:book']/*[name()='isbn:number'])"</span> <span class="predicate-type">==</span> <span class="string">"1568491379"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"string(//*[local-name()='book']/*[local-name()='number'])"</span> <span class="predicate-type">==</span> <span class="string">"1568491379"</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"></span>
<span class="line"></span><span class="comment"># Test XPath assert with default XML namespace.</span>
<span class="line"></span><span class="comment"># _ can be used to target a default namespace.</span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/assert-xpath-svg</span></span>
</span><span class="response"><span class="line"></span>
<span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="line section-header">[Asserts]</span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"//_:svg/_:g/_:circle"</span> <span class="subquery-type">count</span> <span class="predicate-type">==</span> <span class="number">3</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"//*[local-name()='svg']/*[local-name()='g']/*[local-name()='circle']"</span> <span class="subquery-type">count</span> <span class="predicate-type">==</span> <span class="number">3</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"//*[name()='svg']/*[name()='g']/*[name()='circle']"</span> <span class="subquery-type">count</span> <span class="predicate-type">==</span> <span class="number">3</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"></span>
<span class="line"></span><span class="comment"># Test XPath assert with default and prefixed XML namespace.</span>
<span class="line"></span><span class="comment"># _ can be used to target a default namespace.</span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/assert-xpath-namespaces</span></span>
</span><span class="response"><span class="line"></span>
<span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="line section-header">[Asserts]</span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"string(//_:book/_:title)"</span> <span class="predicate-type">==</span> <span class="string">"Cheaper by the Dozen"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"string(//_:book/isbn:number)"</span> <span class="predicate-type">==</span> <span class="string">"1568491379"</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"//*[name()='book']/*[name()='notes']"</span> <span class="subquery-type">count</span> <span class="predicate-type">==</span> <span class="number">1</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"//*[local-name()='book']/*[local-name()='notes']"</span> <span class="subquery-type">count</span> <span class="predicate-type">==</span> <span class="number">1</span></span>
<span class="line"><span class="query-type">xpath</span> <span class="string">"//_:book/_:notes/*[local-name()='p']"</span> <span class="subquery-type">count</span> <span class="predicate-type">==</span> <span class="number">1</span></span>
</span></span><span class="line"></span>
</code></pre>

View File

@ -2,8 +2,48 @@ GET http://localhost:8000/assert-xpath
HTTP/1.0 200
[Asserts]
xpath "normalize-space(//data)" equals "café"
xpath "normalize-space(//data)" equals "caf\u{00e9}"
xpath "normalize-space(//data)" == "café"
xpath "normalize-space(//data)" == "caf\u{00e9}"
xpath "//toto" not exists
<data>café</data>
# Test XPath assert with XML namespace.
GET http://localhost:8000/assert-xpath-simple-namespaces
HTTP/1.0 200
[Asserts]
xpath "string(//bk:book/bk:title)" == "Cheaper by the Dozen"
xpath "string(//*[name()='bk:book']/*[name()='bk:title'])" == "Cheaper by the Dozen"
xpath "string(//*[local-name()='book']/*[local-name()='title'])" == "Cheaper by the Dozen"
xpath "string(//bk:book/isbn:number)" == "1568491379"
xpath "string(//*[name()='bk:book']/*[name()='isbn:number'])" == "1568491379"
xpath "string(//*[local-name()='book']/*[local-name()='number'])" == "1568491379"
# Test XPath assert with default XML namespace.
# _ can be used to target a default namespace.
GET http://localhost:8000/assert-xpath-svg
HTTP/1.0 200
[Asserts]
xpath "//_:svg/_:g/_:circle" count == 3
xpath "//*[local-name()='svg']/*[local-name()='g']/*[local-name()='circle']" count == 3
xpath "//*[name()='svg']/*[name()='g']/*[name()='circle']" count == 3
# Test XPath assert with default and prefixed XML namespace.
# _ can be used to target a default namespace.
GET http://localhost:8000/assert-xpath-namespaces
HTTP/1.0 200
[Asserts]
xpath "string(//_:book/_:title)" == "Cheaper by the Dozen"
xpath "string(//_:book/isbn:number)" == "1568491379"
xpath "//*[name()='book']/*[name()='notes']" count == 1
xpath "//*[local-name()='book']/*[local-name()='notes']" count == 1
xpath "//_:book/_:notes/*[local-name()='p']" count == 1

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/assert-xpath"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"xpath","expr":"normalize-space(//data)"},"predicate":{"type":"equal","value":"café"}},{"query":{"type":"xpath","expr":"normalize-space(//data)"},"predicate":{"type":"equal","value":"café"}},{"query":{"type":"xpath","expr":"//toto"},"predicate":{"not":true,"type":"exist"}}],"body":{"type":"xml","value":"<data>café</data>"}}}]}
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/assert-xpath"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"xpath","expr":"normalize-space(//data)"},"predicate":{"type":"equal","value":"café"}},{"query":{"type":"xpath","expr":"normalize-space(//data)"},"predicate":{"type":"equal","value":"café"}},{"query":{"type":"xpath","expr":"//toto"},"predicate":{"not":true,"type":"exist"}}],"body":{"type":"xml","value":"<data>café</data>"}}},{"request":{"method":"GET","url":"http://localhost:8000/assert-xpath-simple-namespaces"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"xpath","expr":"string(//bk:book/bk:title)"},"predicate":{"type":"equal","value":"Cheaper by the Dozen"}},{"query":{"type":"xpath","expr":"string(//*[name()='bk:book']/*[name()='bk:title'])"},"predicate":{"type":"equal","value":"Cheaper by the Dozen"}},{"query":{"type":"xpath","expr":"string(//*[local-name()='book']/*[local-name()='title'])"},"predicate":{"type":"equal","value":"Cheaper by the Dozen"}},{"query":{"type":"xpath","expr":"string(//bk:book/isbn:number)"},"predicate":{"type":"equal","value":"1568491379"}},{"query":{"type":"xpath","expr":"string(//*[name()='bk:book']/*[name()='isbn:number'])"},"predicate":{"type":"equal","value":"1568491379"}},{"query":{"type":"xpath","expr":"string(//*[local-name()='book']/*[local-name()='number'])"},"predicate":{"type":"equal","value":"1568491379"}}]}},{"request":{"method":"GET","url":"http://localhost:8000/assert-xpath-svg"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"xpath","expr":"//_:svg/_:g/_:circle","subquery":{"type":"count"}},"predicate":{"type":"equal","value":3}},{"query":{"type":"xpath","expr":"//*[local-name()='svg']/*[local-name()='g']/*[local-name()='circle']","subquery":{"type":"count"}},"predicate":{"type":"equal","value":3}},{"query":{"type":"xpath","expr":"//*[name()='svg']/*[name()='g']/*[name()='circle']","subquery":{"type":"count"}},"predicate":{"type":"equal","value":3}}]}},{"request":{"method":"GET","url":"http://localhost:8000/assert-xpath-namespaces"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"xpath","expr":"string(//_:book/_:title)"},"predicate":{"type":"equal","value":"Cheaper by the Dozen"}},{"query":{"type":"xpath","expr":"string(//_:book/isbn:number)"},"predicate":{"type":"equal","value":"1568491379"}},{"query":{"type":"xpath","expr":"//*[name()='book']/*[name()='notes']","subquery":{"type":"count"}},"predicate":{"type":"equal","value":1}},{"query":{"type":"xpath","expr":"//*[local-name()='book']/*[local-name()='notes']","subquery":{"type":"count"}},"predicate":{"type":"equal","value":1}},{"query":{"type":"xpath","expr":"//_:book/_:notes/*[local-name()='p']","subquery":{"type":"count"}},"predicate":{"type":"equal","value":1}}]}}]}

View File

@ -1 +1,13 @@
<data>café</data>
<?xml version="1.0"?>
<!-- initially, the default namespace is "books" -->
<book xmlns='urn:loc.gov:books'
xmlns:isbn='urn:ISBN:0-395-36341-6'>
<title>Cheaper by the Dozen</title>
<isbn:number>1568491379</isbn:number>
<notes>
<!-- make HTML the default namespace for some commentary -->
<p xmlns='http://www.w3.org/1999/xhtml'>
This is a <i>funny</i> book!
</p>
</notes>
</book>

View File

@ -1,7 +1,60 @@
# coding=utf-8
from flask import Response
from app import app
@app.route("/assert-xpath")
def assert_xpath():
return "<data>café</data>"
body = "<data>café</data>"
return Response(body, mimetype="text/xml")
@app.route("/assert-xpath-svg")
def assert_xpath_svg():
body = """<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg">
<style type="text/css">
circle:hover {fill-opacity:0.9;}
</style>
<g style="fill-opacity:0.7;">
<circle cx="6.5cm" cy="2cm" r="100" style="fill:red; stroke:black; stroke-width:0.1cm" transform="translate(0,50)" />
<circle cx="6.5cm" cy="2cm" r="100" style="fill:blue; stroke:black; stroke-width:0.1cm" transform="translate(70,150)" />
<circle cx="6.5cm" cy="2cm" r="100" style="fill:green; stroke:black; stroke-width:0.1cm" transform="translate(-70,150)"/>
</g>
</svg>
"""
return Response(body, mimetype="text/xml")
@app.route("/assert-xpath-simple-namespaces")
def assert_xpath_simple_ns():
body = """<?xml version="1.0"?>
<!-- both namespace prefixes are available throughout -->
<bk:book xmlns:bk='urn:loc.gov:books'
xmlns:isbn='urn:ISBN:0-395-36341-6'>
<bk:title>Cheaper by the Dozen</bk:title>
<isbn:number>1568491379</isbn:number>
</bk:book>
"""
return Response(body, mimetype="text/xml")
@app.route("/assert-xpath-namespaces")
def assert_xpath_ns():
body = """<?xml version="1.0"?>
<!-- initially, the default namespace is "books" -->
<book xmlns='urn:loc.gov:books'
xmlns:isbn='urn:ISBN:0-395-36341-6'>
<title>Cheaper by the Dozen</title>
<isbn:number>1568491379</isbn:number>
<notes>
<!-- make HTML the default namespace for some commentary -->
<p xmlns='http://www.w3.org/1999/xhtml'>
This is a <i>funny</i> book!
</p>
</notes>
</book>
"""
return Response(body, mimetype="text/xml")