aksoyih/http-mock 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

aksoyih/http-mock

最新稳定版本:1.0.0

Composer 安装命令:

composer require aksoyih/http-mock

包简介

A PHP library for mocking HTTP clients with customizable responses and behaviors

README 文档

README

A PHP library for mocking HTTP clients with customizable responses and behaviors. Perfect for testing API integrations and HTTP client implementations.

Installation

composer require aksoyih/http-mock --dev

Usage

Basic Usage

use Aksoyih\HttpMock\HttpMock;

$mock = new HttpMock();

// Define a mock response
$mock->when('GET', 'https://api.example.com/users')
    ->willReturn(['id' => 1, 'name' => 'John Doe']);

// Get the mock response
$response = $mock->getMockResponse('GET', 'https://api.example.com/users');
// Returns: ['id' => 1, 'name' => 'John Doe']

Customizing Response

// Set status code and headers
$mock->when('POST', 'https://api.example.com/users')
    ->withStatus(201)
    ->withHeaders(['Content-Type' => 'application/json'])
    ->willReturn(['message' => 'User created']);

Simulating Delays

// Add a global delay to all responses
$mock->withDelay(1000); // 1 second delay

// Or reset the delay
$mock->reset();

Simulating Errors

// Simulate a global error
$mock->withGlobalError('Network connection failed');

// Or simulate specific endpoint errors
$mock->when('GET', 'https://api.example.com/error')
    ->withStatus(500)
    ->willReturn(['error' => 'Internal Server Error']);

Features

  • Mock HTTP responses for specific endpoints
  • Customize response status codes and headers
  • Simulate network delays
  • Simulate error conditions
  • Fluent interface for easy configuration
  • Reset functionality for test isolation

Requirements

  • PHP 8.1 or higher

License

MIT License

统计信息

  • 总下载量: 201
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-20