Skip to content

Overview

A solution for running and configuring a local HTTP server to mimic REST API endpoints used by your iOS, Android or KMM application.

Supported Environments

  • Native iOS apps (written in Swift)
  • Native Android apps
  • Kotlin Multiplatform Apps (Android & iOS targets only).

Quick Start

Important

Mockzilla does not support HTTPS, all traffic is cleartext HTTP.

Warning

Mockzilla is not a production tool. It is for development and testing only. It should never be deployed to production. Running a server on device may introduce un-foreseen security issues.

Installation

Add the gradle dependency:

implementation("com.apadmi:mockzilla:2.0.1")

Add the SPM dependency in XCode:

  1. File > Swift Packages > Add Package Dependency
  2. Add https://github.com/Apadmi-Engineering/SwiftMockzilla.git

Note

Note: This is not for KMM projects (for those, the gradle dependecy should be added to shared source set). This SPM dependency is for purely native iOS apps only.

Either install the package using:

flutter pub add mockzilla

Or add the dependency in your pubspec.yaml file directly:

mockzilla: <version>

Starting The Server

Mockzilla is entirely driven by a config object which is used to start the server.

(1): Create the config:

val config = MockzillaConfig.Builder()
    .addEndpoint(
        EndpointConfiguration
            .Builder("Hello World")
            .setDefaultHandler {
                MockzillaHttpResponse(body = "Hello World")
            })
    .build()
let config = MockzillaConfigBuilder()
    .addEndpoint(endpoint: EndpointConfigurationBuilder(id: "Hello world")
        .setDefaultHandler { _ in
            MockzillaHttpResponse(body: "Hello world")
        }.build()
    ).build()
final mockzillaConfig = MockzillaConfig().addEndpoint(
    () => EndpointConfig(
        name: "Hello world",
        endpointMatcher: (request) => request.uri.endsWith("/hello-world"),
        defaultHandler: (request) => const MockzillaHttpResponse(
            body: "Hello world",
        ),
        errorHandler: (request) => const MockzillaHttpResponse(
            statusCode: 418,
        ),
    ),
);

See here for more information on configuring your endpoints. (Including compile-time safety!)

(2): Just start the server!

Note

For KMM apps, even though all the configuration can be defined in Kotlin. The server should still be started directly from within native code on both platforms. See the KMM demo for an example.

class RootApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        val config = MockzillaConfig.Builder()....
        startMockzilla(config, this)
    }
}
import UIKit
import SwiftMockzilla
import mockzilla

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_: UIApplication,
                     didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
    {
        let config = MockillaConfig.Builder()...
        startMockzilla(config: config)

        return true
    }
}
    // Make sure to call this before starting Mockzilla!
    WidgetsFlutterBinding.ensureInitialized();

    await Mockzilla.startMockzilla(mockzillaConfig);

(3): Call the server from your client code

Mockzilla listens for calls to http://localhost:8080/local-mock (this should be your base url).

To configure the port see here.

Recommendation

Since Mockzilla shouldn't be included in production binaries, we recommend creating a new product flavour specifically for the mock and only including this dependency for this variant.

For KMM projects this will require creating a new KMM module in your project specifically for the mock.

Flutter itself doesn't support product flavours in the same way as Android or iOS, however we can use different Dart entrypoints into your application and Dart's tree-shaking to achieve a similar effect.

  1. Before integrating Mockzilla, duplicate your main.dart file and rename it to create main_mock.dart.

    |-lib
      |-main.dart
      |-main_mock.dart
    
  2. In main_mock.dart, follow the instructions above to configure and start the Mockzilla server and perform any additional configuration on your HTTP client to use the localhost base URL above.

  3. We can now use the newly created main_mock.dart as a different entrypoint to your Flutter app to enable Mockzilla while leaving the standard main.dart as your production entrypoint!

    flutter run -t lib/main_mock.dart
    
  4. Optionally, feel free to move the Mockzilla config to an auxiliary file. Just make sure that the declarations aren't used in your production app.

Tips

Ensure your development machine and test device are on the same wifi network. You can replace localhost with your device's IP addresss and try calling these endpoints from Postman (or a similar REST client.)

alt text

Back to top