@@ -98,6 +98,57 @@ public class RESTExternalVariableTest {
9898 .elseUse (ElementSelectors .byName )
9999 .build ());
100100
101+ @ Test
102+ public void queryPostWithExternalVariableNotSupplied () throws IOException {
103+ final String query =
104+ "<exist:query xmlns:exist=\" http://exist.sourceforge.net/NS/exist\" xmlns:sx=\" http://exist-db.org/xquery/types/serialized\" xmlns:" + TEST_PREFIX + "=\" " + TEST_NAMESPACE + "\" xmlns:xs=\" http://www.w3.org/2001/XMLSchema\" wrap=\" yes\" typed=\" yes\" >\n " +
105+ "\t <exist:text><![CDATA[\n " +
106+ "declare variable $local:my-variable as xs:string* external;\n " +
107+ "$local:my-variable\n " +
108+ "\t ]]></exist:text>\n " +
109+ "</exist:query>\n " ;
110+
111+ final HttpResponse response = doPostWithAuth (getResourceUri (), query );
112+ final int resultStatusCode = response .getStatusLine ()
113+ .getStatusCode ();
114+
115+ assertEquals ("Server returned response code: " + resultStatusCode , HttpStatus .BAD_REQUEST_400 , resultStatusCode );
116+
117+ final String actual = readResponse (response .getEntity ());
118+ assertThat (actual , CompareMatcher .isIdenticalTo ("<exception><path>/db/test/test.xml</path><message>err:XPDY0002 The value of external variable: local:my-variable has not been set</message></exception>" ));
119+ }
120+
121+ @ Test
122+ public void queryPostWithExternalVariableUndeclared () throws IOException {
123+ final String query =
124+ "<exist:query xmlns:exist=\" http://exist.sourceforge.net/NS/exist\" xmlns:sx=\" http://exist-db.org/xquery/types/serialized\" xmlns:" + TEST_PREFIX + "=\" " + TEST_NAMESPACE + "\" xmlns:xs=\" http://www.w3.org/2001/XMLSchema\" wrap=\" yes\" typed=\" yes\" >\n " +
125+ "\t <exist:variables>\n " +
126+ "\t \t <exist:variable>\n " +
127+ "\t \t \t <exist:qname><exist:prefix>local</exist:prefix><exist:localname>my-variable</exist:localname></exist:qname>\n " +
128+ "\t \t \t <sx:sequence><sx:value type=\" xs:string\" >hello</sx:value></sx:sequence>\n " +
129+ "\t \t </exist:variable>\n " +
130+ "\t \t <exist:variable>\n " +
131+ "\t \t \t <exist:qname><exist:prefix>local</exist:prefix><exist:localname>other-variable</exist:localname></exist:qname>\n " +
132+ "\t \t \t <sx:sequence><sx:value type=\" xs:string\" >goodbye</sx:value></sx:sequence>\n " +
133+ "\t \t </exist:variable>\n " +
134+ "\t </exist:variables>\n " +
135+ "\t <exist:text><![CDATA[\n " +
136+ "declare variable $local:my-variable as xs:string* external;\n " +
137+ "$local:my-variable\n " +
138+ "\t ]]></exist:text>\n " +
139+ "</exist:query>\n " ;
140+
141+ final HttpResponse response = doPostWithAuth (getResourceUri (), query );
142+ final int resultStatusCode = response .getStatusLine ()
143+ .getStatusCode ();
144+
145+ assertEquals ("Server returned response code: " + resultStatusCode , HttpStatus .BAD_REQUEST_400 , resultStatusCode );
146+
147+ final String actual = readResponse (response .getEntity ());
148+
149+ assertThat (actual , CompareMatcher .isIdenticalTo ("<exception><path>/db/test/test.xml</path><message>err:XPDY0002 External variable local:other-variable is not declared in the XQuery</message></exception>" ));
150+ }
151+
101152 @ Test
102153 public void queryPostWithExternalVariableUntypedNotSupplied () throws IOException {
103154 queryPostWithExternalVariable (HttpStatus .BAD_REQUEST_400 , null , (ExternalVariableValueRep []) null );
@@ -1592,7 +1643,8 @@ private void queryPostWithExternalVariable(final Tuple2<Integer, String> expecte
15921643
15931644 assertEquals ("Server returned response code: " + resultStatusCode , (int ) expectedResponse ._1 , resultStatusCode );
15941645
1595- final String actual = readResponse (response .getEntity ());
1646+ String actual = readResponse (response .getEntity ());
1647+ actual = actual .replaceFirst ("\\ s*\\ [source:[^\\ ]]*\\ ]</message></exception>$" , "</message></exception>" ); // NOTE(AR) remove any source information from the actual response
15961648
15971649 @ Nullable final String expected ;
15981650 if (expectedResponse ._1 == HttpStatus .OK_200 ) {
0 commit comments