Skip to content

Commit

Permalink
Merge pull request #19 from Intelworks/incorrect-response-should-rais…
Browse files Browse the repository at this point in the history
…e-exception

Adding workarounds to deal with libtaxii response parsing issues
  • Loading branch information
traut committed Apr 8, 2015
2 parents f379b59 + da64be9 commit 9dfb3e4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
18 changes: 17 additions & 1 deletion cabby/abstract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import urlparse
import urllib
import urllib2

from libtaxii.clients import HttpClient
Expand All @@ -11,7 +12,7 @@
from .utils import configure_client_auth
from .exceptions import (
NoURIProvidedError, UnsuccessfulStatusError, ServiceNotFoundError,
AmbiguousServicesError, ClientException, HTTPError
AmbiguousServicesError, ClientException, HTTPError, InvalidResponseError
)


Expand Down Expand Up @@ -154,10 +155,25 @@ def _execute_request(self, request, uri=None, service_type=None):
error = response_raw
self.log.debug("%s: %s", error, error.read())
raise HTTPError(error)
# https://github.com/TAXIIProject/libtaxii/issues/186
elif isinstance(response_raw, urllib.addinfourl) and \
not response_raw.info().getheader('X-TAXII-Content-Type'):

headers = ''.join(response_raw.info().headers)
body = response_raw.read()

self.log.debug("Invalid response:\n%s", headers + body)
raise InvalidResponseError("Invalid response received from %s" %
full_path)

response = get_message_from_http_response(
response_raw, in_response_to='0')

if response.version != self.taxii_version:
raise InvalidResponseError("TAXII version in response message '%s' "
"does not match client's configured version '%s'" %
(response.version, self.taxii_version))

self.log.info("Response received for %s from %s",
request.message_type, full_path)

Expand Down
3 changes: 3 additions & 0 deletions cabby/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class ClientException(Exception):
class HTTPError(ClientException):
pass

class InvalidResponseError(ClientException):
pass

class UnsuccessfulStatusError(ClientException):

def __init__(self, taxii_status, *args, **kwargs):
Expand Down
40 changes: 34 additions & 6 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@
from cabby import exceptions as exc
from cabby import entities

from fixtures11 import *
import fixtures11
import fixtures10

CUSTOM_HEADER_NAME = 'X-custom-header'
CUSTOM_HEADER_VALUE = 'header value with space!'


def get_fix(version):
return (fixtures10 if version == 10 else fixtures11)

def make_client(version, **kwargs):
client = create_client(HOST, version=("1.1" if version == 11 else "1.0"), **kwargs)
client = create_client(get_fix(version).HOST, version=("1.1" if version == 11 else "1.0"), **kwargs)
return client


def register_uri(uri, body, **kwargs):
def register_uri(uri, body, version, **kwargs):
content_type = VID_TAXII_XML_11 if version == 11 else VID_TAXII_XML_10
httpretty.register_uri(httpretty.POST, uri, body=body, content_type='application/xml',
adding_headers={'X-TAXII-Content-Type': VID_TAXII_XML_11}, **kwargs)
adding_headers={'X-TAXII-Content-Type': content_type}, **kwargs)


def get_sent_message(version):
Expand All @@ -39,11 +45,14 @@ def test_set_headers(version):

httpretty.enable()

register_uri(DISCOVERY_URI_HTTP, DISCOVERY_RESPONSE)
uri = get_fix(version).DISCOVERY_URI_HTTP
response = get_fix(version).DISCOVERY_RESPONSE

register_uri(uri, response, version)

client = make_client(version, headers={CUSTOM_HEADER_NAME : CUSTOM_HEADER_VALUE})

services = client.discover_services(uri=DISCOVERY_URI_HTTP)
services = client.discover_services(uri=uri)

assert len(services) == 4

Expand All @@ -56,3 +65,22 @@ def test_set_headers(version):
assert last_request.headers[CUSTOM_HEADER_NAME] == CUSTOM_HEADER_VALUE

httpretty.disable()



@pytest.mark.parametrize("version", [11, 10])
def test_invalid_response(version):

httpretty.enable()

uri = get_fix(version).DISCOVERY_URI_HTTP

httpretty.register_uri(httpretty.POST, uri, body="INVALID-BODY", content_type='text/html')

client = make_client(version)

with pytest.raises(exc.InvalidResponseError):
services = client.discover_services(uri=uri)

httpretty.disable()

0 comments on commit 9dfb3e4

Please sign in to comment.