Skip to content

Commit

Permalink
Add pytest fixture to prepare for pytest-httpbin
Browse files Browse the repository at this point in the history
We add a testing utils class `HttpbinRemote`, which is simply
a drop-in replacement for the `httpbin` fixture that pytest-httpbin
provides. `HttbinRemote` uses the remotely served version of httpbin.

This will allow us to write tests in the style of pytest-httpbin
until we can merge PR #164 into master.
  • Loading branch information
hemberger authored and moy committed Dec 8, 2017
1 parent 61d5040 commit 7360835
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 34 deletions.
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest

# This file is automatically discovered by pytest to define
# shared fixtures only once.


@pytest.fixture
def httpbin():
from utils import HttpbinRemote
return HttpbinRemote()
26 changes: 13 additions & 13 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import pytest


def test_submit_online():
def test_submit_online(httpbin):
"""Complete and submit the pizza form at http://httpbin.org/forms/post """
browser = mechanicalsoup.Browser()
page = browser.get("http://httpbin.org/forms/post")
page = browser.get(httpbin + "/forms/post")
form = page.soup.form

form.find("input", {"name": "custname"})["value"] = "Philip J. Fry"
Expand Down Expand Up @@ -104,21 +104,21 @@ def test__request_file():
assert "multipart/form-data" in response.request.headers["Content-Type"]


def test_no_404():
def test_no_404(httpbin):
browser = mechanicalsoup.Browser()
resp = browser.get("http://httpbin.org/nosuchpage")
resp = browser.get(httpbin + "/nosuchpage")
assert resp.status_code == 404


def test_404():
def test_404(httpbin):
browser = mechanicalsoup.Browser(raise_on_404=True)
with pytest.raises(mechanicalsoup.LinkNotFoundError):
resp = browser.get("http://httpbin.org/nosuchpage")
resp = browser.get("http://httpbin.org/")
resp = browser.get(httpbin + "/nosuchpage")
resp = browser.get(httpbin.url)
assert resp.status_code == 200


def test_set_cookiejar():
def test_set_cookiejar(httpbin):
"""Set cookies locally and test that they are received remotely."""
# construct a phony cookiejar and attach it to the session
jar = RequestsCookieJar()
Expand All @@ -127,25 +127,25 @@ def test_set_cookiejar():

browser = mechanicalsoup.Browser()
browser.set_cookiejar(jar)
resp = browser.get("http://httpbin.org/cookies")
resp = browser.get(httpbin + "/cookies")
assert resp.json() == {'cookies': {'field': 'value'}}


def test_get_cookiejar():
def test_get_cookiejar(httpbin):
"""Test that cookies set by the remote host update our session."""
browser = mechanicalsoup.Browser()
resp = browser.get("http://httpbin.org/cookies/set?k1=v1&k2=v2")
resp = browser.get(httpbin + "/cookies/set?k1=v1&k2=v2")
assert resp.json() == {'cookies': {'k1': 'v1', 'k2': 'v2'}}

jar = browser.get_cookiejar()
assert jar.get('k1') == 'v1'
assert jar.get('k2') == 'v2'


def test_post():
def test_post(httpbin):
browser = mechanicalsoup.Browser()
data = {'color': 'blue', 'colorblind': 'True'}
resp = browser.post("http://httpbin.org/post", data)
resp = browser.post(httpbin + "/post", data)
assert(resp.status_code == 200 and resp.json()['form'] == data)


Expand Down
8 changes: 4 additions & 4 deletions tests/test_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import pytest


def test_submit_online():
def test_submit_online(httpbin):
"""Complete and submit the pizza form at http://httpbin.org/forms/post """
browser = mechanicalsoup.Browser()
page = browser.get("http://httpbin.org/forms/post")
page = browser.get(httpbin + "/forms/post")
form = mechanicalsoup.Form(page.soup.form)

input_data = {"custname": "Philip J. Fry"}
Expand Down Expand Up @@ -36,10 +36,10 @@ def test_submit_online():
assert data["comments"] == "freezer"


def test_submit_set():
def test_submit_set(httpbin):
"""Complete and submit the pizza form at http://httpbin.org/forms/post """
browser = mechanicalsoup.Browser()
page = browser.get("http://httpbin.org/forms/post")
page = browser.get(httpbin + "/forms/post")
form = mechanicalsoup.Form(page.soup.form)

form["custname"] = "Philip J. Fry"
Expand Down
34 changes: 17 additions & 17 deletions tests/test_stateful_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ def test_request_forward():
assert r.text == 'Success!'


def test_submit_online():
def test_submit_online(httpbin):
"""Complete and submit the pizza form at http://httpbin.org/forms/post """
browser = mechanicalsoup.StatefulBrowser()
browser.set_user_agent('testing MechanicalSoup')
browser.open("http://httpbin.org/")
browser.open(httpbin.url)
for link in browser.links():
if link["href"] == "/":
browser.follow_link(link)
break
browser.follow_link("forms/post")
assert browser.get_url() == "http://httpbin.org/forms/post"
assert browser.get_url() == httpbin + "/forms/post"
browser.select_form("form")
browser["custname"] = "Customer Name Here"
browser["size"] = "medium"
Expand All @@ -53,39 +53,39 @@ def test_submit_online():
assert set(expected_headers).issubset(json["headers"].keys())


def test_no_404():
def test_no_404(httpbin):
browser = mechanicalsoup.StatefulBrowser()
resp = browser.open("http://httpbin.org/nosuchpage")
resp = browser.open(httpbin + "/nosuchpage")
assert resp.status_code == 404


def test_404():
def test_404(httpbin):
browser = mechanicalsoup.StatefulBrowser(raise_on_404=True)
with pytest.raises(mechanicalsoup.LinkNotFoundError):
resp = browser.open("http://httpbin.org/nosuchpage")
resp = browser.open("http://httpbin.org/")
resp = browser.open(httpbin + "/nosuchpage")
resp = browser.open(httpbin.url)
assert resp.status_code == 200


def test_user_agent():
def test_user_agent(httpbin):
browser = mechanicalsoup.StatefulBrowser(user_agent='007')
resp = browser.open("http://httpbin.org/user-agent")
resp = browser.open(httpbin + "/user-agent")
assert resp.json() == {'user-agent': '007'}


def test_open_relative():
def test_open_relative(httpbin):
# Open an arbitrary httpbin page to set the current URL
browser = mechanicalsoup.StatefulBrowser()
browser.open("http://httpbin.org/html")
browser.open(httpbin + "/html")

# Open a relative page and make sure remote host and browser agree on URL
resp = browser.open_relative("/get")
assert resp.json()['url'] == "http://httpbin.org/get"
assert browser.get_url() == "http://httpbin.org/get"
assert resp.json()['url'] == httpbin + "/get"
assert browser.get_url() == httpbin + "/get"

# Test passing additional kwargs to the session
resp = browser.open_relative("/basic-auth/me/123", auth=('me', '123'))
assert browser.get_url() == "http://httpbin.org/basic-auth/me/123"
assert browser.get_url() == httpbin + "/basic-auth/me/123"
assert resp.json() == {"authenticated": True, "user": "me"}


Expand Down Expand Up @@ -303,9 +303,9 @@ def test_form_multiple():
assert(response.status_code == 200 and response.text == 'Success!')


def test_upload_file():
def test_upload_file(httpbin):
browser = mechanicalsoup.StatefulBrowser()
browser.open("http://httpbin.org/forms/post")
browser.open(httpbin + "/forms/post")

# Create two temporary files to upload
def make_file(content):
Expand Down
10 changes: 10 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ def text_callback(request, context):

browser = mechanicalsoup.StatefulBrowser(requests_adapters={'mock': mock})
return browser, url


class HttpbinRemote:
"""Drop-in replacement for pytest-httpbin's httpbin fixture
that uses the remote httpbin server instead of a local one."""
def __init__(self):
self.url = "http://httpbin.org"

def __add__(self, x):
return self.url + x

0 comments on commit 7360835

Please sign in to comment.