FunkLoadでJSONをPOSTする

FunkLoadJSONをPOSTする方法を調べたのでメモ。

FunkLoadでJSONを送信するテストコード

funkload.utils.Dataを使うと、Content-Typeを指定してデータ列を送信できる。

test_Simple.py
# coding: utf8
import unittest
import json
from funkload.FunkLoadTestCase import FunkLoadTestCase
from funkload.utils import Data


class Simple(FunkLoadTestCase):
    def setUp(self):
        self.server_url = 'http://localhost:5000/'

    def test_simple(self):
        """Content-Type: application/jsonでJSONをPOSTする例
        """
        server_url = self.server_url
        json_data = Data(
            'application/json',
            json.dumps({
                'spam': 123,
                'egg': [1, 2, 3]}))
        ret = self.post(
            server_url,
            json_data,
            description='Get url')
        print ret.body


if __name__ in ('main', '__main__'):
    unittest.main()

サーバー側

テスト対象のサーバー側はFlaskで送信されたJSONを返すぐらいのものを用意した。

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/', methods=['POST'])
def hello():
    print request.json
    return jsonify(result=request.json)

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

実行結果

$ python test_Simple.py
test_simple: Starting -----------------------------------
        Access 20 times the main url
test_simple: POST: http://localhost:5000/ [User data application/json]
        Page 1: Get url ...
test_simple:  Done in 0.006s
{
  "result": {
    "egg": [
      1, 
      2, 
      3
    ], 
    "spam": 123
  }
}
.
----------------------------------------------------------------------
Ran 1 test in 0.008s

OK