web接口測試之GET與POST請求


  關於HTTP協議,我考慮了一下覺得沒必要再花一節內容來介紹,因為網上關於HTTP協議的介紹非常詳細。本着以盡量避免介紹一空洞了概念與理論來介紹接口測試,我這里仍然會給出具體實例。

  在此之前先簡單的介紹一下基本概念:我們想要打開一個網站,首先是需要往瀏覽器的地址的URL輸入框架中輸入網地址。當我敲下回車后,通過HTTP協議,將網址傳送到域名解析服務器,域名解析服務器根據網址找到對應的IP主機(系統服務器)。這個過程叫request,即請求;當IP主機拿到請求后,將相應的資源返回給用戶瀏覽器。這個過程叫response,即響應。

  當用戶瀏覽器向系統服務器請求時,有幾種方法,最常用的就是GET和POST兩種方法。

  在此我們來開發這樣一個可以接收GET和POST請求的web應用。當然,這里就要求讀者具備一定的web開發基礎了。但不編程語言與web框架不是我們討論的重點。

  以flask框架的代碼為例。

 

 

GET請求                              

 

pyfl/

|---- /hello.py

|----/templates/

|----|-----------/index.html

|----|-----------/user.html

 

hello.py

from flask import Flask,render_template app = Flask(__name__) @app.route("/") def index(): return render_template("index.html") if __name__ == '__main__': app.run(debug=True)

index.html

<h1> This is index page <h1>

啟動flask容器:

 

訪問:http://127.0.0.1:5000/

通過firebug查看GET請求信息:

當然,這個返回只是一個靜態的頁面,並且不需要任何參數,我們只需要判斷返回是否為200即可。

擴充hello.py如下:

from flask import Flask,render_template app = Flask(__name__) @app.route("/") def index(): return render_template("index.html") @app.route("/user/<name>") def user(name): return render_template("user.html",name=name) if __name__ == '__main__': app.run(debug=True)

user.html

<h1> Hell, {{name}} !<h1>

訪問:http://127.0.0.1:5000/user/aaa

  相比較來說,這個GET請求就復雜了一些,在請求的時候跟了一些參數(aaa),后台(hello.py)對參數了進行了接收,並且將其反回到了user.html頁面中。

  這個時候,我們就可以對這個參數做一些簡單的測試,比較參數為空,字符,數字,腳本,sql 之類的。其實,安全測試的sql注入也是通過輸參中帶入sql語句入手的。

 

 

POST請求                             

 

pyfl/

|---- /hello.py

|----/templates/

|----|-----------/index.html

 

hello.py

from flask import Flask,render_template,request app = Flask(__name__) @app.route("/") def index(): return render_template("index.html") @app.route("/login",methods = ['GET', 'POST']) def login(): if request.method == "POST": username = request.form.get('username') password = request.form.get('password') if username=="zhangsan" and password=="123": return "<h1>welcome, %s !</h1>" %username else: return "<h1>login Failure !</h1>"    
    else: return "<h1>login Failure !</h1>"


if __name__ == '__main__': app.run(debug=True)

index.html

<form action="/login" method="post"> username: <input type="text" name="username"> password: <input type="password" name="password">
    <input type="submit" id="submit">
</form>

訪問:http://127.0.0.1:5000/

輸入用戶名,密碼登錄(后台hello.py判定,用戶名為“zhangsan”,密碼為“123”登錄成功,其它帳號失敗。

Python的有一個requests庫,可以很方便的模擬測試POST請求。

#coding=utf-8
import requests s = requests data={"username":"zhangsan","password":"123",} r = s.post('http://127.0.0.1:5000/login', data) print r.status_code print r.headers['content-type'] print r.encoding print r.text

執行結果:

200 text/html; charset=utf-8 utf-8
<h1>welcome, zhangsan !</h1>

POST接口的測試也一樣,通過不輸入為空,或錯誤的用戶名密碼,檢查返回的內容。

 

 

===================

  本文算是入門,可討論的問題還有很多,例如接口返回的是json格式的數據,例如接口為了安全加了數字簽名。從測試的角度,有哪個工作可以模擬這些請求,如何組織和運行測試用例。后面有時間再討論。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM