Skip to content

Commit

Permalink
tests: httpretty mock
Browse files Browse the repository at this point in the history
* Mocked HTTPretty, which has a bug causing SSL requests
  fail for certain versions of openssl library.
  Related: gabrielfalcao/HTTPretty#242

Signed-off-by: Krzysztof Nowak <k.nowak@cern.ch>
  • Loading branch information
Krzysztof Nowak authored and lnielsen committed Nov 13, 2017
1 parent 73cdea3 commit ee58a84
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 10 deletions.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import json
from os.path import dirname, join

import httpretty
import pytest
from httpretty_mock import httpretty
from lxml import etree


Expand Down
67 changes: 67 additions & 0 deletions tests/httpretty_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
#
# This file is part of DataCite.
#
# Copyright (C) 2017 CERN.
#
# DataCite is free software; you can redistribute it and/or modify it
# under the terms of the Revised BSD License; see LICENSE file for
# more details.

"""Mock HTTPretty.
HTTPretty fix related to SSL bug:
https://github.com/gabrielfalcao/HTTPretty/issues/242
"""


import httpretty
import httpretty.core
from httpretty import HTTPretty as OriginalHTTPretty

try:
from requests.packages.urllib3.contrib.pyopenssl import \
inject_into_urllib3, extract_from_urllib3
pyopenssl_override = True
except:
pyopenssl_override = False


class MyHTTPretty(OriginalHTTPretty):
"""
HTTPretty mock.
pyopenssl monkey-patches the default ssl_wrap_socket() function in the
'requests' library, but this can stop the HTTPretty socket monkey-patching
from working for HTTPS requests.
Our version extends the base HTTPretty enable() and disable()
implementations to undo and redo the pyopenssl monkey-patching,
respectively.
"""

@classmethod
def enable(cls):
"""Enable method mock."""
OriginalHTTPretty.enable()
if pyopenssl_override:
# Take out the pyopenssl version - use the default implementation
extract_from_urllib3()

@classmethod
def disable(cls):
"""Disable method mock."""
OriginalHTTPretty.disable()
if pyopenssl_override:
# Put the pyopenssl version back in place
inject_into_urllib3()


# Substitute in our version
HTTPretty = MyHTTPretty

httpretty.core.httpretty = MyHTTPretty

# May need to set other module-level attributes here, e.g. enable, reset etc,
# depending on your needs
httpretty.httpretty = MyHTTPretty
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import socket
import ssl

import httpretty
import pytest
from helpers import APIURL, get_client
from httpretty_mock import httpretty
from mock import patch
from requests import ConnectionError

Expand Down
2 changes: 1 addition & 1 deletion tests/test_doi_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from __future__ import absolute_import, print_function

import httpretty
import pytest
from helpers import APIURL, get_client
from httpretty_mock import httpretty

from datacite.errors import DataCiteForbiddenError, DataCiteGoneError, \
DataCiteNoContentError, DataCiteNotFoundError, DataCiteServerError, \
Expand Down
2 changes: 1 addition & 1 deletion tests/test_doi_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from __future__ import absolute_import, print_function

import httpretty
import pytest
from helpers import APIURL, get_client
from httpretty_mock import httpretty

from datacite._compat import b
from datacite.errors import DataCiteBadRequestError, DataCiteForbiddenError, \
Expand Down
2 changes: 1 addition & 1 deletion tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from __future__ import absolute_import, print_function

import httpretty
from httpretty_mock import httpretty

APIURL = "https://mds.datacite.org/"

Expand Down
2 changes: 1 addition & 1 deletion tests/test_media_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from __future__ import absolute_import, print_function

import httpretty
import pytest
from helpers import APIURL, get_client
from httpretty_mock import httpretty

from datacite.errors import DataCiteForbiddenError, DataCiteNotFoundError, \
DataCiteServerError, DataCiteUnauthorizedError
Expand Down
2 changes: 1 addition & 1 deletion tests/test_media_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from __future__ import absolute_import, print_function

import httpretty
import pytest
from helpers import APIURL, get_client
from httpretty_mock import httpretty

from datacite.errors import DataCiteBadRequestError, DataCiteForbiddenError, \
DataCiteServerError, DataCiteUnauthorizedError
Expand Down
2 changes: 1 addition & 1 deletion tests/test_metadata_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from __future__ import absolute_import, print_function

import httpretty
import pytest
from helpers import APIURL, get_client
from httpretty_mock import httpretty

from datacite.errors import DataCiteForbiddenError, DataCiteNotFoundError, \
DataCiteServerError, DataCiteUnauthorizedError
Expand Down
2 changes: 1 addition & 1 deletion tests/test_metadata_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from __future__ import absolute_import, print_function

import httpretty
import pytest
from helpers import APIURL, get_client
from httpretty_mock import httpretty

from datacite.errors import DataCiteForbiddenError, DataCiteGoneError, \
DataCiteNotFoundError, DataCiteServerError, DataCiteUnauthorizedError
Expand Down
2 changes: 1 addition & 1 deletion tests/test_metadata_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from __future__ import absolute_import, print_function

import httpretty
import pytest
from helpers import APIURL, get_client
from httpretty_mock import httpretty

from datacite.errors import DataCiteBadRequestError, DataCiteForbiddenError, \
DataCiteServerError, DataCiteUnauthorizedError
Expand Down

0 comments on commit ee58a84

Please sign in to comment.