Skip to content

Commit

Permalink
Add test cases to validate error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuvindu committed Jul 16, 2024
1 parent 2c85bf9 commit 4c52958
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
14 changes: 12 additions & 2 deletions ballerina/modules/soap11/tests/http_soap_service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ crypto:PublicKey serverPublicKey = check crypto:decodeRsaPublicKeyFromTrustStore

service / on new http:Listener(9090) {

resource function post .() returns xml|error {
return xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AddResponse xmlns="http://tempuri.org/"><AddResult>5</AddResult></AddResponse></soap:Body></soap:Envelope>`;
resource function post .(http:Request request) returns xml|error {
string action = check request.getHeader("SOAPAction");
if action == "http://tempuri.org/Add" {
return xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AddResponse xmlns="http://tempuri.org/"><AddResult>5</AddResult></AddResponse></soap:Body></soap:Envelope>`;
} else {
return xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://tempuri.org/invalid_action.
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring><detail/></soap:Fault></soap:Body></soap:Envelope>`;
}
}

resource function post getPayload(http:Request request) returns http:Response|error {
Expand Down
33 changes: 32 additions & 1 deletion ballerina/modules/soap11/tests/soap11_client_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,37 @@ function testSendReceive() returns error? {
test:assertEquals(response, expected);
}

@test:Config {
groups: ["soap11", "send_receive"]
}
function testSendReceiveWithInvalidAction() returns error? {
Client soapClient = check new ("http://localhost:9090",
{
inboundSecurity: NO_POLICY,
outboundSecurity: {}
}
);

xml body = xml `<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<quer:Add xmlns:quer="http://tempuri.org/">
<quer:intA>2</quer:intA>
<quer:intB>3</quer:intB>
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml response = check soapClient->sendReceive(body, "http://tempuri.org/invalid_action");
xml expected = xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://tempuri.org/invalid_action.
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring><detail/></soap:Fault></soap:Body></soap:Envelope>`;
test:assertEquals(response, expected);
}

@test:Config {
groups: ["soap11", "send_receive"]
}
Expand Down Expand Up @@ -627,5 +658,5 @@ function testInvalidOutboundConfigWithMime() returns error? {
mtomMessage.push(bytesPart);
xml|Error response = soapClient->sendReceive(mtomMessage, "http://tempuri.org/Add", path = "/getSecuredMimePayload");
test:assertTrue(response is Error);
test:assertEquals((<Error>response).message(), "Outbound security configurations do not match with the SOAP response.");
test:assertEquals((<Error>response).message(), "Outbound security configurations do not match with the SOAP response");
}
10 changes: 9 additions & 1 deletion ballerina/modules/soap12/tests/http_soap_service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ service / on new http:Listener(9090) {
response.setPayload(payload);
return response;
}
return error("Invalid action is found");
xml payload = xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://tempuri.org/invalid_action.
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring><detail/></soap:Fault></soap:Body></soap:Envelope>`;
http:Response response = new;
response.setPayload(payload);
return response;
}

resource function post getSamePayload(http:Request request) returns http:Response|error {
Expand Down
50 changes: 48 additions & 2 deletions ballerina/modules/soap12/tests/soap12_client_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ function testSendReceive12WithAction() returns error? {
test:assertEquals(response, expected);
}

@test:Config {
groups: ["soap12", "send_receive"]
}
function testSendReceive12WithInvalidSoapAction() returns error? {
Client soapClient = check new ("http://localhost:9090");
xml body = xml `<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<quer:Add xmlns:quer="http://tempuri.org/">
<quer:intA>2</quer:intA>
<quer:intB>3</quer:intB>
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml response = check soapClient->sendReceive(body, "http://tempuri.org/invalid_action", path = "/getActionPayload");
xml expected = xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://tempuri.org/invalid_action.
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring><detail/></soap:Fault></soap:Body></soap:Envelope>`;
test:assertEquals(response, expected);
}

@test:Config {
groups: ["soap12", "send_receive"]
}
Expand Down Expand Up @@ -199,7 +224,7 @@ function testSendReceive12WithMime2() returns error? {
}

@test:Config {
groups: ["soap12", "send_receive", "mime", "h"]
groups: ["soap12", "send_receive", "mime"]
}
function testSendReceive12MimeWithoutAction() returns error? {
Client soapClient = check new ("http://localhost:9090");
Expand Down Expand Up @@ -322,6 +347,27 @@ function testSendReceiveError() returns error? {
test:assertEquals((<Error>response).message(), SOAP_ERROR);
}

@test:Config {
groups: ["soap12", "send_receive"]
}
function testSendReceiveWithInvalidAction() returns error? {
Client soapClient = check new ("http://www.dneonline.com/invalidcalculator.asmx?WSDL");
xml body = xml `<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<quer:Add xmlns:quer="http://tempuri.org/">
<quer:intA>2</quer:intA>
<quer:intB>3</quer:intB>
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|Error response = soapClient->sendReceive(body, "http://tempuri.org/invalid_action");
io:println(response);
test:assertTrue(response is Error);
test:assertEquals((<Error>response).message(), SOAP_ERROR);
}

@test:Config {
groups: ["soap12", "send_receive"]
}
Expand Down Expand Up @@ -608,5 +654,5 @@ function testInvalidOutboundConfigWithMime12() returns error? {

mime:Entity[]|Error response = soapClient->sendReceive(mtomMessage, "http://tempuri.org/Add", path = "/getSecuredMimePayload");
test:assertTrue(response is Error);
test:assertEquals((<Error>response).message(), "Outbound security configurations do not match with the SOAP response.");
test:assertEquals((<Error>response).message(), "Outbound security configurations do not match with the SOAP response");
}

0 comments on commit 4c52958

Please sign in to comment.