go-httpbin:HTTP 请求测试工具推荐

语言: CN / TW / HK

httpbin 是什么

A simple HTTP Request & Response Service (written in Python + Flask).

一个简单的 HTTP 请求和回复测试服务。

详见: https://httpbin.org/

httpbin 用途

http 开发调试

httpbin 提供了 GET/PUT/POST/PATCH/DELETET 常见方法,可以把请求头、参数等返回,类似 Echo 服务。方便调试 http 请求。

http 库测试

使用开源或者自己项目封装的 http 请求库,可以把 httpbin 作为服务端,测试各种场景。

除了在线的 https://httpbin.org/,也可以本地部署。

云原生示例服务

可作为演示服务,测试平台功能。例如,isito 就有 httpbin sample 服务,用来实验 istio 各种特性

详见: https://github.com/istio/istio/tree/master/samples/httpbin

httpbin 常用接口

/get 接口

/put, /post, /patch 还会打印出请求体数据

curl 'https://httpbin.org/get?paramA=aa'
{
  "args": {
    "paramA": "aa"
  },
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.64.1",
    "X-Amzn-Trace-Id": "Root=1-6107dcac-1d6d3fb32f67d8184c184992"
  },
  "origin": "111.207.117.50",
  "url": "https://httpbin.org/get?paramA=aa"
}

/status/{codes} 接口

用来返回特定的状态码

curl 'https://httpbin.org/status/418'

    -=[ teapot ]=-

       _...._
     .'  _ _ `.
    | ."` ^ `". _,
    \_;`"---"`|//
      |       ;/
      \_     _/
        `"""`

/ip 接口

返回原始 ip

curl 'https://httpbin.org/ip'
{
  "origin": "xxx.xxx.xxx.xxx"
}

/delay/{delay} 接口

延时一定时间返回

还有很多测试接口,可在官网查看

go-httpbin 优化

httpbin 的 github https://github.com/postmanlabs/httpbin 在 2018.11 后就没有更新了,有一些需求和 bug 都没有解决。

无意将发现了 go-httpbin, 是一个只使用 golang 标准库实现的 httpbin。

所以我 fork 了 mccutchen/go-httpbin 项目,并添加了一些特性。

  • add -response-delay args (more observability, except /delay/{time} api)

  • pretty response json (more like httpbin)

  • add query param envs (identify httpbin instance, default show HOSTNAME for k8s. eg: curl http://localhost:8080/get?envs=ENV1,ENV2)

  • add anything api (show anything request and response)

详见: https://github.com/chinaran/go-httpbin

可以直接使用 docker 尝试运行

docker run -p 8080:80 ghcr.io/chinaran/go-httpbin:1.2-alpine3.13

示例:(请求注入了 sidecar 的 httpbin 服务)

kubectl -n ran-test exec -it client-54d574b9b-6w9xc -- curl http://go-httpbin/get
{
    "envs": {
        "HOSTNAME": "go-httpbin-6f55bbcfcd-skrxl"
    },
    "args": {},
    "headers": {
        "Accept": "*/*",
        "Content-Length": "0",
        "Host": "go-httpbin",
        "User-Agent": "curl/7.76.1",
        "X-B3-Parentspanid": "d4ae8aad7301da2d",
        "X-B3-Sampled": "1",
        "X-B3-Spanid": "68b5055ffd105f5b",
        "X-B3-Traceid": "26e3ee5a3196b1bcd4ae8aad7301da2d",
        "X-Client-Name": "client",
        "X-Client-Namespace": "ran-test",
        "X-Envoy-Attempt-Count": "1",
        "X-Forwarded-Client-Cert": "By=spiffe://cluster.local/ns/ran-test/sa/default;Hash=0cb9c538a64d9f747c86731252bd28d6e9121c7cf0be731f2484fd3b4a1af57f;Subject=\"\";URI=spiffe://cluster.local/ns/ran-test/sa/default",
        "X-Forwarded-Proto": "http",
        "X-Request-Id": "1f2a3700-1bdf-9a0a-9091-2c6aba324606"
    },
    "origin": "127.0.0.1:46756",
    "url": "http://go-httpbin/get"
}