Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Moco Framework for HTTP Service Mocking

Tech 2

Setup and Execution

Download the Moco runner from Maven Central. Execute the standalone JAR with the desired configuration.

java -jar moco-runner-1.3.0-standalone.jar http -p 9090 -c api-mock.json

Basic Configuration Structure

A Moco configuration file is a JSON array defining request-response pairs.

[
  {
    "description": "Basic Moco server configuration",
    "request": {
      "uri": "/greet"
    },
    "response": {
      "text": "Moco server is operational"
    }
  }
]

Simulating a Simple GET Request

Define a handler for a GET request without parameters.

[
  {
    "description": "Simulate a simple GET request",
    "request": {
      "uri": "/fetch-data",
      "method": "get"
    },
    "response": {
      "text": "Data retrieval successful"
    }
  }
]

Simulating a GET Request with Query Parameters

Handle a GET request thatt includes query string parameters.

[
  {
    "description": "Simulate a GET request with query parameters",
    "request": {
      "uri": "/search",
      "method": "get",
      "queries": {
        "category": "books",
        "limit": "10"
      }
    },
    "response": {
      "text": "Search results for books"
    }
  }
]

Simulating a POST Request Without Body

Define a handler for a POST request with no payload.

[
  {
    "description": "Simulate a POST request without body",
    "request": {
      "uri": "/submit-form",
      "method": "post"
    },
    "response": {
      "text": "Form submitted"
    }
  }
]

Simulating a POST Request with Form Data

Handle a POST request containing form-urlencoded data.

[
  {
    "description": "Simulate a POST request with form data",
    "request": {
      "uri": "/register-user",
      "method": "post",
      "forms": {
        "email": "user@example.com",
        "password": "securepass"
      }
    },
    "response": {
      "text": "User registration successful"
    }
  }
]

Handling GET Requests with Cookies

Define a handler for a GET request that requires a specific cookie.

[
  {
    "description": "Simulate a GET request with a cookie",
    "request": {
      "uri": "/access-profile",
      "method": "get",
      "cookies": {
        "sessionToken": "xyz789"
      }
    },
    "response": {
      "text": "Profile accessed"
    }
  }
]

Handling POST Requests with Coookies and JSON Payload

Define a hendler for a POST request that includes both a cookie and a JSON body.

[
  {
    "description": "Simulate a POST request with cookie and JSON payload",
    "request": {
      "uri": "/place-order",
      "method": "post",
      "cookies": {
        "authToken": "auth-12345"
      },
      "json": {
        "itemId": "SKU1001",
        "count": 2
      }
    },
    "response": {
      "status": 201,
      "json": {
        "orderRef": "ORD98765",
        "message": "Order placed"
      }
    }
  }
]

Handling Requests with Custom Headers

Define a handler for a request that requires specific headers.

[
  {
    "description": "Simulate a POST request with custom header and JSON body",
    "request": {
      "uri": "/update-settings",
      "method": "post",
      "headers": {
        "X-API-KEY": "key-5678"
      },
      "json": {
        "theme": "dark",
        "notifications": true
      }
    },
    "response": {
      "status": 200,
      "json": {
        "result": "Settings updated"
      }
    }
  }
]

Configuring a Redirect Response

Set up a handler to redirect encoming requests too another URL.

[
  {
    "description": "Redirect to an external website",
    "request": {
      "uri": "/go-to-docs"
    },
    "redirectTo": "https://docs.example.com"
  }
]
Tags: Javatesting

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.