Skip to content

Commit

Permalink
test: move all non-local URLs to pytest fixtures (#1408)
Browse files Browse the repository at this point in the history
This is in anticipation to switch them to local URLs.

Related to Issue #575.
  • Loading branch information
Thiago Perrotta authored Oct 9, 2023
1 parent f703824 commit 382f8b5
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 95 deletions.
11 changes: 4 additions & 7 deletions tests/browsing_context/test_get_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,11 @@ async def test_browsingContext_getTreeWithRoot_contextReturned(websocket):
}


# TODO: make offline.
@pytest.mark.asyncio
async def test_browsingContext_afterNavigation_getTreeWithNestedCrossOriginContexts_contextsReturned(
websocket, context_id, html, iframe):
nested_iframe = 'https://example.com/'
another_nested_iframe = 'https://example.org/'
page_with_nested_iframe = html(iframe(nested_iframe))
another_page_with_nested_iframe = html(iframe(another_nested_iframe))
websocket, context_id, html, iframe, example_url, another_example_url):
page_with_nested_iframe = html(iframe(example_url))
another_page_with_nested_iframe = html(iframe(another_example_url))

await goto_url(websocket, context_id, page_with_nested_iframe, "complete")
await goto_url(websocket, context_id, another_page_with_nested_iframe,
Expand All @@ -77,7 +74,7 @@ async def test_browsingContext_afterNavigation_getTreeWithNestedCrossOriginConte
"context": context_id,
"children": [{
"context": ANY_STR,
"url": another_nested_iframe,
"url": another_example_url,
"children": []
}],
"parent": None,
Expand Down
11 changes: 4 additions & 7 deletions tests/browsing_context/test_nested_browsing_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,11 @@ async def test_nestedBrowsingContext_navigateSameDocumentNavigation_waitComplete
} == result


# TODO: make offline.
@pytest.mark.asyncio
async def test_nestedBrowsingContext_afterNavigation_getTreeWithNestedCrossOriginContexts_contextsReturned(
websocket, iframe_id, html, iframe):
nested_iframe = 'https://example.com/'
another_nested_iframe = 'https://example.org/'
page_with_nested_iframe = html(iframe(nested_iframe))
another_page_with_nested_iframe = html(iframe(another_nested_iframe))
websocket, iframe_id, html, iframe, example_url, another_example_url):
page_with_nested_iframe = html(iframe(example_url))
another_page_with_nested_iframe = html(iframe(another_example_url))

await goto_url(websocket, iframe_id, page_with_nested_iframe, "complete")
await goto_url(websocket, iframe_id, another_page_with_nested_iframe,
Expand All @@ -269,7 +266,7 @@ async def test_nestedBrowsingContext_afterNavigation_getTreeWithNestedCrossOrigi
"context": iframe_id,
"children": [{
"context": ANY_STR,
"url": another_nested_iframe,
"url": another_example_url,
"children": []
}],
"parent": ANY_STR,
Expand Down
39 changes: 39 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,45 @@ def url_all_origins(request):
return request.param


@pytest.fixture
def example_url():
"""Return a generic example URL with status code 200."""
# TODO: Switch to a local server so that it works off-line.
# Alternatively: https://www.example.org/
return "https://www.example.com/"


@pytest.fixture
def another_example_url():
# TODO: Switch to a local server so that it works off-line.
"""Return a generic example URL with status code 200, in a domain other than the example_url fixture."""
return "https://www.example.org/"


@pytest.fixture
def auth_required_url():
"""Return a URL that requires authentication (status code 401)."""
# TODO: Switch to a local server so that it works off-line.
# All of these URLs work, just pick one.
# url = "https://authenticationtest.com/HTTPAuth/"
# url = "http://the-internet.herokuapp.com/basic_auth"
return "http://httpstat.us/401"


@pytest.fixture
def bad_ssl_url():
"""
Return a URL with an expired SSL certificate.
In Chromium, this generates the following error:
> Your connection is not private
> NET::ERR_CERT_DATE_INVALID
"""
# TODO: Switch to a local server so that it works off-line.
return "https://expired.badssl.com/"


@pytest.fixture
def read_sorted_messages(websocket):
"""Read the given number of messages from the websocket, and returns them
Expand Down
10 changes: 4 additions & 6 deletions tests/network/test_add_intercept.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,8 @@ async def test_add_intercept_type_pattern_port_empty_invalid(websocket):
"pattern",
"string and pattern",
])
async def test_add_intercept_blocks(websocket, context_id, url_patterns):
# TODO: make offline.
url = "https://www.example.com/"

async def test_add_intercept_blocks(websocket, context_id, url_patterns,
example_url):
await subscribe(websocket, ["cdp.Fetch.requestPaused"])

result = await execute_command(
Expand All @@ -312,7 +310,7 @@ async def test_add_intercept_blocks(websocket, context_id, url_patterns):
websocket, {
"method": "browsingContext.navigate",
"params": {
"url": url,
"url": example_url,
"context": context_id,
}
})
Expand All @@ -327,7 +325,7 @@ async def test_add_intercept_blocks(websocket, context_id, url_patterns):
"networkId": ANY_STR,
"request": AnyExtending({
"headers": ANY_DICT,
"url": url,
"url": example_url,
}),
"requestId": ANY_STR,
"resourceType": "Document",
Expand Down
31 changes: 12 additions & 19 deletions tests/network/test_continue_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@ async def test_continue_request_invalid_phase(websocket, context_id, phase,


@pytest.mark.asyncio
async def test_continue_request_invalid_url(websocket, context_id):
# TODO: make offline.
url = "https://www.example.com/"
async def test_continue_request_invalid_url(websocket, context_id,
example_url):
invalid_url = '%invalid%'

network_id = await create_dummy_blocked_request(
websocket, context_id, url=url, phases=["beforeRequestSent"])
websocket, context_id, url=example_url, phases=["beforeRequestSent"])

with pytest.raises(
Exception,
Expand Down Expand Up @@ -148,10 +147,7 @@ async def test_continue_request_non_blocked_request(websocket, context_id,


@pytest.mark.asyncio
async def test_continue_request_completes(websocket, context_id):
# TODO: make offline.
url = "https://www.example.com/"

async def test_continue_request_completes(websocket, context_id, example_url):
await subscribe(websocket, ["cdp.Fetch.requestPaused"])

await execute_command(
Expand All @@ -161,15 +157,15 @@ async def test_continue_request_completes(websocket, context_id):
"phases": ["beforeRequestSent"],
"urlPatterns": [{
"type": "string",
"pattern": url,
"pattern": example_url,
}, ],
},
})
await send_JSON_command(
websocket, {
"method": "browsingContext.navigate",
"params": {
"url": url,
"url": example_url,
"context": context_id,
}
})
Expand All @@ -183,7 +179,7 @@ async def test_continue_request_completes(websocket, context_id):
"method": "network.continueRequest",
"params": {
"request": network_id,
"url": url,
"url": example_url,
},
})

Expand All @@ -205,10 +201,7 @@ async def test_continue_request_completes(websocket, context_id):


@pytest.mark.asyncio
async def test_continue_request_twice(websocket, context_id):
# TODO: make offline.
url = "https://www.example.com/"

async def test_continue_request_twice(websocket, context_id, example_url):
await subscribe(websocket, ["cdp.Fetch.requestPaused"])

await execute_command(
Expand All @@ -218,15 +211,15 @@ async def test_continue_request_twice(websocket, context_id):
"phases": ["beforeRequestSent"],
"urlPatterns": [{
"type": "string",
"pattern": url,
"pattern": example_url,
}, ],
},
})
await send_JSON_command(
websocket, {
"method": "browsingContext.navigate",
"params": {
"url": url,
"url": example_url,
"context": context_id,
}
})
Expand All @@ -240,7 +233,7 @@ async def test_continue_request_twice(websocket, context_id):
"method": "network.continueRequest",
"params": {
"request": network_id,
"url": url,
"url": example_url,
},
})

Expand All @@ -258,7 +251,7 @@ async def test_continue_request_twice(websocket, context_id):
"method": "network.continueRequest",
"params": {
"request": network_id,
"url": url,
"url": example_url,
},
})

Expand Down
Loading

0 comments on commit 382f8b5

Please sign in to comment.