Backend Development 12 min read

Building a Mock Server with Python, Django, MySQL, Redis, and Vue

This guide explains how to create a full‑stack mock server using Python, Django, MySQL, Redis, and Vue, covering database setup, REST API implementation, Redis caching, and a Vue front‑end for managing mock data, enabling efficient automated testing and development.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Building a Mock Server with Python, Django, MySQL, Redis, and Vue

Mock servers simulate backend responses, allowing automated tests and development to run without a real server, improving efficiency, reducing cost, and fostering tighter front‑end/back‑end collaboration.

First, create a MySQL database and a mock table to store mock data:

CREATE DATABASE mockdb;
USE mockdb;
CREATE TABLE mock (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
url VARCHAR(255) NOT NULL,
method VARCHAR(10) NOT NULL,
request_body TEXT,
response_body TEXT
);

Next, implement a Django REST API to add, delete, update, and retrieve mock entries. The key files are:

# urls.py
from django.urls import path
from .views import MockList, MockDetail
urlpatterns = [
    path('mocks/', MockList.as_view()),
    path('mocks/
/', MockDetail.as_view()),
]
# views.py
from rest_framework import generics
from .models import Mock
from .serializers import MockSerializer
class MockList(generics.ListCreateAPIView):
    queryset = Mock.objects.all()
    serializer_class = MockSerializer
class MockDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Mock.objects.all()
    serializer_class = MockSerializer
# models.py
from django.db import models
class Mock(models.Model):
    url = models.CharField(max_length=255)
    method = models.CharField(max_length=10)
    request_body = models.TextField(blank=True)
    response_body = models.TextField()
    def __str__(self):
        return f"{self.method} {self.url}"
# serializers.py
from rest_framework import serializers
from .models import Mock
class MockSerializer(serializers.ModelSerializer):
    class Meta:
        model = Mock
        fields = '__all__'

To speed up lookups, a Redis‑based service caches mock responses. The service class uses Django settings for connection details and synchronizes data between MySQL and Redis:

# mock.py
import json, redis, requests
from django.conf import settings
from .models import Mock
class MockService:
    def __init__(self):
        self.r = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB)
        self.mock_api = f"http://{settings.API_HOST}/api/mocks/"
    def add_mock(self, url, method, request_body, response_body):
        mock = Mock.objects.create(url=url, method=method, request_body=request_body, response_body=response_body)
        self.r.hset('mocks', f"{method}:{url}", json.dumps({'response_body': response_body}))
        return requests.post(self.mock_api, data={'url': url, 'method': method, 'request_body': request_body, 'response_body': response_body})
    # remove_mock, update_mock, get_mock_response methods omitted for brevity

Finally, a Vue component (using Element‑UI) provides a UI for managing mock entries. It displays a table of mocks and a dialog for creating or editing records, communicating with the Django API via Axios:

By combining the Python/Django backend, MySQL storage, Redis caching, and the Vue front‑end, you obtain a complete mock service that can be used in automated tests or during development to quickly simulate API responses.

TestingRedisVueDjangoAPIMock Server
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.