Skip to content

Latest commit

 

History

History
114 lines (91 loc) · 4.29 KB

File metadata and controls

114 lines (91 loc) · 4.29 KB

HttpBin

Hello, World!

def test_hello_world():
    query_string = {'greeting': 'Hello, World!'}
    resp = requests.get('http://httpbin/get', params=query_string)
    resp.raise_for_status() # best practice

    assert resp.json()['args']['greeting'] == 'Hello, World!'

新手上路?? {: #getting-started }

  • httpbin.org 首頁是用 Flasgger 產生的 API 文件,區分為 HTTP Methods、Auth、Status codes、Response formats、Cookies、Images、Redirects 及 Anything,可以分開學習。

Request Inspection ??

Anything

/anything 因為 "returns anything passed in request data" 的特定,又支援不同的 HTTP method,可以用來檢查往 server 端送出的資料是否正確,但無法安排特定的 response,因為型態固定是 application/json,結構也是固定的。例如:

$ curl https://httpbin.org/anything?key1=value1 --data key2=value2
{
  "args": {        <-- 收到的資料
    "key1": "value1"
  },
  "data": "",
  "files": {},
  "form": {
    "key2": "value2"
  },
  "headers": {     <-- 收到的 header
    "Accept": "*/*",
    "Connection": "close",
    "Content-Length": "11",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "POST",
  "origin": "12.34.56.78",
  "url": "https://httpbin.org/anything?key1=value1"
}

參考資料:

  • Anything - httpbin.org
    • Returns anything that is passed to request 看似 Request inspection 是它的簡化版?
    • 支援 DELETE、GET、PATCH、POST、PUT,有 /anything/anything/{anything} 兩種 endpoint? 只要以 /anything 開頭即可。
  • Enable request logging · Issue #421 · requests/httpbin
    • drichards-levtech: 想看 server side 收到什麼?
    • sigmavirus24: (member) /anything already returns the data as it is seen by HTTPbin => 這一點可以用來確認收到什麼!!

安裝設置 {: #setup }

Docker

$ docker run --rm -d --name httpbin kennethreitz/httpbin
$ docker run --rm -it --link httpbin alpine
/ # apk add --no-cache curl
/ # curl -i http://httpbin/get
HTTP/1.1 200 OK
Server: gunicorn/19.9.0
Date: Fri, 28 Sep 2018 13:37:49 GMT
Connection: keep-alive
Content-Type: application/json
Content-Length: 175
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin",
    "User-Agent": "curl/7.61.1"
  },
  "origin": "172.17.0.5",
  "url": "http://httpbin/get"
}

參考資料:

  • httpbin.org 一開始就講 Run locally: $ docker run -p 80:80 kennethreitz/httpbin
  • kennethreitz/httpbin - Docker Hub 這裡什麼都沒講
    • Dockerfileubuntu:18.04 為基礎,執行用 CMD ["gunicorn", "-b", "0.0.0.0:80", "httpbin:app", "-k", "gevent"]
    • Tags 只有 latest XD

參考資料 {: #reference }

社群: