# Loki, a Dynamic Mock Server for HTTP/TCP Testing

[Grab](https://yomu.fyi/company/grab) · Thuy Nguyen · Apr 10, 2019

**Type:** Problem & solution

## Summary

Grab built Loki, a dynamic mock server written in Golang that simulates backend services on local developer machines and CI pipelines. Mobile app testing previously suffered from heavy dependencies on complex, brittle staging environments and interconnected services communicating over HTTP, HTTPS, and TCP. Loki handles both HTTP and TCP traffic on distinct ports while exposing a unified RESTful API to manage test expectations. It provides runtime flexibility through sandboxed JavaScript execution, configurable request sequence ordering, and an in-memory cron scheduler for TCP push messages. Adopting Loki decoupled mobile releases from staging stability, improving delivery cycles and enabling automated UI testing with Espresso and XCUITest.

## Context

End-to-end mobile app testing at Grab faced significant friction due to heavy dependencies on backend services, staging downtime, data corruption, and complex multi-app business flows spanning HTTP and TCP.

## Approach / What changed

Grab developed Loki, a containerized Golang mock server with pluggable storage backends (MySQL, Redis, or in-memory cache) that supports dynamic JavaScript injection, sequenced HTTP responses, and an in-memory cron system to push scheduled TCP events.

## Takeaways

- Mocking TCP required handling long-running client-server connections, configurable request delimiters for JSON deserialization, and unsolicited push messages via an in-memory cron scheduler.
- Dynamic responses can be computed at runtime using an in-house JavaScript execution library running inside an isolated sandbox.
- The httpTimesAndOrder API enables developers to configure ordered response state transitions on the same endpoint to simulate sequential lifecycle changes like ride booking stages.

**Tags:** [CI/CD](https://yomu.fyi/topic/ci-cd), [Developer Experience](https://yomu.fyi/topic/developer-experience), [Docker](https://yomu.fyi/topic/docker), [Go](https://yomu.fyi/topic/go), [Testing](https://yomu.fyi/topic/testing)

[Read original post](https://engineering.grab.com/loki-dynamic-mock-server-http-tcp-testing)
