ShaftHQ / SHAFT_ENGINE

SHAFT is a unified test automation engine for web, mobile, API, CLI, database, and desktop e2e testing. Powered by best-in-class frameworks, SHAFT provides a wizard-like syntax to drive your automation efficiently, maximize your ROI, and minimize your learning curve with no limitations! Stop reinventing the wheel! Upgrade now!
https://shafthq.github.io/
MIT License
330 stars 127 forks source link

[Bug]: (API) SOAP Request Fails Due to Improper Encoding of XML Content Type #1697

Closed KyrillosNageh closed 2 months ago

KyrillosNageh commented 2 months ago

Describe the bug

When attempting to send a SOAP request with an XML body, the request fails due to an improper encoding of the content type text/xml. The system throws a java.lang.IllegalArgumentException indicating that it doesn't know how to encode the SOAP envelope as a byte stream

Environment

  1. SHAFT_Engine version : Latest version (8.3.20240803)

To Reproduce

Steps to reproduce the behavior:

  1. Initialize a RequestBuilder object to make a SOAP request.
  2. Set the Content-Type header to text/xml.
  3. Attempt to send the request with the provided XML body.
  4. Observe the failure in the logs.

Example code:

api = new SHAFT.API("http://www.dneonline.com/calculator.asmx"); String requestXml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n" + " \n" + " \n" + " \n" + " 10</tem:intA>\n" + " 2</tem:intB>\n" + " </tem:Multiply>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>";

api.addHeader("Content-Type", "text/xml"); api.addHeader("SOAPAction", "http://tempuri.org/Multiply"); Response response = api.post("") .setRequestBody(requestXml) .performRequest();

Expected behavior

The SOAP request should be properly encoded and sent to the server, with the XML body being handled as text/xml content.

Actual behavior

The system throws a java.lang.IllegalArgumentException, indicating that it does not know how to encode the SOAP envelope as a byte stream, causing the request to fail.

Link to SHAFT_Engine Console logs

https://gist.github.com/KyrillosNageh/086244efc9d26761bfc34e03c58a78e7

MohabMohie commented 2 months ago

can you kindly share a native rest assured code snippet that works?

KyrillosNageh commented 2 months ago
    String requestXml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n" +
            "   <soapenv:Header/>\n" +
            "   <soapenv:Body>\n" +
            "      <tem:Multiply>\n" +
            "         <tem:intA>10</tem:intA>\n" +
            "         <tem:intB>2</tem:intB>\n" +
            "      </tem:Multiply>\n" +
            "   </soapenv:Body>\n" +
            "</soapenv:Envelope>";

    Response response = given()
            .header("Content-Type", "text/xml")
            //.contentType(ContentType.XML)
            .header("SOAPAction", "http://tempuri.org/" + "Multiply")
            .body(requestXml)
            .when()
            .post("http://www.dneonline.com/calculator.asmx")
            .then()
            .statusCode(200)  // Validate status code directly
            .extract().response();

    String Result =RestActions.getResponseXMLValue(response, "AddResponse/AddResult");

    SHAFT.Report.log(">>>>>>>>>>>> Test result >>>> " + Result);
KyrillosNageh commented 2 months ago

@MohabMohie The issue occurs when we set Content-Type using api.addHeader("Content-Type", "application/json"); Where in this example when we use this api.addHeader("Content-Type", "application/json"); give the error that exist on screenshot but with this, it works fine .setContentType(ContentType.JSON)

the main reason for it until now I think:

  1. use to api.addHeader("Content-Type", "application/json"); to set Content-Type
  2. Request body contains \n

image

public void testCreateUser() { String requestBody= " {\n" + " \"name\": \"Test user1\",\n" + " \"job\": \"leader\"\n" + " }";

    api = new SHAFT.API("https://reqres.in/api/");
   api.addHeader("Content-Type", "application/json");
    Response response = api.post("users")
            //.setContentType(ContentType.JSON)
                    .setRequestBody(requestBody)
                            .performRequest();
    System.out.println("Response Body: " + response.getBody().asString());
}

image