Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use request session to allow using cookies #6 #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions behave_http/steps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@
import jpath
import json
from ensure import ensure
from purl import URL
import requests
import time
from six.moves import urllib

from behave_http.utils import dereference_step_parameters_and_data, append_path


def get_requests(context):
try:
return context.session
except AttributeError as e:
context.session = requests.Session()
return context.session


@behave.given('I am using server "{server}"')
@dereference_step_parameters_and_data
def using_server(context, server):
context.server = URL(server)
context.server = server


@behave.given('I set base URL to "{base_url}"')
@dereference_step_parameters_and_data
def set_base_url(context, base_url):
context.server = context.server.add_path_segment(base_url)
context.server = urllib.parse.urljoin(context.server, base_url)


@behave.given('I set "{var}" header to "{value}"')
Expand Down Expand Up @@ -60,7 +68,8 @@ def header_oauth(context, key, secret):
@behave.given('I set variable "{variable}" to {value}')
@dereference_step_parameters_and_data
def set_var(context, variable, value):
setattr(context, variable, json.loads(value))
#setattr(context, variable, json.loads(value))
context.template_data[variable] = json.loads(value)


@behave.given('I do not want to verify server certificate')
Expand All @@ -77,7 +86,7 @@ def poll_GET(context, url_path_segment, jsonpath):
json_value = json.loads(context.data.decode('utf-8'))
url = append_path(context.server, url_path_segment)
for i in range(context.n_attempts):
response = requests.get(
response = get_requests(context).get(
url, headers=context.headers, auth=context.auth,
verify=context.verify_ssl)
if jpath.get(jsonpath, response.json()) == json_value:
Expand All @@ -93,7 +102,7 @@ def poll_GET(context, url_path_segment, jsonpath):
@dereference_step_parameters_and_data
def head_request(context, url_path_segment):
url = append_path(context.server, url_path_segment)
context.response = requests.head(
context.response = get_requests(context).head(
url, headers=context.headers, auth=context.auth,
verify=context.verify_ssl)

Expand All @@ -102,7 +111,7 @@ def head_request(context, url_path_segment):
@dereference_step_parameters_and_data
def options_request(context, url_path_segment):
url = append_path(context.server, url_path_segment)
context.response = requests.options(
context.response = get_requests(context).options(
url, headers=context.headers, auth=context.auth,
verify=context.verify_ssl)

Expand All @@ -111,7 +120,7 @@ def options_request(context, url_path_segment):
@dereference_step_parameters_and_data
def trace_request(context, url_path_segment):
url = append_path(context.server, url_path_segment)
context.response = requests.request(
context.response = get_requests(context).request(
'TRACE', url, headers=context.headers, auth=context.auth,
verify=context.verify_ssl)

Expand All @@ -120,7 +129,7 @@ def trace_request(context, url_path_segment):
@dereference_step_parameters_and_data
def patch_request(context, url_path_segment):
url = append_path(context.server, url_path_segment)
context.response = requests.patch(
context.response = get_requests(context).patch(
url, data=context.data, headers=context.headers, auth=context.auth,
verify=context.verify_ssl)

Expand All @@ -129,7 +138,7 @@ def patch_request(context, url_path_segment):
@dereference_step_parameters_and_data
def put_request(context, url_path_segment):
url = append_path(context.server, url_path_segment)
context.response = requests.put(
context.response = get_requests(context).put(
url, data=context.data, headers=context.headers, auth=context.auth,
verify=context.verify_ssl)

Expand All @@ -138,7 +147,7 @@ def put_request(context, url_path_segment):
@dereference_step_parameters_and_data
def post_request(context, url_path_segment):
url = append_path(context.server, url_path_segment)
context.response = requests.post(
context.response = get_requests(context).post(
url, data=context.data, headers=context.headers, auth=context.auth,
verify=context.verify_ssl)

Expand All @@ -147,7 +156,7 @@ def post_request(context, url_path_segment):
@dereference_step_parameters_and_data
def get_request(context, url_path_segment):
url = append_path(context.server, url_path_segment)
context.response = requests.get(
context.response = get_requests(context).get(
url, headers=context.headers, auth=context.auth,
verify=context.verify_ssl)

Expand All @@ -156,7 +165,7 @@ def get_request(context, url_path_segment):
@dereference_step_parameters_and_data
def delete_request(context, url_path_segment):
url = append_path(context.server, url_path_segment)
context.response = requests.delete(
context.response = get_requests(context).delete(
url, headers=context.headers, auth=context.auth,
verify=context.verify_ssl)

Expand All @@ -171,7 +180,8 @@ def store_for_template(context, jsonpath, variable):
@behave.then('the variable "{variable}" should be "{value}"')
@dereference_step_parameters_and_data
def get_var(context, variable, value):
ensure(getattr(context, variable)).equals(value)
#ensure(getattr(context, variable)).equals(value)
ensure(context.template_data[variable]).equals(value)


@behave.then('the response status should be one of "{statuses}"')
Expand Down
23 changes: 9 additions & 14 deletions behave_http/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from functools import wraps
import jinja2
import os
from purl import URL
from six.moves import urllib


def _get_data_from_context(context):
Expand All @@ -20,11 +20,13 @@ def dereference_step_parameters_and_data(f):

This involves three steps:

1) Rendering feature file step parameters as a Jinja2 template against
context.template_data.
1) Rendering feature file step parameters as a Jinja2 template
against context.template_data.

2) Replacing step parameters with environment variable values if they
look like an environment variable (start with a "$").
2) Replacing step parameters with environment variable values
if they look like an environment variable (start with a
"$"). Fall back to original value if environment variable does
not exist.

3) Treating context.text as a Jinja2 template rendered against
context.template_data, and putting the result in context.data.
Expand All @@ -36,19 +38,12 @@ def wrapper(context, **kwargs):
for key, value in kwargs.items():
value = jinja2.Template(value).render(context.template_data)
if value.startswith('$'):
value = os.environ.get(value[1:], '')
value = os.environ.get(value[1:], value)
decoded_kwargs[key] = value
context.data = _get_data_from_context(context)
return f(context, **decoded_kwargs)
return wrapper


def append_path(url, url_path):
target = URL(url_path)
if url_path.startswith('/'):
url = url.path(target.path())
else:
url = url.add_path_segment(target.path())
if target.query():
url = url.query(target.query())
return url.as_string()
return urllib.parse.urljoin(url, url_path)
64 changes: 32 additions & 32 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
#!/usr/bin/env python

from distutils.core import setup

import os

from setuptools import setup

with open(os.path.join(os.path.dirname(__file__),
'README.rst')) as readme_file:
with open(os.path.join(os.path.dirname(__file__), "README.rst")) as readme_file:
long_description = readme_file.read()

setup_requires = ['wheel']
setup_requires = ["wheel"]

install_requires = [
'behave>=1.2.4',
'Jinja2>=2.5',
'jpath>=1.1',
'ensure>=0.1.6',
'purl>=0.6',
'requests>=2.0.0',
"behave>=1.2.4",
"Jinja2>=2.5",
"jpath>=1.1",
"ensure>=0.1.6",
"requests>=2.0.0",
"six",
]

setup(
name='behave-http',
version='0.1.1',
packages=['behave_http', 'behave_http.steps'],
setup_requires=setup_requires,
install_requires=install_requires,
description="Behave HTTP steps",
long_description=long_description,
url='https://github.com/mikek/behave-http',
author='Mykhailo Kolesnyk',
author_email='[email protected]',
license='BSD 2-Clause',
url="https://github.com/mikek/behave-http",
author="Mykhailo Kolesnyk",
author_email="[email protected]",
license="BSD 2-Clause",
py_modules="behave_http",
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Development Status :: 4 - Beta',
'Natural Language :: English',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'Operating System :: OS Independent',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
'Topic :: Software Development :: Testing',
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Development Status :: 4 - Beta",
"Natural Language :: English",
"Environment :: Web Environment",
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
"Topic :: Software Development :: Testing",
],
)