mocks-server / main

Node.js mock server running live, interactive mocks in place of real APIs
https://www.mocks-server.org
Apache License 2.0
281 stars 14 forks source link

Mocking a SOAP / none-REST webservice #454

Open jobe451 opened 1 year ago

jobe451 commented 1 year ago

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

javierbrea commented 1 year ago

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.

EmreCihanbeyoglu commented 1 year ago

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"]
  }
]
javierbrea commented 1 year ago

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

jatla commented 9 months ago

What if request body contains XML?

javierbrea commented 8 months ago

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"]
  }
]
jatla commented 8 months ago

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?

javierbrea commented 8 months ago

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.