承接 drewlabs/htr 相关项目开发

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

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

drewlabs/htr

Composer 安装命令:

composer require drewlabs/htr

包简介

HTTP Web Service Test runner implementation using PHP Programming language and configuration files written in YAML or JSON format

README 文档

README

HTr is a PHP based utility library that allow application (espacially HTTP REST services) developper to easily test their RESTful service using a a single configuration file writtern in YAML or JSON.

  • Why YAML or JSON In modern web standard, JSON and YAML entity definition languages have become the defacto standard for modeling configuration that are easily portable between programming tools and languages. Therefore instead of creating a new language, we leverage existing ones that are known amoung developper community.

Installation

The library is a PHP based library therefore a PHP binary is required along side the powerful PHP libraries package manager composer.

To install the library simply run the command below:

> composer require drewlabs/htr

Usage

Configuration

HTr client application works with configuration files written in either JSON (Javascript Object Notation) format or YAML format. Below is a sample configuration file:

version: 0.1.0
# $schema: http://json-schema.org/id/<SCHEMA_ID>
name: My Test API

# Environment definitions
# Note: Environemnts are used by the runner to customize requests
env:
  _host: http://127.0.0.1:12300
  _apiVersion: "api"
  _postId: 2
  _commentId: 2

components:
  # The part below defines a request group or directory
  - name: "posts"
    description: Defines post management REST interfaces"
    items:
      - url: "[_host]/[_apiVersion]/posts"
        method: "GET"
        authorization:
          name: "bearer"
          value: "[_bearerToken]"
        body:
        params:
          page: 1
          per_page: 50
        tests:
          - "[status] eq 200" # Asser that request response status code == 200
      - url: "[_host]/[_apiVersion]/post"
        method: "POST"
        authorization:
          name: "bearer"
          value: "[_bearerToken]"
        body:
          title: "Environments"
          content: "This is an environment post"
        tests:
          - "[body].title eq Environment" #  Assert that request response body is has title field == Environments
          - "[status] eq 200" # Assert that request must be completed with status code 200
      - url: "[_host]/[_apiVersion]/post/:[_postId]"
        method: "PUT"
        authorization:
          name: "bearer"
          value: "[_bearerToken]"
        body:
        # Pass request body
        tests:
          - "[status] eq 422" # Assert that request response status code is 422

  # Comments requests directory
  - name: "comments"
    description: "Defines comments management REST interfaces"
    items:
      # Here we difines a request configuration that sends a POST request to http://127.0.0.1:12300/api/comments
      - url: "[_host]/[_apiVersion]/posts"
        method: "GET"
        authorization:
          name: "bearer"
          value: "[_bearerToken]"
        # body:
        params:
          page: 1
          per_page: 50
        tests:
          - "[status] eq 200" # Assert that request response status code == 200
      - url: "[_host]/[_apiVersion]/comments"
        method: "POST"
        authorization:
          name: "bearer"
          value: "[_bearerToken]"
        body:
          post_id: "[_postId]"
          content: "My Comment"
        tests:
          - "[status] eq 200" # Assert that request must be completed with status code 200
      - url: "[_host]/[_apiVersion]/post/:[_commentId]"
        method: "PUT"
        authorization:
          name: "bearer"
          value: "[_bearerToken]"
        body:
        # Pass request body
        tests:
          - "[status] eq 422" # Assert that request response status code is 422

Note Based on the configuration above requests can be customized using environment variables. Environment variables must be enclosed in [variable].

Testing

Test assertion use a natural language to make it easy to write test. Below is the syntax to write test:

  • left op right -> For simple assertions
  • left op righ and condition_2_left and condition_2_right -> For composed assertions

Suported operator for testing are:

  • Logical operators
    • and : For joining 2 or more conditions and return true only if all conditions return true
    • or : For joining 2 o more condition and return true if one of the condittions is true
  • Comparison operations
    • lt or > : Stand for value less than
    • lte or <= : For less than or equals to comparison
    • gt or > : For greater than comparison
    • gte or >= : For greater than or equals to comparison
    • eq or ne : Respectively equals or not equals
    • has : Check if an array has a given key or attribute
    • in : Checks if a value exists in a list of value. Ex: mango in banana,orange,pinneaple

We use [] to denote properties of the response object:

  • [status] denote response status code
  • [body] denote response body
  • [headers] denote response headers.

To Access properties of response body, we use [body] concatenated with the property name using . symbol as follow;

  • [body].title -> To access the title property of the response body
  • [body].address.email -> Can be used for instance to access inner property of responser body object

Command Line

The library provides a command line interface for testing your HTTP REST Service based on a configuration file. Below is the command to run a basic test command using a configuration file located in the root of your project:

> ./vendor/bin/htr test --input=$(pwd)/htr.yml

The command above assune you have a file named htr.yml in the root of your project that contains a valid HTr test configuration.

  • Debug By default the cli script will run tests in silent mode and write output to a uniquely generated file at the root of your project. To run the HTr cli in debug mode, execute the command below:

    > ./vendor/bin/htr test --input=$(pwd)/htr.yml --verbose

    Running the client application in debug mode allow developper ro see live running request with each request parameters output to the terminal.

  • Command output To override the output file name path were command logs are written simple use the --output flag as below:

    > ./vendor/bin/htr test --input=$(pwd)/htr.yml --output="$(pwd)/htr.log"
  • Request filtering

    Sometimes developper might want to execute specific requests or specific request directory components. For such cases, HTr provides developpers with --request or --req for filtering by request name or id.

    > ./vendor/bin/htr test --input=$(pwd)/htr.yml -req="request-name"
    // For running single request
    // or
    > ../vendor/bin/htr test --input=$(pwd)/htr.yml -req="request1" --req="request2"
    // For running multiple request matching the provided name

    Similary, to execute specific request directories, you can use -d or -directory flag. The HTr client will only execute tests for requests in specified directory

>./vendor/bin/htr test --input=$(pwd)/htr.yml -d="directory name"
// For running tests in a single directory
// or
> ./vendor/bin/htr test --input=$(pwd)/htr.yml -d="directory1" -d="directory2"

For running tests in multiple directories
  • Overriding environment variables

    When running tests using HTr client, --input argument usually contains an env field that is that are applied when running tests. To override the environment variable with command line variables, simple use --env switch as follow:

    ./vendor/bin/htr test --env=MyVar:MyVarValue --input=$(pwd)/htr.yml

  • Json By default the HTr client support YAML based configuration files. In order to use JSON configuration file instead, simply use the --json flag as follow:

  > ./vendor/bin/htr test --input=$(pwd)/htr.json --json

drewlabs/htr 适用场景与选型建议

drewlabs/htr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 105 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 drewlabs/htr 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-22