Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 36 ยท Testing and Tools

Mock SOAP Services and Fault Injection

Build a programmable SOAP mock server in Node.js with canned responses, request recording, latency and injected faults, so you can test client behavior without a real partner system.

Real SOAP back ends are awkward to test against. The partner's test system is down on weekends, it returns different data every day, it charges per call, or it simply cannot be made to fail on demand. You still need to know what your application does when the service times out, returns a fault, or sends malformed XML.

The answer is a mock service: a fake endpoint you control completely. Tools such as SoapUI's MockService, WireMock, Mountebank and Postman mock servers do this as products. In this lesson you will build a small one yourself, so you understand which features matter: canned responses chosen by operation, recording of what clients send, and fault injection to rehearse failure.

The mock runs on loopback inside the example program, and the client is the same program. This keeps everything runnable here. In a real project the mock is started by your test setup (or lives as a shared container) and your application's service URL is pointed at it.

What a useful mock does

  • Matches a request to a scripted answer, usually by operation name and sometimes by a value inside the body.
  • Responds with a canned envelope, a fault, or a deliberate delay.
  • Records every request, so tests can assert that your application sent what it should (the right SOAPAction, the right amount).
  • Misbehaves on purpose: returns HTTP 503, drops the connection, sends truncated XML, or answers very slowly.

A programmable mock

The mock below takes a list of rules. The first rule whose when function accepts the request body wins. The test then runs several scenarios, and finally reads back the recorded requests.

Node.js A scriptable mock with request recording

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The recorded list is what makes mocks valuable for tests: after running your application you can assert that it sent Transfer exactly once and with the right amount.

Fault injection

Failure paths are the least tested and most damaging code in integrations. Fault injection means making failures happen deliberately so you can watch your client's response. Useful injected behaviors:

Injection Simulates Good client reaction
HTTP 503 Overloaded or restarting service Retry with backoff
Slow response Overloaded database behind the service Time-out, then fail cleanly
SOAP Client fault Bad request data Do not retry; report
SOAP Server fault Bug or outage in the service Retry limited; alert
Truncated or invalid XML Proxy cutting the connection Treat as failure, never crash
HTTP 200 with an HTML error page A gateway or login page in the way Detect non-XML content type

The next program wraps a healthy handler in a "chaos" layer driven by a list of behaviors: the first request gets a 503, the second is slow, the third returns broken XML, and then the service behaves. A client with a 300 ms time-out and a validation step is run against it.

Node.js Chaos layer: scripted failures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Four attempts print four different outcomes. If any of those lines surprised you, that is exactly the kind of thing fault injection is for: it shows which failure your code was never designed to survive.

Practical tips

  • Keep mock responses in files (one per scenario) so testers can edit them and so they can be versioned.
  • Generate mocks from the WSDL when you can; tools like SoapUI create example responses automatically.
  • Make the mock fail loudly when no rule matches, as ours does, so an unexpected request is noticed.
  • Do not trust mocks forever. Run a small set of tests against the real test system regularly, so the mock does not drift from reality.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main reason to record requests in a mock?

    Recording lets you verify the outgoing messages: action headers, amounts, counts.
  2. 2. A client receives HTTP 200 with an HTML login page. What should it do?

    Gateways and captive portals often return HTML with status 200; validate content type and envelope structure.
  3. 3. Which injected failure should NOT normally be retried?

    A Client fault is deterministic; retrying cannot fix the request.
  4. 4. Why should a mock return an error when no rule matches?

    A silent default hides mistakes in what the client sends.

Summary

  • A mock service gives you a controllable stand-in for slow, costly or unavailable partners.
  • Match requests by operation and values, respond with canned envelopes, and record what arrives.
  • Inject failures (503, delays, faults, broken XML) to prove the client degrades gracefully.
  • Keep scenarios in files, generate from the WSDL where possible, and periodically test against the real system.

Next: Debugging SOAP Traffic and Tools