Skip to content

Commit f8d62a4

Browse files
committed
foobar
1 parent 6719547 commit f8d62a4

14 files changed

+129
-94
lines changed

.github/workflows/main.yml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,6 @@ jobs:
2020
matrix:
2121
python-version: ['3.5', '3.6', '3.7', '3.8', '3.9', '3.10', '3.11', 'pypy3.9']
2222

23-
services:
24-
# https://docs.github.com/en/actions/using-containerized-services/about-service-containers
25-
redis:
26-
# Docker Hub image
27-
image: redis
28-
# Set health checks to wait until redis has started
29-
options: >-
30-
--health-cmd "redis-cli ping"
31-
--health-interval 10s
32-
--health-timeout 5s
33-
--health-retries 5
34-
ports:
35-
# Maps port 6379 on service container to the host
36-
- 6379:6379
37-
3823
steps:
3924
- uses: actions/checkout@v3
4025
- name: Set up Python ${{ matrix.python-version }}
@@ -45,9 +30,20 @@ jobs:
4530
cache-dependency-path: |
4631
Pipfile
4732
setup.py
33+
- uses: isbang/compose-action@v1.4.1
34+
with:
35+
compose-file: "./docker-compose.yml"
36+
down-flags: "--remove-orphans"
37+
up-flags: "--no-start"
4838
- name: Install dependencies
4939
run: |
5040
make develop
41+
make services-up
42+
- name: Setup hostname
43+
run: |
44+
export CONTAINER_ID=$(docker-compose ps -q proxy)
45+
export CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_ID)
46+
echo "$CONTAINER_IP httpbin.local" | sudo tee -a /etc/hosts
5147
- name: Test
5248
run: |
5349
make test

Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ install-test-requirements:
88
pipenv install --dev
99
pipenv run python -c "import pipfile; pf = pipfile.load('Pipfile'); print('\n'.join(package+version for package, version in pf.data['default'].items()))" > requirements.txt
1010

11+
services-up:
12+
pipenv run docker-compose up -d
13+
14+
services-down:
15+
pipenv run docker-compose down --remove-orphans
16+
1117
test-python:
1218
@echo "Running Python tests"
13-
pipenv run python run_tests.py || exit 1
19+
pipenv run wait-for-it --service httpbin.local:443 --service localhost:6379 --timeout 5 -- pipenv run python run_tests.py || exit 1
1420
@echo ""
1521

1622
lint-python:
@@ -35,6 +41,7 @@ publish: install-test-requirements
3541

3642
clean:
3743
rm -rf *.egg-info dist/
38-
find . -type d -name __pycache__ -exec rm -rf {} \;
44+
find . -type d -name __pycache__ | xargs rf -rf
3945

40-
.PHONY: clean publish safetest test setup develop lint-python test-python install-test-requirements install-dev-requirements
46+
.PHONY: clean publish safetest test setup develop lint-python test-python
47+
.PHONY: services-up services-down install-test-requirements install-dev-requirements

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ wheel = "*"
2929
twine = "*"
3030
anaconda-client = "*"
3131
fastapi = "*"
32+
docker-compose = "*"
33+
wait-for-it = "*"
3234

3335
[requires]
3436
python_version = "3"

mocket/mocket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class MocketSocket:
164164
_secure_socket = False
165165

166166
def __init__(
167-
self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, *args, **kwargs
167+
self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, **kwargs
168168
):
169169
self.true_socket = true_socket(family, type, proto)
170170
self._buflen = 65536

mocket/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from .compat import decode_from_bytes, encode_to_bytes
77

8-
SSL_PROTOCOL = ssl.PROTOCOL_SSLv23
8+
SSL_PROTOCOL = ssl.PROTOCOL_TLSv1_2
99

1010

1111
class MocketSocketCore(io.BytesIO):

tests/main/test_http.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ def assertEqualHeaders(self, first, second, msg=None):
3030
class TrueHttpEntryTestCase(HttpTestCase):
3131
@mocketize
3232
def test_truesendall(self):
33-
url = "http://httpbin.org/ip"
33+
url = "http://httpbin.local/ip"
3434
resp = urlopen(url)
3535
self.assertEqual(resp.code, 200)
3636
resp = requests.get(url)
3737
self.assertEqual(resp.status_code, 200)
3838

3939
@mocketize(truesocket_recording_dir=recording_directory)
4040
def test_truesendall_with_recording(self):
41-
url = "http://httpbin.org/ip"
41+
url = "http://httpbin.local/ip"
4242

4343
urlopen(url)
4444
requests.get(url)
@@ -54,11 +54,11 @@ def test_truesendall_with_recording(self):
5454
with io.open(dump_filename) as f:
5555
responses = json.load(f)
5656

57-
self.assertEqual(len(responses["httpbin.org"]["80"].keys()), 2)
57+
self.assertEqual(len(responses["httpbin.local"]["80"].keys()), 2)
5858

5959
@mocketize(truesocket_recording_dir=recording_directory)
6060
def test_truesendall_with_gzip_recording(self):
61-
url = "http://httpbin.org/gzip"
61+
url = "http://httpbin.local/gzip"
6262

6363
requests.get(url)
6464
resp = requests.get(url)
@@ -70,11 +70,11 @@ def test_truesendall_with_gzip_recording(self):
7070
with io.open(dump_filename) as f:
7171
responses = json.load(f)
7272

73-
assert len(responses["httpbin.org"]["80"].keys()) == 1
73+
assert len(responses["httpbin.local"]["80"].keys()) == 1
7474

7575
@mocketize(truesocket_recording_dir=recording_directory)
7676
def test_truesendall_with_chunk_recording(self):
77-
url = "http://httpbin.org/range/70000?chunk_size=65536"
77+
url = "http://httpbin.local/range/70000?chunk_size=65536"
7878

7979
requests.get(url)
8080
resp = requests.get(url)
@@ -86,12 +86,14 @@ def test_truesendall_with_chunk_recording(self):
8686
with io.open(dump_filename) as f:
8787
responses = json.load(f)
8888

89-
assert len(responses["httpbin.org"]["80"].keys()) == 1
89+
assert len(responses["httpbin.local"]["80"].keys()) == 1
9090

9191
@mocketize
9292
def test_wrongpath_truesendall(self):
93-
Entry.register(Entry.GET, "http://httpbin.org/user.agent", Response(status=404))
94-
response = urlopen("http://httpbin.org/ip")
93+
Entry.register(
94+
Entry.GET, "http://httpbin.local/user.agent", Response(status=404)
95+
)
96+
response = urlopen("http://httpbin.local/ip")
9597
self.assertEqual(response.code, 200)
9698

9799

@@ -205,7 +207,7 @@ def test_mockhttp_entry_collect_duplicates(self):
205207

206208
@mocketize
207209
def test_multipart(self):
208-
url = "http://httpbin.org/post"
210+
url = "http://httpbin.local/post"
209211
data = '--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="content"\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 68\r\n\r\nAction: comment\nText: Comment with attach\nAttachment: x1.txt, x2.txt\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_2"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_1"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz--\r\n'
210212
headers = {
211213
"Content-Length": "495",
@@ -229,7 +231,7 @@ def test_multipart(self):
229231
"accept-encoding": "identity",
230232
"content-length": "495",
231233
"content-type": "multipart/form-data; boundary=xXXxXXyYYzzz",
232-
"host": "httpbin.org",
234+
"host": "httpbin.local",
233235
"user-agent": "Mocket",
234236
"connection": "keep-alive",
235237
},
@@ -300,9 +302,11 @@ def test_request_bodies(self):
300302

301303
@mocketize(truesocket_recording_dir=os.path.dirname(__file__))
302304
def test_truesendall_with_dump_from_recording(self):
303-
requests.get("http://httpbin.org/ip", headers={"user-agent": "Fake-User-Agent"})
304305
requests.get(
305-
"http://httpbin.org/gzip", headers={"user-agent": "Fake-User-Agent"}
306+
"http://httpbin.local/ip", headers={"user-agent": "Fake-User-Agent"}
307+
)
308+
requests.get(
309+
"http://httpbin.local/gzip", headers={"user-agent": "Fake-User-Agent"}
306310
)
307311

308312
dump_filename = os.path.join(
@@ -311,7 +315,7 @@ def test_truesendall_with_dump_from_recording(self):
311315
with io.open(dump_filename) as f:
312316
responses = json.load(f)
313317

314-
self.assertEqual(len(responses["httpbin.org"]["80"].keys()), 2)
318+
self.assertEqual(len(responses["httpbin.local"]["80"].keys()), 2)
315319

316320
@mocketize
317321
def test_post_file_object(self):

tests/main/test_http_with_xxhash.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@
66
import os
77

88
import requests
9-
from tests.main.test_http import HttpTestCase
109

1110
from mocket import Mocket, mocketize
11+
from tests.main.test_http import HttpTestCase
1212

1313

1414
class HttpEntryTestCase(HttpTestCase):
15-
1615
@mocketize(truesocket_recording_dir=os.path.dirname(__file__))
1716
def test_truesendall_with_dump_from_recording(self):
18-
requests.get('http://httpbin.org/ip', headers={"user-agent": "Fake-User-Agent"})
19-
requests.get('http://httpbin.org/gzip', headers={"user-agent": "Fake-User-Agent"})
17+
requests.get(
18+
"http://httpbin.local/ip", headers={"user-agent": "Fake-User-Agent"}
19+
)
20+
requests.get(
21+
"http://httpbin.local/gzip", headers={"user-agent": "Fake-User-Agent"}
22+
)
2023

2124
dump_filename = os.path.join(
2225
Mocket.get_truesocket_recording_dir(),
23-
Mocket.get_namespace() + '.json',
26+
Mocket.get_namespace() + ".json",
2427
)
2528
with io.open(dump_filename) as f:
2629
responses = json.load(f)
2730

28-
self.assertEqual(len(responses['httpbin.org']['80'].keys()), 2)
31+
self.assertEqual(len(responses["httpbin.local"]["80"].keys()), 2)

tests/main/test_httpretty.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ def test_httpretty_provides_easy_access_to_querystrings():
5454

5555
requests.get("http://yipit.com/?foo=bar&foo=baz&chuck=norris")
5656
expect(HTTPretty.last_request.querystring).to.equal(
57-
{"foo": ["bar", "baz"], "chuck": ["norris"],}
57+
{
58+
"foo": ["bar", "baz"],
59+
"chuck": ["norris"],
60+
}
5861
)
5962

6063

@@ -118,13 +121,19 @@ def test_httpretty_should_allow_forcing_headers_requests():
118121
HTTPretty.GET,
119122
"http://github.com/foo",
120123
body="<root><baz /</root>",
121-
forcing_headers={"Content-Type": "application/xml", "Content-Length": "19",},
124+
forcing_headers={
125+
"Content-Type": "application/xml",
126+
"Content-Length": "19",
127+
},
122128
)
123129

124130
response = requests.get("http://github.com/foo")
125131

126132
expect(dict(response.headers)).to.equal(
127-
{"content-type": "application/xml", "content-length": "19",}
133+
{
134+
"content-type": "application/xml",
135+
"content-length": "19",
136+
}
128137
)
129138

130139

@@ -195,12 +204,18 @@ def test_can_inspect_last_request():
195204
response = requests.post(
196205
"http://api.github.com",
197206
'{"username": "gabrielfalcao"}',
198-
headers={"content-type": "text/json",},
207+
headers={
208+
"content-type": "text/json",
209+
},
199210
)
200211

201212
expect(HTTPretty.last_request.method).to.equal("POST")
202-
expect(HTTPretty.last_request.body).to.equal(b'{"username": "gabrielfalcao"}',)
203-
expect(HTTPretty.last_request.headers["content-type"]).to.equal("text/json",)
213+
expect(HTTPretty.last_request.body).to.equal(
214+
b'{"username": "gabrielfalcao"}',
215+
)
216+
expect(HTTPretty.last_request.headers["content-type"]).to.equal(
217+
"text/json",
218+
)
204219
expect(response.json()).to.equal({"repositories": ["HTTPretty", "lettuce"]})
205220

206221

@@ -216,12 +231,18 @@ def test_can_inspect_last_request_with_ssl():
216231
response = requests.post(
217232
"https://secure.github.com",
218233
'{"username": "gabrielfalcao"}',
219-
headers={"content-type": "text/json",},
234+
headers={
235+
"content-type": "text/json",
236+
},
220237
)
221238

222239
expect(HTTPretty.last_request.method).to.equal("POST")
223-
expect(HTTPretty.last_request.body).to.equal(b'{"username": "gabrielfalcao"}',)
224-
expect(HTTPretty.last_request.headers["content-type"]).to.equal("text/json",)
240+
expect(HTTPretty.last_request.body).to.equal(
241+
b'{"username": "gabrielfalcao"}',
242+
)
243+
expect(HTTPretty.last_request.headers["content-type"]).to.equal(
244+
"text/json",
245+
)
225246
expect(response.json()).to.equal({"repositories": ["HTTPretty", "lettuce"]})
226247

227248

@@ -240,14 +261,15 @@ def test_httpretty_ignores_querystrings_from_registered_uri():
240261

241262
@httprettified
242263
def test_multiline():
243-
url = "http://httpbin.org/post"
264+
url = "http://httpbin.local/post"
244265
data = b"content=Im\r\na multiline\r\n\r\nsentence\r\n"
245266
headers = {
246267
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
247268
"Accept": "text/plain",
248269
}
249270
HTTPretty.register_uri(
250-
HTTPretty.POST, url,
271+
HTTPretty.POST,
272+
url,
251273
)
252274
response = requests.post(url, data=data, headers=headers)
253275

@@ -264,15 +286,16 @@ def test_multiline():
264286

265287
@httprettified
266288
def test_multipart():
267-
url = "http://httpbin.org/post"
289+
url = "http://httpbin.local/post"
268290
data = b'--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="content"\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 68\r\n\r\nAction: comment\nText: Comment with attach\nAttachment: x1.txt, x2.txt\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_2"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_1"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz--\r\n'
269291
headers = {
270292
"Content-Length": "495",
271293
"Content-Type": "multipart/form-data; boundary=xXXxXXyYYzzz",
272294
"Accept": "text/plain",
273295
}
274296
HTTPretty.register_uri(
275-
HTTPretty.POST, url,
297+
HTTPretty.POST,
298+
url,
276299
)
277300
response = requests.post(url, data=data, headers=headers)
278301
expect(response.status_code).to.equal(200)

tests/main/test_https.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_json(response):
4444
@pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
4545
@mocketize(truesocket_recording_dir=recording_directory)
4646
def test_truesendall_with_recording_https():
47-
url = "https://httpbin.org/ip"
47+
url = "https://httpbin.local/ip"
4848

4949
requests.get(url, headers={"Accept": "application/json"})
5050
resp = requests.get(url, headers={"Accept": "application/json"})
@@ -57,15 +57,15 @@ def test_truesendall_with_recording_https():
5757
with io.open(dump_filename) as f:
5858
responses = json.load(f)
5959

60-
assert len(responses["httpbin.org"]["443"].keys()) == 1
60+
assert len(responses["httpbin.local"]["443"].keys()) == 1
6161

6262

6363
@pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
6464
def test_truesendall_after_mocket_session():
6565
Mocket.enable()
6666
Mocket.disable()
6767

68-
url = "https://httpbin.org/ip"
68+
url = "https://httpbin.local/ip"
6969
resp = requests.get(url)
7070
assert resp.status_code == 200
7171

@@ -74,8 +74,8 @@ def test_truesendall_after_mocket_session():
7474
def test_real_request_session():
7575
session = requests.Session()
7676

77-
url1 = "https://httpbin.org/ip"
78-
url2 = "http://httpbin.org/headers"
77+
url1 = "https://httpbin.local/ip"
78+
url2 = "http://httpbin.local/headers"
7979

8080
with Mocketizer():
8181
assert len(session.get(url1).content) < len(session.get(url2).content)

0 commit comments

Comments
 (0)