Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 36 ยท Testing and Tools

Testing SOAP Services with node:test

Learn a layered strategy for testing SOAP services - unit tests for handlers and XML builders, integration tests over HTTP, fault and security cases - with runnable node:test examples.

A SOAP service is a promise to other people's software: "send me this XML and I will answer with that XML". Promises need tests, and SOAP has a few traps that ordinary function tests do not catch: namespaces that are wrong by one character, a fault that comes back as HTTP 200, a response element in the wrong order that a strict client rejects.

In this lesson you will build a small test suite using Node's built-in test runner (node:test) and node:assert, organized in layers from the cheapest tests to the most realistic.

The testing pyramid for SOAP

  1. Unit tests on the business logic (a plain function; no XML at all).
  2. Message tests on the code that builds and reads XML: given this input, is the envelope exactly right?
  3. Integration tests that start the service on a loopback port and call it over HTTP, including faults and malformed input.
  4. Contract tests that check the service against its WSDL/XSD (see Contract Validation).
  5. Consumer or partner tests in a staging environment, run less often.

The lower layers run in milliseconds and should be most numerous.

How tests run here

On this site every example is one program, so the code below writes the service and the test file into a temporary folder and starts the test runner with run(). A tiny reporter prints one line per test. On your own machine you would keep these as two files and run node --test.

Node.js Unit and message tests

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.

Look at the rounding test: 10 EUR to GBP is 10 / 0.9 * 0.8 = 8.888..., so rounding to cents gives 8.89. Tests like this pin down behavior that would otherwise drift. (The test files are written from template literals, which is why a few regular expressions carry doubled backslashes; in your own project they would be ordinary files.)

The esc test deserves attention. Escaping bugs are the most common source of malformed SOAP responses, and the failure shows up only when real data contains & or <. Always test with such characters.

Integration tests over HTTP

Unit tests cannot detect a wrong Content-Type or a fault returned with status 200. For that, start the real server on port 0 (the operating system picks a free port) and call it.

Node.js Integration test with a fault case

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 last test is deliberately uncomfortable: it passes, but only because it records an existing weakness (the service accepted a crafted value and ignored the rest). Writing such tests next to the happy path is how you discover that a validator is missing. After you fix the service to reject non-numeric ids with a stricter pattern, the same test would assert a Client fault instead.

What else to test

  • Faults per operation: every fault the WSDL declares should have a test that triggers it.
  • Headers: missing or invalid security headers must produce the right fault, not a crash.
  • Large and malformed input: empty body, huge body, invalid XML, DTD present, wrong namespace.
  • Time-outs and retries: a slow dependency should produce a controlled fault.
  • Backward compatibility: keep old sample requests as fixture files and replay them after every change.

Store sample requests and expected responses in files next to the tests. They double as documentation, and partners can reuse them in their own tools.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why start a test server on port 0?

    Binding to port 0 picks an unused port; read it back from server.address().port.
  2. 2. Which problem can an integration test catch that a unit test of the handler cannot?

    Status codes and headers are set by the HTTP layer, which only an HTTP-level test exercises.
  3. 3. What is the purpose of testing with characters like < and & in values?

    Unescaped markup characters break responses only when real data contains them.
  4. 4. Which layer should have the MOST tests?

    Fast, cheap unit and message tests give quick feedback; slower layers confirm the whole.

Summary

  • Test SOAP in layers: logic, message building, HTTP integration, contract, then partner tests.
  • Use node:test and node:assert/strict; start servers on port 0 and read the port back.
  • Test faults, headers, malformed input and special characters, not only the happy path.
  • Keep sample messages as fixtures that you replay after every change.

Next: Mock SOAP Services and Fault Injection