main icon indicating copy to clipboard operation
main copied to clipboard

Mocking a SOAP / none-REST webservice

Open jobe451 opened this issue 3 years ago • 7 comments

Is it possible to mock a SOAP-service with mocks-server?

jobe451 avatar Jan 31 '23 09:01 jobe451

Hi @jobe451 , I don't know much about SOAP, but I suppose that you could simulate it with mocks-server if the protocol is HTTP. You could send XML responses using the text route variants.

Anyway, I will let this issue opened in order to investigate further about how to provide a better support and documentation for SOAP services.

javierbrea avatar Mar 02 '23 19:03 javierbrea

It seems that both file format and text format are working fine with simple requests and responses. But I don't know if it can handle SOAP services for more complex scenarios / requests. Do you have any comment / news on it?

#users.json file:

[
  {
    "id": "get-users",
    "url": "/api/users",
    "method": "GET",
    "variants": [
      {
        "id": "success",
        "type": "json",
        "options": {
          "status": 200,
          "body": [
            {
              "id": 1,
              "name": "my name"
            },
            {
              "id": 2,
              "name": "my brother's name"
            }
          ]
        }
      },
      {
        "id": "xml-sample-with-file",
        "type": "file",
        "options": {
          "status": 200,
          "path": "mocks/routes/xml-sample.xml"
        }
      },
      {
        "id": "xml-sample-with-text",
        "type": "text",
        "options": {
          "status": 200,
          "headers": {
            "Content-type": "application/xml"
          },
          "body": "<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>"
        }
      }
    ]
  }
]

#collections.json file:

[
  {
    "id": "base",
    "routes": ["get-users:xml-sample-with-text"]
  }
]

EmreCihanbeyoglu avatar Mar 22 '23 07:03 EmreCihanbeyoglu

Hi @EmreCihanbeyoglu , Could you be more specific about the "more complex scenarios / requests" that you need to handle?

javierbrea avatar Apr 07 '23 08:04 javierbrea

What if request body contains XML?

jatla avatar Oct 05 '23 09:10 jatla

Hi @jatla , your request body can contain XML without problem, it won't produce any error.

But, in case you want to use the middleware variant and access to the data in the request body, you'll need to add an extra middleware for parsing the xml data. You could do it by installing the express-xml-bodyparser dependency, and defining a route for using it before your routes needing to read the xml parsed data. Something like:

var xmlparser = require('express-xml-bodyparser');

module.exports = [
  {
    id: "xml-parser",
    url: "*", // URLs using the xml parser
    method: ["POST", "PUT", "PATCH"],
    variants: [
      {
        id: "enabled",
        type: "middleware",
        options: {
          middleware: xmlparser(),
        },
      },
    ],
  },
  {
    id: "create-user",
    url: "/api/users",
    method: "POST",
    variants: [
      {
        id: "middleware",
        type: "middleware",
        options: {
          middleware:(req, res, _next, core) => {
            // Here is available parsed XML body
            core.logger.info(JSON.stringify(req.body));

            res.set("Content-Type", "application/xml");
            res.status(201).send(`
            <user>
               <id type="integer">5</id>
               <created-at type="dateTime">2008-10-27T22:54:10+02:00</created-at>
               <updated-at type="dateTime">2013-04-19T22:02:26+03:00</updated-at>
            </user>
            `);
          }
        },
      },
    ],
  },
];

And don't forget to load the xml parser route before the others in your routes collection:

[
  {
    "id": "xml-routes",
    "routes": ["xml-parser:enabled", "create-user:middleware"]
  }
]

javierbrea avatar Oct 08 '23 08:10 javierbrea

Thank you for quick reply.

I was thinking of enabling it here similar to JSON body parser - https://github.com/mocks-server/main/blob/master/packages/core/src/server/Server.js#L202

I tried making code changes in my workspace and run the server in my workspace. Being a novice in all things node js, I couldn't figure out how to run the server from workspace. Could you point me to any documentation on running the server from workspace?

jatla avatar Oct 10 '23 03:10 jatla

Hi again @jatla , I will kept this issue open in order to implement a new variant "xml", and also to add the XML parser to the core package, so it can be enabled with an option. Meanwhile, I recommend you to use the method I mentioned. Cloning the repo to execute the server is not the better option. Take into account that it is a monorepo configured for development stuff, not for distribution.

javierbrea avatar Oct 10 '23 05:10 javierbrea