承接 upscale/http-server-mock 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

upscale/http-server-mock

Composer 安装命令:

composer create-project upscale/http-server-mock

包简介

HTTP server that responds to recognized requests with static body and headers

README 文档

README

This project implements an HTTP server that responds to recognized requests with static body and headers. Virtually any part of a request, including headers, can be configured to influence a response being returned. Mapping between requests and responses is declared in a JSON configuration file.

While being a general-purpose server, the project is particularly geared towards REST APIs. It allows to stand up a fully operational fake API server for prototyping purposes in a matter of minutes.

Features:

  • Match requests by method, URL, headers, body
  • Respond with static headers and body
  • Request/response body in external files
  • JSON configuration file

Installation

Install the project to a directory of your choice via Composer:

composer create-project --no-dev upscale/http-server-mock /tmp/http-server-mock

Usage

The easiest way to launch the server is via the PHP's built-in web server:

php -S 127.0.0.1:8080 /tmp/http-server-mock/server.php

Open http://127.0.0.1:8080 in a browser to confirm the server is running.

Alternatively, use the HTTP server Apache 2.x with mod_rewrite installed. The project comes pre-configured to be deployed anywhere under the Document Root to start serving incoming requests.

Configuration

Configuration defines rules of matching responses to requests by their properties, in the JSON format.

The server looks for the configuration file in the following locations:

  1. Filename in environment variable HTTP_SERVER_MOCK_CONFIG_FILE
  2. File config.json in the project root directory
  3. File config.json.dist provided out of the box

Format

The JSON configuration file has the following format:

[
    {
        "request": {
            "method": "POST",
            "path": "/blog/posts",
            "params": {},
            "headers": {"Accept": "application/json", "Content-Type": "application/json"},
            "cookies": {},
            "body": "{\"title\":\"Hello World!\",\"body\":\"This is my first post!\"}"
        },
        "response": {
            "status": 201,
            "reason": "Created",
            "headers": {"Content-Type": "application/json"},
            "body": "{\"id\":\"1\",\"title\":\"Hello World!\",\"body\":\"This is my first post!\"}"
        }
    }
]

Majority of the properties are scalar. Properties params, headers, cookies are key-value pairs.

Virtually all of the properties are optional and can be omitted to not participate in the matching.

External Sources

In the configuration example above both request and response bodies are embedded into the configuration file. While useful in some cases, it may bloat the file and create additional formatting challenges, such as escaping.

The configuration supports references to external sources, for instance:

{
    "body": "file://%base_dir%/data/blog/posts/1/response.json"
}

The placeholder %base_dir% represents an absolute path to the server installation directory.

Request Format

The server takes into account format of a request body when matching requests. Human-readable formats allow reasonable degree of freedom in the syntax to improve contents readability. Variations in things like whitespaces and indentation are typically permitted by most formats. Request body will be matched successfully regardless of the syntax variation in use.

The format is determined automatically from the request header Content-Type. Mapping of common MIME types to supported formats is declared in mime-types.php. Should the automatic detection not suffice, the format can be enforced in the configuration.

The configuration syntax to enforce the body format:

{
    "request": {
        "format": "json"
    }
}

Supported formats: binary, text, xml, html, json.

The format binary is assumed by default.

Response Delay

Time needed for a real server to compute a response varies dramatically from one request to another. For testing purposes it may be desired to emulate realistic response times for heavyweight operations.

The configuration allows to specify a fixed response delay, for instance:

{
    "response": {
        "delay": 2000
    }
}

The delay duration is in milliseconds (1 second = 1000 milliseconds).

Algorithm

For each incoming request the server reads the rules from the configuration file and evaluates them. Rules are being evaluated against the request one by one in a declared order until the first match is found. Once matched successfully, a response from the matched rule is returned and further processing stops.

Rule evaluation is done by checking an incoming request for presence of declared request properties. In order to match, a request needs to contain matching values for all the declared properties. For key-value properties, presence of all declared pairs is necessary. Additional data not mentioned in the configuration is permitted.

Note: Rules with specific properties need to precede generic ones sharing the same subset of properties. Otherwise, a generic rule will match before evaluation of a specific rule occurs, effectively suppressing the latter.

Contributing

Pull Requests with fixes and improvements are welcome!

License

Copyright © Upscale Software. All rights reserved.

Licensed under the Apache License, Version 2.0.

upscale/http-server-mock 适用场景与选型建议

upscale/http-server-mock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.75k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 04 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「http」 「rest」 「api」 「restful」 「mock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 upscale/http-server-mock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 upscale/http-server-mock 我们能提供哪些服务?
定制开发 / 二次开发

基于 upscale/http-server-mock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2016-04-26