Skip to content

Commit

Permalink
fix: add restrictions client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakrkris committed Apr 17, 2020
1 parent 2c34d19 commit 56067ba
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 86 deletions.
5 changes: 4 additions & 1 deletion src/parser/xmlHandler.js
Expand Up @@ -123,7 +123,10 @@ class XMLHandler {
if (this.options.enforceRestrictions && descriptor.type) {
const schema = this.schemas[descriptor.type.nsURI];
if (schema) {
const type = schema.simpleTypes[descriptor.type.name];
let type = schema.simpleTypes[descriptor.type.name];
if (!type && descriptor.type.anonymous) {
type = descriptor.type.anonymous;
}
if (type) {
const restriction = type.restriction;
if (restriction) {
Expand Down
13 changes: 11 additions & 2 deletions src/parser/xsd/element.js
Expand Up @@ -82,9 +82,18 @@ class Element extends XSDElement {
}
break;
} else if (child instanceof SimpleType) {
child.$name = this.$name;
let typeQName = child.getQName();
var descriptor = this.descriptor =
new XSDElement.ElementDescriptor(qname, typeQName, form, isMany);
descriptor.isSimple = true;
descriptor.jsType = child.jsType;
descriptor.type = child.type;
if (child.type && child.type.jsType) {
descriptor.jsType = child.type.jsType;
} else if (child.jsType) {
descriptor.jsType = child.jsType;
}
descriptor.type = typeQName;
descriptor.type.anonymous = child;
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/client-restrictions-test.js
Expand Up @@ -374,6 +374,24 @@ describe('SOAP Client', function() {
}, 'http://' + hostname + ":" + server.address().port);
});

it('should throw if the maxLength doesn\'t match its restriction in a private simple type', function (done) {
soap.createClient(__dirname + '/wsdl/restriction_anonymous_types.wsdl', { enforceRestrictions: true }, function (err, client) {
assert.equal(err, null);
try {
client.TestRestrictions({
RestrictionRequest: {
elementWithAnonymousType: 'abcdef'
}
}, function () {
done('It should have thrown error');
});
} catch (err) {
assert.equal(err.message, 'The field elementWithAnonymousType cannot have value abcdef due to the violations: ["length is bigger than maxLength (6 > 3)"]');
done();
}

}, 'http://' + hostname + ":" + server.address().port);
});
});

});
39 changes: 21 additions & 18 deletions test/wsdl-test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions test/wsdl/restriction_anonymous_types.wsdl
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.Restrictions.com"
xmlns:n="http://www.Restriction.com/Types"
xmlns:ns="http://schemas.xmlsoap.org/soap/encoding/"
targetNamespace="http://www.Restrictions.com">
<wsdl:types>
<xsd:schema xmlns:tns="http://www.Restriction.com/Types"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.Restriction.com/Types"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:complexType name="RestrictionRequestType">
<xsd:all>
<xsd:element type="xsd:string" name="stringElement"/>
<xsd:element name="elementWithAnonymousType">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="3"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:all>
</xsd:complexType>
<xs:element name="RestrictionRequest" type="RestrictionRequestType"/>
<xs:element name="RestrictionResponse" type="RestrictionRequestType"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="RestrictionsRequest">
<wsdl:part name="RestrictionsRequest" element="n:RestrictionRequest"/>
</wsdl:message>
<wsdl:message name="RestrictionsResponse">
<wsdl:part name="RestrictionsResponse" element="n:RestrictionResponse"/>
</wsdl:message>
<wsdl:portType name="RestrictionsPortType">
<wsdl:operation name="TestRestrictions">
<wsdl:input message="tns:RestrictionsRequest"/>
<wsdl:output message="tns:RestrictionsResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="RestrictionsBinding" type="tns:RestrictionsPortType">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="TestRestrictions">
<soap:operation soapAction="http://www.Restrictions.com#Restrictions"
style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="RestrictionsService">
<wsdl:port name="RestrictionsPortType" binding="tns:RestrictionsBinding">
<soap:address location="http://www.Restrictions.com/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
8 changes: 7 additions & 1 deletion test/wsdl/restriction_types.xsd
Expand Up @@ -94,9 +94,15 @@
<xs:element name="enumeration1" type="EnumerationType"/>
<xs:element name="enumeration2" type="EnumerationType"/>
<xs:element name="enumeration3" type="EnumerationType"/>
<xs:element name="elementWithAnonymousType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="RestrictionRequest" type="RestrictionRequestType"/>
<xs:element name="RestrictionResponse" type="RestrictionRequestType"/>
</xs:schema>

64 changes: 0 additions & 64 deletions test/wsdl/simpleTypeTest.wsdl

This file was deleted.

0 comments on commit 56067ba

Please sign in to comment.