Exemple #1
0
def get_mockreturn(self,
                   method,
                   url,
                   params=None,
                   data=None,
                   headers=None,
                   cookies=None,
                   files=None,
                   auth=None,
                   timeout=None,
                   allow_redirects=True,
                   proxies=None,
                   hooks=None,
                   stream=None,
                   verify=None,
                   cert=None,
                   json=None):
    filename = data_path(DATA_FILES[(url, params
                                     and frozenset(params.items()))])
    content = open(filename, 'rb').read()
    return MockResponse(content,
                        url=url,
                        headers=headers,
                        stream=stream,
                        auth=auth)
Exemple #2
0
def nonremote_request(self, url, **kwargs):

    targetname = kwargs['params']['sstr']

    with open(data_path(DATA_FILES[targetname]), 'rb') as f:
        response = MockResponse(content=f.read(), url=url)
    return response
Exemple #3
0
def get_mockreturn(method,
                   url,
                   params=None,
                   timeout=10,
                   cache=False,
                   **kwargs):
    filename = data_path(DATA_FILES[params['spatial']])
    content = open(filename, 'rb').read()
    return MockResponse(content, **kwargs)
Exemple #4
0
def get_mockreturn(url, params=None, stream=False, timeout=10, **kwargs):
    if stream:
        filename = data_path(DATA_FILES['img'])
        return MockResponse(open(filename, 'rb').read(),
                            content_type='image/fits',
                            **kwargs)
    elif params['RA'] == 163.6136:
        filename = data_path(DATA_FILES['pos_t'])
    elif params['NAIFID'] == 2003226:
        filename = data_path(DATA_FILES['nid_t'])
    elif params['PID'] == 30080:
        filename = data_path(DATA_FILES['pid_t'])
    elif params['REQKEY'] == 21641216:
        filename = data_path(DATA_FILES['rqk_t'])
    else:
        raise ValueError("Query not pre-loaded.")

    content = open(filename, 'rb').read()
    return MockResponse(content, **kwargs)
Exemple #5
0
def get_mockreturn(method, url, params=None, timeout=10, **kwargs):
    search_type = params.get('search_type')
    if search_type is not None:
        filename = data_path(DATA_FILES[search_type])
    elif 'imgdata' in url:
        filename = data_path(DATA_FILES['extract_urls'])
    else:
        filename = data_path(DATA_FILES['object'])
    content = open(filename, "rb").read()
    return MockResponse(content, **kwargs)
Exemple #6
0
def test_parse_result(capsys):
    content = open(data_path(DATA_FILES['error']), 'rb').read()
    response = MockResponse(content)
    with pytest.raises(RemoteServiceError) as exinfo:
        ned.core.Ned._parse_result(response)
    if hasattr(exinfo.value, 'message'):
        assert exinfo.value.message == ("The remote service returned the "
                                        "following error message.\nERROR:  "
                                        "No note found.")
    else:
        assert exinfo.value.args == ("The remote service returned the "
                                     "following error message.\nERROR:  "
                                     "No note found.", )
Exemple #7
0
def mock_get(self, method, url, *args, **kwargs):  # pragma: nocover
    assert url == conf.url_api

    params = kwargs.get("params", None)
    assert params is not None

    try:
        with open(RESPONSE_FILE, "r") as f:
            responses = json.load(f)
    except FileNotFoundError:
        responses = {}

    # Work out where the expected response is saved
    table = params["table"]
    key = urlencode(sorted(params.items()))
    try:
        index = responses.get(table, []).index(key)
    except ValueError:
        index = -1

    # If the NASA_EXOPLANET_ARCHIVE_GENERATE_RESPONSES environment variable is set, we make a
    # remote request if necessary. Otherwise we throw a ValueError.
    if index < 0:
        if "NASA_EXOPLANET_ARCHIVE_GENERATE_RESPONSES" not in os.environ:
            raise ValueError("unexpected request")
        with requests.Session() as session:
            resp = session.old_request(method, url, params=params)
        responses[table] = responses.get(table, [])
        responses[table].append(key)
        index = len(responses[table]) - 1
        with open(
                os.path.join(TEST_DATA,
                             "{0}_expect_{1}.txt".format(table, index)),
                "w") as f:
            f.write(resp.text)
        with open(RESPONSE_FILE, "w") as f:
            json.dump(responses, f, sort_keys=True, indent=2)

    with open(
            os.path.join(TEST_DATA, "{0}_expect_{1}.txt".format(table, index)),
            "r") as f:
        data = f.read()

    return MockResponse(data.encode("utf-8"))
Exemple #8
0
def nonremote_request(self, url, **kwargs):

    with open(data_path(DATA_FILE), 'rb') as f:
        response = MockResponse(content=f.read(), url=url)
    return response