define httpbin host and port in one place

This commit is contained in:
debugtalk
2018-08-02 11:29:01 +08:00
parent 365116b205
commit f071c02513
11 changed files with 150 additions and 33 deletions

View File

@@ -94,4 +94,4 @@
- ${teardown_hook_sleep_N_secs($response, $n_secs)}
validate:
- eq: ["status_code", 200]
- eq: [content.headers.Host, "127.0.0.1:3458"]
- contained_by: [content.headers.Host, $HTTPBIN_SERVER]

View File

@@ -3,18 +3,27 @@ import time
import unittest
import requests
from httpbin import app as httpbin_app
from httprunner import utils
from tests.api_server import app as flask_app
try:
from httpbin import app as httpbin_app
HTTPBIN_HOST = "127.0.0.1"
HTTPBIN_PORT = 3458
except ImportError:
HTTPBIN_HOST = "httpbin.org"
HTTPBIN_PORT = 80
FLASK_APP_PORT = 5000
HTTPBIN_APP_PORT = 3458
HTTPBIN_SERVER = "http://{}:{}".format(HTTPBIN_HOST, HTTPBIN_PORT)
def run_flask():
flask_app.run(port=FLASK_APP_PORT)
def run_httpbin():
httpbin_app.run(port=HTTPBIN_APP_PORT)
if HTTPBIN_HOST == "127.0.0.1":
httpbin_app.run(host=HTTPBIN_HOST, port=HTTPBIN_PORT)
class ApiServerUnittest(unittest.TestCase):

View File

@@ -6,6 +6,8 @@ import random
import string
import time
from tests.base import HTTPBIN_SERVER
try:
import urllib
except NameError:

93
tests/httpbin/basic.yml Normal file
View File

@@ -0,0 +1,93 @@
- config:
name: basic test with httpbin
request:
base_url: http://httpbin.org/
- test:
name: index
request:
url: /
method: GET
validate:
- eq: ["status_code", 200]
- contains: [content, "HTTP Request & Response Service"]
- test:
name: headers
request:
url: /headers
method: GET
validate:
- eq: ["status_code", 200]
- eq: [content.headers.Host, "httpbin.org"]
- test:
name: user-agent
request:
url: /user-agent
method: GET
validate:
- eq: ["status_code", 200]
- startswith: [content.user-agent, "python-requests"]
- test:
name: get without params
request:
url: /get
method: GET
validate:
- eq: ["status_code", 200]
- eq: [content.args, {}]
- test:
name: get with params in url
request:
url: /get?a=1&b=2
method: GET
validate:
- eq: ["status_code", 200]
- eq: [content.args, {'a': '1', 'b': '2'}]
- test:
name: get with params in params field
request:
url: /get
params:
a: 1
b: 2
method: GET
validate:
- eq: ["status_code", 200]
- eq: [content.args, {'a': '1', 'b': '2'}]
- test:
name: set cookie
request:
url: /cookies/set?name=value
method: GET
validate:
- eq: ["status_code", 200]
# - eq: [cookies.name, "value"]
- test:
name: extract cookie
request:
url: /cookies
method: GET
validate:
- eq: ["status_code", 200]
- eq: [cookies.name, "value"]
- test:
name: post data
request:
url: /post
method: POST
headers:
Content-Type: application/json
data: abc
validate:
- eq: ["status_code", 200]

View File

@@ -1,7 +1,7 @@
- config:
name: basic test with httpbin
request:
base_url: http://127.0.0.1:3458/
base_url: $HTTPBIN_SERVER
setup_hooks:
- ${hook_print(setup)}
teardown_hooks:
@@ -19,7 +19,7 @@
- ${teardown_hook_sleep_N_secs($response, 1)}
validate:
- eq: ["status_code", 200]
- eq: [content.headers.Host, "127.0.0.1:3458"]
- contained_by: [content.headers.Host, $HTTPBIN_SERVER]
- test:
name: alter response

View File

@@ -1,7 +1,7 @@
- config:
name: load images
request:
base_url: http://127.0.0.1:3458
base_url: $HTTPBIN_SERVER
- test:
name: get png image

View File

@@ -1,7 +1,7 @@
- config:
name: test upload file with httpbin
request:
base_url: http://127.0.0.1:3458
base_url: $HTTPBIN_SERVER
- test:
name: upload file

View File

@@ -0,0 +1,13 @@
- config:
name: basic test with httpbin
request:
base_url: http://httpbin.org/
- test:
name: headers
request:
url: /headers
method: GET
validate:
- eq: ["status_code", 200]
- assert_status_code_is_200: ["status_code"]

View File

@@ -3,7 +3,7 @@ import shutil
from httprunner import HttpRunner
from httprunner.exceptions import FileNotFound
from tests.base import ApiServerUnittest
from tests.base import HTTPBIN_SERVER, ApiServerUnittest
class TestHttpRunner(ApiServerUnittest):
@@ -118,7 +118,7 @@ class TestHttpRunner(ApiServerUnittest):
{
"name": "post data",
"request": {
"url": "http://127.0.0.1:3458/post",
"url": "{}/post".format(HTTPBIN_SERVER),
"method": "POST",
"headers": {
"Content-Type": "application/json"

View File

@@ -1,7 +1,7 @@
import requests
from httprunner import exceptions, response, utils
from httprunner.compat import bytes, str
from tests.base import ApiServerUnittest
from tests.base import HTTPBIN_SERVER, ApiServerUnittest
class TestResponse(ApiServerUnittest):
@@ -28,7 +28,7 @@ class TestResponse(ApiServerUnittest):
self.assertEqual(bytes, type(resp_obj.content))
def test_extract_response_status_code(self):
resp = requests.get(url="http://127.0.0.1:3458/status/200")
resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER))
resp_obj = response.ResponseObject(resp)
extract_binds_list = [
@@ -48,7 +48,7 @@ class TestResponse(ApiServerUnittest):
resp_obj.extract_response(extract_binds_list)
def test_extract_response_encoding_ok_reason_url(self):
resp = requests.get(url="http://127.0.0.1:3458/status/200")
resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER))
resp_obj = response.ResponseObject(resp)
extract_binds_list = [
@@ -62,7 +62,7 @@ class TestResponse(ApiServerUnittest):
self.assertEqual(extract_binds_dict["resp_encoding"], "utf-8")
self.assertEqual(extract_binds_dict["resp_ok"], True)
self.assertEqual(extract_binds_dict["resp_reason"], "OK")
self.assertEqual(extract_binds_dict["resp_url"], "http://127.0.0.1:3458/status/200")
self.assertEqual(extract_binds_dict["resp_url"], "{}/status/200".format(HTTPBIN_SERVER))
extract_binds_list = [{"resp_encoding": "encoding.xx"}]
with self.assertRaises(exceptions.ParamsError):
@@ -82,7 +82,7 @@ class TestResponse(ApiServerUnittest):
def test_extract_response_cookies(self):
resp = requests.get(
url="http://127.0.0.1:3458/cookies",
url="{}/cookies".format(HTTPBIN_SERVER),
headers={
"accept": "application/json"
}
@@ -106,7 +106,7 @@ class TestResponse(ApiServerUnittest):
def test_extract_response_elapsed(self):
resp = requests.post(
url="http://127.0.0.1:3458/anything",
url="{}/anything".format(HTTPBIN_SERVER),
json={
'success': False,
"person": {
@@ -146,7 +146,7 @@ class TestResponse(ApiServerUnittest):
resp_obj.extract_response(extract_binds_list)
def test_extract_response_headers(self):
resp = requests.get(url="http://127.0.0.1:3458/status/200")
resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER))
resp_obj = response.ResponseObject(resp)
extract_binds_list = [
@@ -167,7 +167,7 @@ class TestResponse(ApiServerUnittest):
def test_extract_response_body_json(self):
resp = requests.post(
url="http://127.0.0.1:3458/anything",
url="{}/anything".format(HTTPBIN_SERVER),
json={
'success': False,
"person": {
@@ -192,7 +192,7 @@ class TestResponse(ApiServerUnittest):
# "Connection": "keep-alive",
# "Content-Length": "129",
# "Content-Type": "application/json",
# "Host": "127.0.0.1:3458",
# "Host": HTTPBIN_SERVER,
# "User-Agent": "python-requests/2.18.4"
# },
# "json": {
@@ -211,7 +211,7 @@ class TestResponse(ApiServerUnittest):
# },
# "method": "POST",
# "origin": "127.0.0.1",
# "url": "http://127.0.0.1:3458/anything"
# "url": "{}/anything".format(HTTPBIN_SERVER)
# }
extract_binds_list = [
@@ -251,7 +251,7 @@ class TestResponse(ApiServerUnittest):
)
def test_extract_response_body_html(self):
resp = requests.get(url="http://127.0.0.1:3458/")
resp = requests.get(url=HTTPBIN_SERVER)
resp_obj = response.ResponseObject(resp)
extract_binds_list = [
@@ -269,7 +269,7 @@ class TestResponse(ApiServerUnittest):
resp_obj.extract_response(extract_binds_list)
def test_extract_response_others(self):
resp = requests.get(url="http://127.0.0.1:3458/status/200")
resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER))
resp_obj = response.ResponseObject(resp)
extract_binds_list = [
@@ -281,7 +281,7 @@ class TestResponse(ApiServerUnittest):
def test_extract_response_fail(self):
resp = requests.post(
url="http://127.0.0.1:3458/anything",
url="{}/anything".format(HTTPBIN_SERVER),
json={
'success': False,
"person": {
@@ -313,7 +313,7 @@ class TestResponse(ApiServerUnittest):
def test_extract_response_json_string(self):
resp = requests.post(
url="http://127.0.0.1:3458/anything",
url="{}/anything".format(HTTPBIN_SERVER),
data="abc"
)
@@ -330,7 +330,7 @@ class TestResponse(ApiServerUnittest):
def test_extract_text_response(self):
resp = requests.post(
url="http://127.0.0.1:3458/anything",
url="{}/anything".format(HTTPBIN_SERVER),
data="LB123abcRB789"
)
@@ -357,7 +357,7 @@ class TestResponse(ApiServerUnittest):
def test_extract_text_response_exception(self):
resp = requests.post(
url="http://127.0.0.1:3458/anything",
url="{}/anything".format(HTTPBIN_SERVER),
data="LB123abcRB789"
)
extract_binds_list = [
@@ -369,7 +369,7 @@ class TestResponse(ApiServerUnittest):
def test_extract_response_empty(self):
resp = requests.post(
url="http://127.0.0.1:3458/anything",
url="{}/anything".format(HTTPBIN_SERVER),
data="abc"
)

View File

@@ -4,7 +4,7 @@ import time
from httprunner import HttpRunner, exceptions, runner
from httprunner.testcase import TestcaseLoader
from httprunner.utils import FileUtils, deep_update_dict
from tests.base import ApiServerUnittest
from tests.base import HTTPBIN_SERVER, ApiServerUnittest
class TestRunner(ApiServerUnittest):
@@ -76,7 +76,7 @@ class TestRunner(ApiServerUnittest):
"path": os.path.join(os.getcwd(), __file__),
"name": "basic test with httpbin",
"request": {
"base_url": "http://127.0.0.1:3458/"
"base_url": HTTPBIN_SERVER
},
"setup_hooks": [
"${sleep_N_secs(0.5)}"
@@ -124,7 +124,7 @@ class TestRunner(ApiServerUnittest):
"path": os.path.join(os.getcwd(), __file__),
"name": "basic test with httpbin",
"request": {
"base_url": "http://127.0.0.1:3458/"
"base_url": HTTPBIN_SERVER
}
}
test = {
@@ -174,7 +174,7 @@ class TestRunner(ApiServerUnittest):
{
"name": "test teardown hooks",
"request": {
"url": "http://127.0.0.1:3458/headers",
"url": "{}/headers".format(HTTPBIN_SERVER),
"method": "GET",
"data": "abc"
},
@@ -209,7 +209,7 @@ class TestRunner(ApiServerUnittest):
{
"name": "test teardown hooks",
"request": {
"url": "http://127.0.0.1:3458/headers",
"url": "{}/headers".format(HTTPBIN_SERVER),
"method": "GET",
"data": "abc"
},
@@ -239,7 +239,7 @@ class TestRunner(ApiServerUnittest):
{
"name": "test teardown hooks",
"request": {
"url": "http://127.0.0.1:3458/headers",
"url": "{}/headers".format(HTTPBIN_SERVER),
"method": "GET",
"data": "abc"
},