Contributions
Re: How do I validate a REST XML request using Groovy
Shortly after posting my question, I found an answer. Isn't that always the way. My solution is similar to what you have posted, but includes an error handler class to ensure all errors are reported on. My Groovy isn't very groovy, so this might not be the best way of coding it. I have also edited the code below to remove code that was specific to what I'm trying to do. import javax.xml.XMLConstants import javax.xml.transform.stream.StreamSource import javax.xml.validation.SchemaFactory import org.xml.sax.ErrorHandler import org.xml.sax.SAXException import org.xml.sax.SAXParseException // Define a class to handle XML validation errors class LoggingErrorHandler implements ErrorHandler { def xmlErrorMessages public void warning(SAXParseException ex) { xmlErrorMessages += ex.message xmlErrorMessages += "\n" } public void error(SAXParseException ex) { xmlErrorMessages += ex.message xmlErrorMessages += "\n" } public void fatalError(SAXParseException ex) { xmlErrorMessages += ex.message xmlErrorMessages += "\n" } } // Validate the incoming message against the schema def incomingXml = mockRequest.requestContent def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) def schema = factory.newSchema(new StreamSource(new FileReader("Schema.xsd"))) def validator = schema.newValidator() def errorHandler = new LoggingErrorHandler() validator.errorHandler = errorHandler validator.validate(new StreamSource(new StringReader(incomingXml))) if (errorHandler.xmlErrorMessages != "") { // Do things here to deal with/report the errors } // Remainder of code to do things with a valid XML message2.1KViews0likes0CommentsRest Response does not return specified status code
I created a new REST project containing a mock service with a single response, set its status code to 507, and put some trivial XML in the body of the response. When I ran a request against this service it returned the correct XML but the wrong response code of 200. After a little investigation, I found a workaround by adding the following script to the response. This shouldn't be required, but at least it shouldn't break anything once this bug is fixed. mockRequest.httpResponse.status = mockResponse.responseHttpStatus I am using SoapUI 5.0.0-m-SNAPSHOT. I have also attached the project mentioned above.10 years agoPlace ReadyAPI QuestionsReadyAPI Questions1.1KViews0likes0CommentsHow do I validate a REST XML request using Groovy
I have a REST mock service with an OnRequest script that needs to apply XML validation to incoming requests. I found a post that explains how to do this for SOAP (viewtopic.php?t=2049), but I can't find anything that indicates how to do this for REST. I have the XSD held in the WADL for the service, but I'm equally happy to store it in a file. I assume I need to achieve the following things, and any hints, tips, or complete solutions would be most welcome; Load the XSD - I can't find out the right combination of methods to get me this information (presumably starting from the mockRequest) Retrieve the request Xml - I can do this now I've downloaded the maintenance version of SoapUI 5 Create some kind of Xml Validator and check the request against the XSD - Can I use the WSDL validator that is mentioned in the post above, or is there a REST based version? I'm quite surprised at how hard I'm finding this as I expected it to be trivial.2.5KViews0likes2Comments