forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_connection.py
68 lines (54 loc) · 2.5 KB
/
test_connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
from logging import getLogger
from tempfile import NamedTemporaryFile
from unittest import TestCase
import warnings
import pytest
from requests import HTTPError
from conda.common.compat import ensure_binary, PY3
from conda.common.url import path_to_url
from conda.gateways.anaconda_client import remove_binstar_token, set_binstar_token
from conda.gateways.connection.session import CondaHttpAuth, CondaSession
from conda.gateways.disk.delete import rm_rf
log = getLogger(__name__)
class CondaHttpAuthTests(TestCase):
def test_add_binstar_token(self):
try:
# token already exists in url, don't add anything
url = "https://conda.anaconda.org/t/dont-add-a-token/biopython/linux-64/repodata.json"
assert CondaHttpAuth.add_binstar_token(url) == url
# even if a token is there, don't use it
set_binstar_token("https://api.anaconda.test", "tk-abacadaba-1029384756")
url = "https://conda.anaconda.test/t/dont-add-a-token/biopython/linux-64/repodata.json"
assert CondaHttpAuth.add_binstar_token(url) == url
# now test adding the token
url = "https://conda.anaconda.test/biopython/linux-64/repodata.json"
new_url = "https://conda.anaconda.test/t/tk-abacadaba-1029384756/biopython/linux-64/repodata.json"
assert CondaHttpAuth.add_binstar_token(url) == new_url
finally:
remove_binstar_token("https://api.anaconda.test")
class CondaSessionTests(TestCase):
def test_local_file_adapter_404(self):
session = CondaSession()
test_path = 'file:///some/location/doesnt/exist'
r = session.get(test_path)
with pytest.raises(HTTPError) as exc:
r.raise_for_status()
assert r.status_code == 404
assert r.json()['path'] == test_path[len('file://'):]
def test_local_file_adapter_200(self):
test_path = None
try:
with NamedTemporaryFile(delete=False) as fh:
test_path = fh.name
fh.write(ensure_binary('{"content": "file content"}'))
test_url = path_to_url(test_path)
session = CondaSession()
r = session.get(test_url)
r.raise_for_status()
assert r.status_code == 200
assert r.json()['content'] == "file content"
finally:
if test_path is not None:
rm_rf(test_path)