Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
public-apis
Commits
4edd0d5f
Unverified
Commit
4edd0d5f
authored
Jan 12, 2022
by
Matheus Felipe
Browse files
Create basic tests to has_cloudflare_protection
parent
8ac60e31
Changes
1
Hide whitespace changes
Inline
Side-by-side
scripts/tests/test_validate_links.py
View file @
4edd0d5f
...
@@ -4,10 +4,29 @@ import unittest
...
@@ -4,10 +4,29 @@ import unittest
from
validate.links
import
find_links_in_text
from
validate.links
import
find_links_in_text
from
validate.links
import
get_host_from_link
from
validate.links
import
get_host_from_link
from
validate.links
import
has_cloudflare_protection
class
FakeResponse
():
def
__init__
(
self
,
code
:
int
,
headers
:
dict
,
text
:
str
)
->
None
:
self
.
status_code
=
code
self
.
headers
=
headers
self
.
text
=
text
class
TestValidateLinks
(
unittest
.
TestCase
):
class
TestValidateLinks
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
code_200
=
200
self
.
code_403
=
403
self
.
code_503
=
503
self
.
cloudflare_headers
=
{
'Server'
:
'cloudflare'
}
self
.
no_cloudflare_headers
=
{
'Server'
:
'google'
}
self
.
text_with_cloudflare_flags
=
'403 Forbidden Cloudflare We are checking your browser...'
self
.
text_without_cloudflare_flags
=
'Lorem Ipsum'
def
test_find_link_in_text
(
self
):
def
test_find_link_in_text
(
self
):
text
=
"""
text
=
"""
# this is valid
# this is valid
...
@@ -68,3 +87,49 @@ class TestValidateLinks(unittest.TestCase):
...
@@ -68,3 +87,49 @@ class TestValidateLinks(unittest.TestCase):
with
self
.
assertRaises
(
TypeError
):
with
self
.
assertRaises
(
TypeError
):
get_host_from_link
()
get_host_from_link
()
def
test_has_cloudflare_protection_with_code_403_and_503_in_response
(
self
):
resp_with_cloudflare_protection_code_403
=
FakeResponse
(
code
=
self
.
code_403
,
headers
=
self
.
cloudflare_headers
,
text
=
self
.
text_with_cloudflare_flags
)
resp_with_cloudflare_protection_code_503
=
FakeResponse
(
code
=
self
.
code_503
,
headers
=
self
.
cloudflare_headers
,
text
=
self
.
text_with_cloudflare_flags
)
result1
=
has_cloudflare_protection
(
resp_with_cloudflare_protection_code_403
)
result2
=
has_cloudflare_protection
(
resp_with_cloudflare_protection_code_503
)
self
.
assertTrue
(
result1
)
self
.
assertTrue
(
result2
)
def
test_has_cloudflare_protection_when_there_is_no_protection
(
self
):
resp_without_cloudflare_protection1
=
FakeResponse
(
code
=
self
.
code_200
,
headers
=
self
.
no_cloudflare_headers
,
text
=
self
.
text_without_cloudflare_flags
)
resp_without_cloudflare_protection2
=
FakeResponse
(
code
=
self
.
code_403
,
headers
=
self
.
no_cloudflare_headers
,
text
=
self
.
text_without_cloudflare_flags
)
resp_without_cloudflare_protection3
=
FakeResponse
(
code
=
self
.
code_503
,
headers
=
self
.
no_cloudflare_headers
,
text
=
self
.
text_without_cloudflare_flags
)
result1
=
has_cloudflare_protection
(
resp_without_cloudflare_protection1
)
result2
=
has_cloudflare_protection
(
resp_without_cloudflare_protection2
)
result3
=
has_cloudflare_protection
(
resp_without_cloudflare_protection3
)
self
.
assertFalse
(
result1
)
self
.
assertFalse
(
result2
)
self
.
assertFalse
(
result3
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment