Skip to content

theaussiepom/soap-service

Repository files navigation

Soap Service

Abstracts the connection to and testing of soap services.

Folders

  • /.vscode/ - Configuration for Visual Studio Code

  • /build/ - Build scripts and other assets needed for building.

  • /dist/ - Distributable files - static and generated by the build process.

  • /doc/ - Documentation; technical details and the output of any generated documentation

  • /samples/ - Samples that demonstrate the use of the package

  • /spec/ - Specifications; feature files, specialised specs such as load testing. Also any scripts needed for executing the specs.

  • /src/ - The source code, including unit tests

  • /temp/ - Temporary files that are discardable. (Not checked in to source control)

  • /tools/ - Executable binaries and other scripts

  • /vendor/ - Third-party executable binaries and other scripts

Files

  • /.editorconfig - Standardised IDE configuration using EditorConfig

  • /.gitignore - Filename patterns for Git to ignore

  • /bitbucket-pipelines.yml - Configuration for BitBucket pipelines CI

  • /CHANGELOG.md - Log of significant changes per release

  • /mocha.opts - Configuration for the Mocha test runner

  • /package.json - NPM package configuration

  • /README.md - Entry point to the repository’s documentation (this file)

  • /tslint.json - Typescript Lint configuration

Getting set up

npm install

Example usage:

Define the Request and Result objects (the members are case sensitive):

export interface GetWeather {
  country: string;
}

export interface GetWeatherResult {
  isSunny: boolean;
}

Define the Service Client:

export interface ServiceClient extends SoapClient {
  GetWeather: SoapMethod<GetWeather, GetWeatherResult>;
}

Define the connection parameters:

const config: ConnectionConfig = {
  credentials: {
    password: "password123",
    user: "weatherman",
  },
  serviceUrl: "www.weatherforecast.com/api/weather",
  wsdlUrl: "www.weatherforecast.com/api/weather?wsdl",
};

Call the service operation

Connections are transient and live only for the life of the call to the Soap Service.

const result = await execute<ServiceClient, GetWeather, GetWeatherResult>(
    config, (c: ServiceClient) => {
      return c.GetWeather;
    }, {
      country: "Australia",
    });

console.log(`Australia is sunny: ${result.isSunny}`);

Test using Mock Service

// Calling server.initialise before all tests runs sets up the test Mock Server:
//   - The server will be refreshed before each test, where the supplied resetService function is called
//   - The server will be stopped upon completion of all tests

// Create stub to represent fake call (see fakeGetWeather definition below)
export const getWeather: SinonStub = stub();

const server: MockServer = new MockServer({
  resetService: reset,
  serviceDefinition: {
    Weather: {
      WeatherSoap: {
        GetWeather: getWeather,
      },
    },
  },
  serviceUrlPath: "/api/weather", // URL path to the service
  wsdl: readFileSync(join(__dirname, "service.wsdl"), "utf8"), // Supply a copy of the wsdl
  wsdlUrlPath: "/api/weather", // URL path to the WSDL (this can be different to the serviceUrlPath)
});

BeforeAll(() => server.initialise());

export function reset() {
  getWeather.reset();
  getWeather.callsFake(fakeGetWeather);
}

function fakeGetWeather(): { GetWeatherResult: boolean } {
  return { GetWeatherResult: true };
}

Getting current status of Mock Service

// Call getStatus on MockServer object
const status: MockServerStatus = server.getStatus();

/*
MockServerStatus {
  config: {
    credentials: {
    password: string;
    user: string;
  },
  serviceUrl: string;
  wsdlUrl: string;
  };
  isAvailable: boolean;
}
*/

Suspending the server during a test

// Call suspendService on the MockServer object, specifying the temporary timeout for the client (milliseconds)
server.suspendService(100);

Further documentation