Swifting a Mock Server (with Vapor)

The aim of this post is put up the level of previous post (Testing at the edge of your App with a Mock Server), just by using Vapor (a swift backend framework) and deploying server with Heroku (a cloud platform that lets companies build, deliver, monitor and scale apps).

Setup the environment


For installing vapor backend framework just:

$ curl -sL check.vapor.sh | bash
$ curl -sL toolbox.vapor.sh | bash
$ vapor --help
Usage: vapor <new|build|run|fetch|clean|test|xcode|version|self|heroku|docker>
Join our Slack if you have questions, need help,
or want to contribute: http://vapor.team

For creating a new vapor project, afterwards create a new git repository with the stuff generated and finally open the project:

$ vapor new mock-vapor
$ cd mock-vapor
$ git init
$ git add .
$ git commit -m "Initial commit"
$ vapor xcode

Up to this point you will have XCode with new project:


Run the project, open your favourite browser and type the url http://localhost:8080


Mock server is working on local ?. Ready for getting to next level? ?


If you do not have an account just sign up here. Later on, download Heroku command line tools, for validating installation just type (restart computer if necessary).

$ heroku --version
$ heroku login
Enter your Heroku credentials.
Email: javi.calatrava@gmail.com
Password (typing will be hidden):
Logged in as javi.calatrava@gmail.com
$ vapor heroku init
Would you like to provide a custom Heroku app name?
Custom app name:
https://mock-vapor.herokuapp.com/ | https://git.heroku.com/mock-vapor.git

Would you like to provide a custom Heroku buildpack?
Setting buildpack...
Are you using a custom Executable name?
Setting procfile...
Committing procfile...
Would you like to push to Heroku now?
This may take a while...
Building on Heroku ... ~5-10 minutes [ • ]

….finally after several minutes the mock server is deployed on a remote site, just type https://mock-vapor.herokuapp.com/ in your browser.


Implementing a dummy service

Our mock server will return .json, first thing to do is setting  content-type.  Just update View.swift extension:

extension View: ResponseRepresentable {
    public func makeResponse() -> Response {
        return Response(status: .ok, headers: [
            "Content-Type": "text/html; application/json;charset=utf-8"
        ], body: .data(data))

Validate with any rest analyzer:


Adding basic fixer answer. This is what our mock servier will answer:

drop.get("latest") { request in
    if let param = request.data["param"]?.string {
        if param == "FORCE_BUSINESS_ERROR" {
            return try drop.view.make("latest_FORCE_BUSINESS_ERROR")
        }else if param == "FORCE_SERVER_ERROR"{
            return  Response(status: .notFound)
            return try drop.view.make("latest") 
        return try drop.view.make("latest")

latest_FORCE_BUSINESS_ERROR is just a json template file.

The client App

The client app will be exactly the same presented in previous post, but obviously replacing urls.n In this case we will use three schemas:

  • Local mock server. For debugging with local mock server.
  • Remote mock server. For working with common development mock server.
  • Fixer real server. For working with final service.

Select Local mock server schema and launch the test:


Deploy the mock server

At the moment there is nothing by typing https://mock-vapor.herokuapp.com/latest in the remote server:before

For deploying just push changes:push

This process can take several minutes… ?


But finally we have the remote with latest, at this moment, just type: https://mock-vapor.herokuapp.com/latest. The remote server is ready!


Switch mock remote server

Switch oclient Mock Server Schema to lauch the tests on the client, but this time bouncing to a remote mock server.



With this post you have seen how to deploy a remote mock server. You can use this server as a test platform or you could develop your app backend. If you were interested in investigating more you can fetch source code from here.

Useful links


Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen + 8 =