Skip to content

Instantly share code, notes, and snippets.

View peterjpxie's full-sized avatar

Peter Jiping Xie peterjpxie

View GitHub Profile
@peterjpxie
peterjpxie / postman_code_snippet.py
Created July 10, 2019 06:14
postman code snippet - python
import requests
url = "http://httpbin.org/post"
payload = "{\n \"key1\": 1,\n \"key2\": \"value2\"\n}"
headers = {
'Content-Type': "application/json,text/plain",
'User-Agent': "PostmanRuntime/7.15.0",
'Accept': "*/*",
'Cache-Control': "no-cache",
@peterjpxie
peterjpxie / test_post_headers_body_json.py
Last active August 9, 2023 03:36
Python Rest API sample - POST
import requests
import json
def test_post_headers_body_json():
url = 'https://httpbin.org/post'
# Additional headers.
headers = {'Content-Type': 'application/json' }
# Body
payload = {'key1': 1, 'key2': 'value2'}
# convert dict to json string by json.dumps() for body data.
@peterjpxie
peterjpxie / REST API logging example.txt
Last active November 21, 2020 19:36
REST API logging example
# pytest -sv
test_post_headers_body_json_pprint.py::test_post_headers_body_json
-----------Request----------->
POST https://httpbin.org/post
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 39
@peterjpxie
peterjpxie / test_post_headers_body_json_pprint.py
Last active April 1, 2020 08:50
test_post_headers_body_json_pprint
import requests
import json
def pretty_print_request(request):
print( '\n{}\n{}\n\n{}\n\n{}\n'.format(
'-----------Request----------->',
request.method + ' ' + request.url,
'\n'.join('{}: {}'.format(k, v) for k, v in request.headers.items()),
request.body)
)
@peterjpxie
peterjpxie / flask_mock_simple_service.py
Last active November 14, 2019 23:11
flask_mock_simple_service
from flask import Flask
app = Flask(__name__)
@app.route('/json', methods=['POST', 'GET'])
def test_json():
return '{"code": 1, "message": "Hello, World!" }'
# Run in HTTP
app.run(host='127.0.0.1', port='5000')
@peterjpxie
peterjpxie / test_mock_service.py
Last active August 8, 2019 05:39
test_mock_service
import requests
def test_mock_service():
url = 'http://127.0.0.1:5000/json'
resp = requests.get(url)
assert resp.status_code == 200
assert resp.json()["code"] == 1
print(resp.text)
from flask import Flask
import time
app = Flask(__name__)
@app.route('/json', methods=['POST', 'GET'])
def test_json():
time.sleep(0.2) # simulate delay
return '{"code": 1, "message": "Hello, World!" }'
# Updated test function in perf_test_mock_service.py
def test_mock_service():
url = 'http://127.0.0.1:5000/json'
resp = requests.get(url)
if resp.status_code != 200:
print('Test failed with response status code %s.' % resp.status_code )
return 'fail', resp.elapsed.total_seconds()
elif resp.json()["code"] != 1:
print('Test failed with code %s != 1.' % resp.json()["code"] )
return 'fail', resp.elapsed.total_seconds()
import requests
import queue
import sys
import time
queue_results = queue.Queue()
# def test_mock_service(): - omitted
def loop_test(loop_wait=0, loop_times=sys.maxsize):
looped_times = 0
import requests
import threading
import queue
import sys
import time
# Global variables
queue_results = queue.Queue()
start_time = 0