Skip to content

astrizhachuk/mockserver-client-1c

Repository files navigation

MockServer client for 1C:Enterprise Platform

Quality Gate Status Maintainability Rating

русский

MockServer-client-1c is designed to manage MoskServer using 1C:Enterprise Platform. This client is distributed as cfe and implemented as DataProcessor that interacts with the MockServer via the REST API. MockServer supports OpenAPI v3 specifications in either JSON or YAML format.

How it works

 Mock = DataProcessors.MockServerClient.Create();
 Mock.Server("http://localhost", "1080")
  .When(
   Mock.Request()
    .WithMetod("GET")
    .WithPath("/some/path")
    .Headers()
      .WithHeader("foo", "boo")
  ).Respond(
   Mock.Response()
    .WithStatusCode(200)
  );

That's all! Mock is created!

// @unit-test
Procedure Verify(Context) Export
  // given
  Mock = DataProcessors.MockServerClient.Create();
  Mock.Server( "http://localhost", "1080", true );
  HTTPConnector.Get( "http://localhost:1080/some/path" );
  HTTPConnector.Get( "http://localhost:1080/some/path" );
  // when
  Mock.When(
      Mock.Request()
        .WithPath("/some/path")
    ).Verify(
      Mock.Times()
        .AtLeast(2)
    );
  // then
  Assert.IsTrue(Mock.IsOk());
EndProcedure

Tested!

Code Examples

Public API

Getting Started

Overview

The typical sequence for using MockServer is as follows:

Start MockServer

Running MockServer documentation

For example, start the MockServer docker container:

docker run -d --rm -p 1080:1080 --name mockserver-1c-integration mockserver/mockserver -logLevel DEBUG -serverPort 1080

Or run docker-compose.yml from root directory of the project:

docker-compose -f "docker-compose.yml" up -d --build

Create an instance of the client

Connect to the default server:

Mock = DataProcessors.MockServerClient.Create();

Connect to the server at the specified host and port:

Mock = DataProcessors.MockServerClient.Create();
Mock = Mock.Server( "http://server" );
# or
Mock = DataProcessors.MockServerClient.Create();
Mock = Mock.Server( "http://server", "1099" );

Connect to the server at the specified host and port with a completely MockServer reset:

Mock = DataProcessors.MockServerClient.Create();
Mock = Mock.Server( "http://server", "1099", True );

Setup Expectations

Setup expectation (and verify requests) consists of two stages: preparing conditions (JSON) and sending an action (PUT JSON).

There are two types of methods: intermediate (returns self-object) and terminal (perform action). Some object's methods as parameters can accept a reference to themselves with preparing conditions or a JSON-format string. Before executing the action, the necessary JSON will be automatically generated depending on the selected terminal operation and preconditions.

Use method chaining style (fluent interface):

  # full JSON without auto-generating
  Mock.Server( "localhost", "1080" )
    .When( "{""name"":""value""}" )
    .Respond();

  # httpRequest property in JSON-style
  Mock.Server( "localhost", "1080" )
    .When(
      Mock.Request( """name"":""value""" )
    )
    .Respond();

  # combined style
  Mock.Server( "localhost", "1080" )
    .When(
      Mock.Request()
        .WithMethod( "GET" )
        .WithPath( "some/path" )
    )
    .Respond(
        Mock.Response( """statusCode"": 404" )
    );

Dependencies

The project built with:

  1. 1C:Enterprise 8.3.16.1502+ (8.3.16 compatibility mode)
  2. 1C:Enterprise Development Tools 2020.4 RC1
  3. 1Unit 0.4.0+
  4. vanessa-automation
  5. dt.bslls.validator
  6. BSL Language Server

Working with HTTP is implemented using the following libraries: