Skip to content
Merged
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
9 changes: 7 additions & 2 deletions github_webhook/webhook.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,7 @@
import hashlib
import hmac
import logging

import json
import six
from flask import abort, request

Expand DownExpand Up@@ -58,7 +58,12 @@ def _postreceive(self):
abort(400, "Invalid signature")

event_type = _get_header("X-Github-Event")
data = request.get_json()
content_type = _get_header("content-type")
data = (
json.loads(request.form.to_dict(flat=True)["payload"])
if content_type == "application/x-www-form-urlencoded"
else request.get_json()
)

if data is None:
abort(400, "Request body must contain json")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,7 @@

setup(
name="github-webhook",
version="1.0.2",
version="1.0.3",
description="Very simple, but powerful, microframework for writing Github webhooks in Python",
url="https://github.com/bloomberg/python-github-webhook",
author="Alex Chamberlain, Fred Phillips, Daniel Kiss, Daniel Beer",
Expand Down
35 changes: 35 additions & 0 deletions tests/test_webhook.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@

import pytest
import werkzeug
import json

try:
from unittest import mock
Expand All@@ -23,6 +24,14 @@ def mock_request():
@pytest.fixture
def push_request(mock_request):
mock_request.headers["X-Github-Event"] = "push"
mock_request.headers["content-type"] = "application/json"
yield mock_request


@pytest.fixture
def push_request_encoded(mock_request):
mock_request.headers["X-Github-Event"] = "push"
mock_request.headers["content-type"] = "application/x-www-form-urlencoded"
yield mock_request


Expand DownExpand Up@@ -64,9 +73,35 @@ def test_run_push_hook(webhook, handler, push_request):
handler.assert_called_once_with(push_request.get_json.return_value)


def test_run_push_hook_urlencoded(webhook, handler, push_request_encoded):
github_mock_payload ={"payload": '{"key": "value"}'}
push_request_encoded.form.to_dict.return_value = github_mock_payload
payload = json.loads(github_mock_payload["payload"])

# WHEN
webhook._postreceive()

# THEN
handler.assert_called_once_with(payload)


def test_do_not_run_push_hook_on_ping(webhook, handler, mock_request):
# GIVEN
mock_request.headers["X-Github-Event"] = "ping"
mock_request.headers["content-type"] = "application/json"

# WHEN
webhook._postreceive()

# THEN
handler.assert_not_called()


def test_do_not_run_push_hook_on_ping_urlencoded(webhook, handler, mock_request):
# GIVEN
mock_request.headers["X-Github-Event"] = "ping"
mock_request.headers["content-type"] = "application/x-www-form-urlencoded"
mock_request.form.to_dict.return_value ={"payload": '{"key": "value"}'}

# WHEN
webhook._postreceive()
Expand Down