Guides

Mock APIs

Mock APIs

Introduction

Skyramp saves you time chasing dependencies by making it easy to mock internal and external APIs and services, giving you precise control over the dependencies you need or services you want to isolate. You can define mock responses for specific endpoints and customize individual response values in JSON payloads.

1. Initializing Endpoints

Start by defining endpoint objects, encapsulating details like name, port, and endpoint path. For instance, defining an endpoint for a REST service:

import skyramp

artists_endpoint = skyramp.RestEndpoint(
    name="my-service",
    port=443,
    rest_path="/artists"
)

2. Defining Mock Responses

Specify mock responses for each endpoint, creating response values for different methods. Example for a POST method request to /artists:

artists_POST_response = r'''{
  "id": 1,
  "name": "John Doe"
}'''

artists_POST_response_value = skyramp.ResponseValue(
    name="artists_POST",
    method_name="POST",
    endpoint_descriptor=artists_endpoint,
    blob=artists_POST_response
)

3. Applying Mock Responses

Apply mock responses to the running service in Docker or Kubernetes using Skyramp’s DockerClient or KubernetesClient objects. Example with Docker:

client = skyramp.DockerClient()
worker_address = "localhost:35142"
client.mocker_apply_v1("", worker_address, [artists_POST_response_value])

4. Overriding Response Values

Override individual response values for fields in JSON payloads. Example:

mock_object = skyramp.MockObject(
    name="negative_artists_POST_invalid_id",
    response_value=artists_POST_response_value,
    blob_override={"id": "abcdefg"}
)

client.mocker_apply_v1(k8s_namespace, dockerworker_address, mock_object)

What's Next

Explore dynamic mock generation with Skyramp’s terminal client mocker generate command. Automate mock data creation to save time and effort. Learn more about generating mocks here.

© 2024 Skyramp, Inc. All rights reserved.

© 2024 Skyramp, Inc. All rights reserved.

© 2024 Skyramp, Inc. All rights reserved.