コード例 #1
0
ファイル: test_volume.py プロジェクト: dxenes1/intern
    def test_get_ids_in_region_success(self, mock_session, mock_resp):
        resolution = 0
        x_range = [0, 100]
        y_range = [10, 50]
        z_range = [20, 42]
        t_range = [0, 1]

        url_prefix = 'https://api.theboss.io'
        auth = 'mytoken'
        send_opts = {}

        fake_prepped_req = PreparedRequest()
        fake_prepped_req.headers = {}
        mock_session.prepare_request.return_value = fake_prepped_req
        mock_session.send.return_value = mock_resp

        mock_resp.status_code = 200
        mock_resp.json.return_value = {'ids': ['1', '10']}

        actual = self.vol.get_ids_in_region(self.anno_chan, resolution,
                                            x_range, y_range, z_range, t_range,
                                            url_prefix, auth, mock_session,
                                            send_opts)

        expected = [1, 10]

        self.assertEqual(expected, actual)
コード例 #2
0
ファイル: client.py プロジェクト: MarkkuPekkarinen/httpie
def apply_missing_repeated_headers(
    original_headers: HTTPHeadersDict,
    prepared_request: requests.PreparedRequest
) -> None:
    """Update the given `prepared_request`'s headers with the original
    ones. This allows the requests to be prepared as usual, and then later
    merged with headers that are specified multiple times."""

    new_headers = HTTPHeadersDict(prepared_request.headers)
    for prepared_name, prepared_value in prepared_request.headers.items():
        if prepared_name not in original_headers:
            continue

        original_keys, original_values = zip(*filter(
            lambda item: item[0].casefold() == prepared_name.casefold(),
            original_headers.items()
        ))

        if prepared_value not in original_values:
            # If the current value is not among the initial values
            # set for this field, then it means that this field got
            # overridden on the way, and we should preserve it.
            continue

        new_headers.popone(prepared_name)
        new_headers.update(zip(original_keys, original_values))

    prepared_request.headers = new_headers
コード例 #3
0
ファイル: test_volume.py プロジェクト: dxenes1/intern
    def test_get_cutout_success(self, mock_session):
        resolution = 0
        x_range = [20, 40]
        y_range = [50, 70]
        z_range = [30, 50]
        time_range = [10, 25]
        id_list = []
        url_prefix = 'https://api.theboss.io'
        auth = 'mytoken'

        fake_prepped_req = PreparedRequest()
        fake_prepped_req.headers = {}
        mock_session.prepare_request.return_value = fake_prepped_req

        data = numpy.random.randint(0, 3000, (15, 20, 20, 20), numpy.uint16)
        compressed_data = blosc.compress(data, typesize=16)
        fake_response = Response()
        fake_response.status_code = 200
        fake_response._content = compressed_data
        mock_session.send.return_value = fake_response
        send_opts = {}

        actual = self.vol.get_cutout(self.chan, resolution, x_range, y_range,
                                     z_range, time_range, id_list, url_prefix,
                                     auth, mock_session, send_opts)

        numpy.testing.assert_array_equal(data, actual)
コード例 #4
0
ファイル: test_volume.py プロジェクト: dxenes1/intern
    def test_get_bounding_box_success(self, mock_session, mock_resp):
        resolution = 0
        id = 44444
        bb_type = 'loose'

        url_prefix = 'https://api.theboss.io'
        auth = 'mytoken'
        send_opts = {}

        fake_prepped_req = PreparedRequest()
        fake_prepped_req.headers = {}
        mock_session.prepare_request.return_value = fake_prepped_req
        mock_session.send.return_value = mock_resp

        mock_resp.status_code = 200
        mock_resp.json.return_value = expected = {
            'x_range': [0, 10],
            'y_range': [0, 10],
            'z_range': [0, 10],
            't_range': [0, 10]
        }

        actual = self.vol.get_bounding_box(self.anno_chan, resolution, id,
                                           bb_type, url_prefix, auth,
                                           mock_session, send_opts)

        self.assertEqual(expected, actual)
コード例 #5
0
    def add_auth(self,
                 r: requests.PreparedRequest) -> requests.PreparedRequest:
        rr = r.copy()
        url = urlparse(r.url)

        if 'Host' in r.headers:
            netloc = r.headers['Host'].decode('utf-8')
        else:
            netloc = url.netloc

        rr.url = urlunparse((url.scheme, netloc, url.path, url.params,
                             url.query, url.fragment))

        if r.method == 'POST':
            if r.body:
                if isinstance(r.body, bytes):
                    body = dict(parse_qsl(r.body.decode("utf-8").strip()))
                elif isinstance(r.body, str):
                    body = dict(parse_qsl(r.body.strip()))
                r.body = urlencode(self.update_params(rr, body))
                new_headers = r.headers
                new_headers[
                    'Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
                r.headers = new_headers

        elif r.method == 'GET':
            url = urlparse(r.url)
            if url.query:
                new_query = urlencode(
                    self.update_params(rr, dict(parse_qsl(url.query))))
                r.url = urlunparse((url.scheme, url.netloc, url.path,
                                    url.params, new_query, url.fragment))

        return r
コード例 #6
0
ファイル: test_volume.py プロジェクト: jhuapl-boss/ndio
    def test_cutout_get_success(self, mock_session):
        resolution = 0
        x_range = '20:40'
        y_range = '50:70'
        z_range = '30:50'
        time_range = '10:25'
        url_prefix = 'https://api.theboss.io'
        auth = 'mytoken'

        fake_prepped_req = PreparedRequest()
        fake_prepped_req.headers = {}
        mock_session.prepare_request.return_value = fake_prepped_req 

        data = numpy.random.randint(0, 3000, (15, 20, 20, 20), numpy.uint16)
        compressed_data = blosc.pack_array(data)
        fake_response = Response()
        fake_response.status_code = 200
        fake_response._content = compressed_data
        mock_session.send.return_value = fake_response
        send_opts = {}

        actual = self.vol.cutout_get(
            self.chan, resolution, x_range, y_range, z_range, time_range,
            url_prefix, auth, mock_session, send_opts)

        numpy.testing.assert_array_equal(data, actual)
コード例 #7
0
def test_write_failure_event_to_db():
    ServiceMonitor.create_table(wait=True)
    monitor = WriteToDynamoMonitor()
    request = PreparedRequest()
    request.body = "The body of request"
    request.headers = {"bill": "Some bill"}
    response = Response()
    response.status_code = 500
    response.headers['ben'] = "Some Ben"
    response.data = "The response text"
    result = monitor.failure("some-service", request, response, 1108)
    assert result.id is not None
    fetched_result = ServiceMonitor.query(result.service_name, ServiceMonitor.id == result.id).next()
    assert fetched_result.id == result.id
    assert fetched_result.event_type == "FAILED"
    assert fetched_result.timestamp == result.timestamp
    assert fetched_result.ttl == result.ttl
コード例 #8
0
ファイル: test_volume.py プロジェクト: jhuapl-boss/ndio
    def test_cutout_get_failure(self, mock_session):
        resolution = 0
        x_range = '20:40'
        y_range = '50:70'
        z_range = '30:50'
        time_range = '10:25'
        url_prefix = 'https://api.theboss.io'
        auth = 'mytoken'

        fake_prepped_req = PreparedRequest()
        fake_prepped_req.headers = {}
        mock_session.prepare_request.return_value = fake_prepped_req 
        fake_response = Response()
        fake_response.status_code = 403
        mock_session.send.return_value = fake_response
        send_opts = {}

        with self.assertRaises(HTTPError):
            actual = self.vol.cutout_get(
                self.chan, resolution, x_range, y_range, z_range, time_range,
                url_prefix, auth, mock_session, send_opts)
コード例 #9
0
ファイル: test_volume.py プロジェクト: dxenes1/intern
    def test_get_cutout_failure(self, mock_session):
        resolution = 0
        x_range = [20, 40]
        y_range = [50, 70]
        z_range = [30, 50]
        time_range = [10, 25]
        id_list = []
        url_prefix = 'https://api.theboss.io'
        auth = 'mytoken'

        fake_prepped_req = PreparedRequest()
        fake_prepped_req.headers = {}
        mock_session.prepare_request.return_value = fake_prepped_req
        fake_response = Response()
        fake_response.status_code = 403
        mock_session.send.return_value = fake_response
        send_opts = {}

        with self.assertRaises(HTTPError):
            actual = self.vol.get_cutout(self.chan, resolution, x_range,
                                         y_range, z_range, time_range, id_list,
                                         url_prefix, auth, mock_session,
                                         send_opts)
コード例 #10
0
ファイル: test_volume.py プロジェクト: dxenes1/intern
    def test_get_ids_in_region_failure(self, mock_session):
        resolution = 0
        x_range = [0, 100]
        y_range = [10, 50]
        z_range = [20, 42]
        t_range = [0, 1]

        url_prefix = 'https://api.theboss.io'
        auth = 'mytoken'
        send_opts = {}

        fake_prepped_req = PreparedRequest()
        fake_prepped_req.headers = {}
        mock_session.prepare_request.return_value = fake_prepped_req
        fake_response = Response()
        fake_response.status_code = 403
        mock_session.send.return_value = fake_response
        send_opts = {}

        with self.assertRaises(HTTPError):
            actual = self.vol.get_ids_in_region(self.anno_chan, resolution,
                                                x_range, y_range, z_range,
                                                t_range, url_prefix, auth,
                                                mock_session, send_opts)
コード例 #11
0
    def _extract_response(page, encoding='utf8'):
        history = []
        set_cookies = []
        res = None
        try:
            for i, url in enumerate(page['history']):
                resource = page['resources'].pop(0)
                while resource['request']['url'] != url:
                    resource = page['resources'].pop(0)

                if resource['error']:
                    return resource['error'], None

                request = resource['request']
                req = PreparedRequest()
                req.method = request['method'].encode(encoding)
                req.url = request['url'].encode(encoding)

                # Set Request Headers
                req.headers = CaseInsensitiveDict()
                for header in request['headers']:
                    req.headers[header['name'].encode(encoding)] = header['value'].encode(encoding)

                # Set Request Cookies
                req._cookies = RequestsCookieJar()
                if set_cookies:
                    if 'Cookie' not in req.headers:
                        req.headers['Cookie'] = ""
                    else:
                        set_cookies.insert(0, '')
                    req.headers['Cookie'] += "; ".join(set_cookies)

                if 'Cookie' in req.headers:
                    cookies = SimpleCookie()
                    cookies.load(req.headers['Cookie'])
                    for key, cookie in cookies.items():
                        req._cookies.set(key, cookie.value)

                req.body = request.get('postData', None)
                if req.body:
                    req.body = req.body.encode(encoding)

                response = resource['endReply'] or resource['startReply']
                res = Response()
                res.encoding = encoding
                res.url = response['url'].encode(encoding)
                res.status_code = response['status']
                for header in response['headers']:
                    res.headers[header['name'].encode(encoding)] = header['value'].encode(encoding)
                    if header['name'] == 'Set-Cookie':
                        set_cookies.append(res.headers[header['name']].rsplit(';', 1)[0])

                res.history = list(history)
                res.request = req

                history.append(res)

            res._content = re.sub(
                (
                    '<html><head></head><body>'
                    '<pre style="word-wrap: break-word; white-space: pre-wrap;">(.*?)</pre>'
                    '</body></html>'
                ),
                r'\1',
                page['content'],
                flags=re.DOTALL
            ).encode(encoding)

            return None, res
        except IndexError:
            return {'errorCode': -1,
                    'errorString': 'An existing connection was forcibly closed by the remote host'}, None
コード例 #12
0
def multipart_matcher(
        files: Dict[str, Any],
        data: Optional[Dict[str, str]] = None) -> Callable[..., Any]:
    """
    Matcher to match 'multipart/form-data' content-type.
    This function constructs request body and headers from provided 'data' and 'files'
    arguments and compares to actual request

    :param files: (dict), same as provided to request
    :param data: (dict), same as provided to request
    :return: (func) matcher
    """
    if not files:
        raise TypeError("files argument cannot be empty")

    prepared = PreparedRequest()
    prepared.headers = {"Content-Type": ""}  # type: ignore[assignment]
    prepared.prepare_body(data=data, files=files)

    def get_boundary(content_type: str) -> str:
        """
        Parse 'boundary' value from header.

        :param content_type: (str) headers["Content-Type"] value
        :return: (str) boundary value
        """
        if "boundary=" not in content_type:
            return ""

        return content_type.split("boundary=")[1]

    def match(request: PreparedRequest) -> Tuple[bool, str]:
        reason = "multipart/form-data doesn't match. "
        if "Content-Type" not in request.headers:
            return False, reason + "Request is missing the 'Content-Type' header"

        request_boundary = get_boundary(request.headers["Content-Type"])
        prepared_boundary = get_boundary(prepared.headers["Content-Type"])

        # replace boundary value in header and in body, since by default
        # urllib3.filepost.encode_multipart_formdata dynamically calculates
        # random boundary alphanumeric value
        request_content_type = request.headers["Content-Type"]
        prepared_content_type = prepared.headers["Content-Type"].replace(
            prepared_boundary, request_boundary)

        request_body = request.body
        prepared_body = prepared.body or ""

        if isinstance(prepared_body, bytes):
            # since headers always come as str, need to convert to bytes
            prepared_boundary = prepared_boundary.encode(
                "utf-8")  # type: ignore[assignment]
            request_boundary = request_boundary.encode(
                "utf-8")  # type: ignore[assignment]

        prepared_body = prepared_body.replace(
            prepared_boundary,
            request_boundary  # type: ignore[arg-type]
        )

        headers_valid = prepared_content_type == request_content_type
        if not headers_valid:
            return (
                False,
                reason +
                "Request headers['Content-Type'] is different. {} isn't equal to {}"
                .format(request_content_type, prepared_content_type),
            )

        body_valid = prepared_body == request_body
        if not body_valid:
            return (
                False,
                reason + "Request body differs. {} aren't equal {}".
                format(  # type: ignore[str-bytes-safe]
                    request_body, prepared_body),
            )

        return True, ""

    return match