1.3.2 • Published 10 months ago

rio-ts-sdk v1.3.2

Weekly downloads
-
License
Apache License 2....
Repository
github
Last release
10 months ago

Typescript SDK for Rio HTTP mocking

This is TypeScript SDK for Rio HTTP/gRPC mock

Prerequisites

  • NodeJS v18+
  • Rio Mmck server v1.2.3+: See example to how to deploy locally

How to use

npm install rio-ts-sdk

Avaliable features/syntax are documented in Rio

Examples

import { Rule, JSONPathRule, JSONResponse, Stub } from 'rio-ts-sdk';

const mockServer = new Server('http://localhost:8896');

it('checkout API', () => {
    const validCardNumber = uuidv4();
    const expectedId = uuidv4();

    // Submit a stub to simulate 3rd party response
    // The mock server will match method, url and body (cardNumber and amount)
    // If these are matched with incoming requests, then mock server responds JSON data
    await new Stub('POST', Rule.contains('/pay'))
        .withDescription('stub for mocking pay API')
        .withRequestBody(
            JSONPathRule('$.cardNumber', Rule.equalsTo(validCardNumber)),
            JSONPathRule('$.amount', Rule.equalsTo(30000))
        )
        .willReturn(JSONResponse({id: expectedId, amount: 30000}))
        .send(mockServer);
    
    // Your test case
    const params = { cardNumber: validCardNumber, amount: 30000}
    const { data, status } = await axios.post('https://your-server.com/checkout', params);
    expect(status).toBe(200)
    expect(data.response.id).toBe(expectedId)
});

See example for end to end example

Dynamic Response

    // The response can be built programmatically based on the request 
    // - .Request.XXX (Cookies, Header, ..)
    // - .JSONBody.<json_field_name>.<json_field_name_child>
    const res = JSONResponse({});
    res.template = new Template();
    res.template.script = `
    status_code: 201

    cookies:
        {{ range $cookie := .Request.Cookies }}
        - name: {{ $cookie.Name }}
          value: {{ $cookie.Value }}
        {{end}}

    headers:
        X-REQUEST-ID: {{ .Request.Header.Get "X-REQUEST-ID"}}

    body: >
        {
            "encrypted_value": "{{ encryptAES "e09b3cc3b4943e2558d1882c9ef999eb" .JSONBody.naked_value}}"
        }
    `;

    await new Stub('POST', Rule.contains('/any_path'))
      .withDescription('dynamic response')
      .willReturn(res)
      .send(mockServer);

See example for end to end example of dynamic response

1.3.2

10 months ago

1.3.1

10 months ago

1.3.0

10 months ago

1.2.0

10 months ago