Skip to content

Commit

Permalink
- Adding useful tests to more quickly iterate on building out configu…
Browse files Browse the repository at this point in the history
…rable app.

- Only outputting helpful request/response info if it is set.
- Distinguish between JSON/form data in request body.
  • Loading branch information
alexdlaird committed Aug 15, 2020
1 parent c35432d commit d39dda8
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 14 deletions.
4 changes: 2 additions & 2 deletions hookee/blueprints/default_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import hookee.plugins

from flask import Blueprint, request, Response
from flask import Blueprint, request, Response, jsonify
from hookee import conf

__author__ = "Alex Laird"
Expand Down Expand Up @@ -52,7 +52,7 @@ def webhook():
click.secho("{}{}{}".format("-" * width, title, "-" * width), fg="magenta", bold=True)
click.echo("")

response = Response()
response = jsonify({})
for name in filter(lambda n: n.startswith("hookee.plugins.default_response"), plugins.keys()):
plugins[name].call(request, response)

Expand Down
1 change: 1 addition & 0 deletions hookee/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def stop(self):
self.alive = False

def _banner(self):
# TODO: refactor all these reused dynamic title widths to helper function
title = "Endpoints Ready for Requests"
width = int((conf.CONSOLE_WIDTH - len(title)) / 2)

Expand Down
5 changes: 4 additions & 1 deletion hookee/plugins/default_request_1_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@

def call(request):
# TODO: pretty this up further
click.secho("Headers: {}".format(dict(request.headers)), fg="magenta")
if request.headers:
click.secho("Headers: {}".format(dict(request.headers)), fg="magenta")

return request
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@

def call(request):
# TODO: pretty this up further
click.secho("Args: {}".format(dict(request.args)), fg="magenta")
if request.args:
click.secho("Query: {}".format(dict(request.args)), fg="magenta")

return request
12 changes: 10 additions & 2 deletions hookee/plugins/default_request_3_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@

def call(request):
# TODO: pretty this up further
click.secho("Form Data: {}".format(dict(request.form)), fg="magenta")
click.secho("Body: {}".format(request.data.decode("utf-8")), fg="magenta")
if request.is_json:
click.secho("Body Type: JSON", fg="magenta")
click.secho("Body: {}".format(request.json), fg="magenta")
elif request.form and not request.data:
click.secho("Body Type: FORM")
click.secho("Body: {}".format(dict(request.form)), fg="magenta")
elif request.data:
click.secho("Body: {}".format(dict(request.data)), fg="magenta")

return request
5 changes: 4 additions & 1 deletion hookee/plugins/default_request_4_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@

def call(request):
# TODO: pretty this up further
click.secho("Files: {}".format(dict(request.files)), fg="magenta")
if request.files:
click.secho("Files: {}".format(dict(request.files)), fg="magenta")

return request
6 changes: 4 additions & 2 deletions hookee/plugins/default_response_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
def call(request, response):
# TODO: pretty this up further
click.secho("Status Code: {}".format(response.status_code), fg="magenta")
click.secho("Headers: {}".format(dict(response.headers)), fg="magenta")
click.secho("Body: {}".format(response.data.decode("utf-8")), fg="magenta")
if response.headers:
click.secho("Headers: {}".format(dict(response.headers)), fg="magenta")
if response.data:
click.secho("Body: {}".format(response.data.decode("utf-8")), fg="magenta")

return response
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ coverage
codecov
nose
mock
twine
twine
requests
38 changes: 38 additions & 0 deletions tests/test_inspection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import requests

from tests.testcase import ManagedTestCase

__author__ = "Alex Laird"
__copyright__ = "Copyright 2020, Alex Laird"
__version__ = "0.0.3"


class TestInspection(ManagedTestCase):

def test_http_get_query_params(self):
# GIVEN
params = {"param_1": "param_value_1"}

# WHEN
requests.get(self.webhook_url, params=params)

# TODO: finish out test assertions

def test_http_post_form_data(self):
# GIVEN
data = {"form_data_1": "form_data_value_1"}

# WHEN
requests.post(self.webhook_url, data=data)

# TODO: finish out test assertions

def test_http_post_json_data(self):
# GIVEN
headers = {"Content-Type": "application/json"}
data = {"json_data_1": "json_data_value_1"}

# WHEN
requests.post(self.webhook_url, headers=headers, json=data)

# TODO: finish out test assertions
5 changes: 1 addition & 4 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__version__ = "0.0.3"


class TestCLI(unittest.TestCase):
class TestManager(unittest.TestCase):

def test_manager(self):
# GIVEN
Expand All @@ -18,9 +18,6 @@ def test_manager(self):
# WHEN
manager.start()

# Wait for things to boot
time.sleep(2)

# THEN
self.assertIsNotNone(manager.server._thread)
self.assertIsNotNone(manager.tunnel._thread)
Expand Down
38 changes: 38 additions & 0 deletions tests/testcase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import time
import unittest

from future.utils import iteritems

from hookee.manager import Manager

__author__ = "Alex Laird"
__copyright__ = "Copyright 2020, Alex Laird"
__version__ = "0.0.3"


class ManagedTestCase(unittest.TestCase):
REQUEST_FORM_DATA_BOUNDARY = "REQUEST_FORM_DATA_BOUNDARY"
FORM_DATA_STARTING_PAYLOAD = '--{0}\r\nContent-Disposition: form-data; name=\\"'.format(REQUEST_FORM_DATA_BOUNDARY)
FORM_DATA_MIDDLE_PAYLOAD = '\"\r\n\r\n'
FORM_DATA_ENDING_PAYLOAD = '--{0}--'.format(REQUEST_FORM_DATA_BOUNDARY)

def setUp(self):
self.port = 5000
self.manager = Manager.get_instance(self.port)

self.manager.start()

self.webhook_url = "{}/webhook".format(self.manager.tunnel.public_url)

def tearDown(self):
self.manager.stop()

time.sleep(2)

def generate_form_data_payload(self, data):
payload = ""
for key, value in iteritems(data):
payload += '{0}{1}{2}{3}\r\n'.format(self.FORM_DATA_STARTING_PAYLOAD, key, self.FORM_DATA_MIDDLE_PAYLOAD,
value)
payload += self.FORM_DATA_ENDING_PAYLOAD
return payload

0 comments on commit d39dda8

Please sign in to comment.