def test_rest_client_head(self, mock_call):
        """Test head"""

        auth = mock.create_autospec(Credentials)
        val = rest_client.head(auth, "http://test", {})
        mock_call.assert_called_with(auth, 'HEAD', "http://test", headers={})

        with self.assertRaises(RestCallException):
            rest_client.head(auth, "http://test/{0}", {})

        val = rest_client.head(auth, "http://test/{name}", {})
        mock_call.assert_called_with(auth, 'HEAD', "http://test/", headers={})

        val = rest_client.head(auth,
                               "http://test/{name}",
                               {},
                               filename="test file.jpg")

        mock_call.assert_called_with(auth,
                                     'HEAD',
                                     "http://test/test%20file.jpg",
                                     headers={})

        mock_call.return_value.headers = {}
        with self.assertRaises(RestCallException):
            rest_client.head(auth, "http://test", {})

        mock_call.return_value.headers = {"content-length":"10"}
        val = rest_client.head(auth, "http://test", {})
        self.assertEqual(val, 10)

        mock_call.side_effect = RestCallException(None, "Boom!", None)
        with self.assertRaises(RestCallException):
            rest_client.head(auth, "http://test", {})
    def test_rest_client_head(self, mock_call):
        """Test head"""

        auth = mock.create_autospec(Credentials)
        val = rest_client.head(auth, "http://test", {})
        mock_call.assert_called_with(auth, 'HEAD', "http://test", headers={})

        with self.assertRaises(RestCallException):
            rest_client.head(auth, "http://test/{0}", {})

        val = rest_client.head(auth, "http://test/{name}", {})
        mock_call.assert_called_with(auth, 'HEAD', "http://test/", headers={})

        val = rest_client.head(auth,
                               "http://test/{name}", {},
                               filename="test file.jpg")

        mock_call.assert_called_with(auth,
                                     'HEAD',
                                     "http://test/test%20file.jpg",
                                     headers={})

        mock_call.return_value.headers = {}
        with self.assertRaises(RestCallException):
            rest_client.head(auth, "http://test", {})

        mock_call.return_value.headers = {"content-length": "10"}
        val = rest_client.head(auth, "http://test", {})
        self.assertEqual(val, 10)

        mock_call.side_effect = RestCallException(None, "Boom!", None)
        with self.assertRaises(RestCallException):
            rest_client.head(auth, "http://test", {})
Ejemplo n.º 3
0
    def props_file(self, userfile):
        """Gets the properties of a file previously uploaded by the user.

        :Args:
            - userfile (:class:`.UserFile`): The userfile reference for the
              file to be checked. Could be generated from a
              :meth:`.FileManager.list_files()` call or file query.
              Must have 'url' attr.

        :Returns:
            - :class:`.Response` with the requested file size in bytes (int) if
              the call was successful.
            - :class:`.Response` with :class:`.RestCallException` if the
              download failed.
        """
        if not isinstance(userfile, UserFile):
            return Response(False, RestCallException(TypeError, "Not a valid UserFile", None))

        self._log.debug("props_file, file={0}".format(userfile))
        url = userfile.url
        try:
            head_resp = rest_client.head(self._auth, url, self.headers)

        except RestCallException as exp:
            return Response(False, exp)

        else:
            return Response(True, head_resp)
Ejemplo n.º 4
0
    def props_output(self, job_id=None, otype="output", url=None):
        """
        Gets the properties of the job output or preview.
        Used to obtain the size of the final job output or its thumbnail,
        which is returned in the response Content Length header.
        Either ``url``, or both ``job_id`` and ``otype`` must be set (although
        ``otype`` is already set by default).
        If all three are set, URL will be used.

        :Kwargs:
            - job_id (str): The ID of the job whose output will be checked.
              The default is None.
            - otype (str): The type of output to be checked, must be a
              string in ``['output', 'preview']``.
            - url (str): The URL directly to the file to be checked. If
              supplied, ``job_id`` and ``otype`` will not be used.
              The default is None.

        :Returns:
            - :class:`.Response` with the requested output size in bytes (int)
              if the call was successful.
            - :class:`.Response` with :exc:`AttributeError` if the correct URL
              arguments are not supplied.
            - :class:`.Response` with :class:`.RestCallException` if the
              download failed.
        """
        self._log.debug("props_output, job_id={0}, " "otype={1}, url={2}".format(job_id, otype, url))

        if not url and job_id:
            if otype not in ["output", "preview"]:
                return Response(False, RestCallException(ValueError, "output type must be 'output' or 'preview'", None))

            url = self.url("jobs/{jobid}/outputs/{type}").format(jobid=job_id, type=otype)

        elif not url and not job_id:
            return Response(False, RestCallException(AttributeError, "Either job_id or url must be set", None))

        try:
            head_resp = rest_client.head(self._auth, url, self.headers)

        except RestCallException as exp:
            return Response(False, exp)

        else:
            return Response(True, head_resp)
Ejemplo n.º 5
0
    def props_output_file(self, job_id=None, fname=None, url=None):
        """
        Get the file size of a given task output.
        Used to obtain the size of the requested file, which is returned in
        the response Content Length header. Either ``url``, or both ``job_id``
        and ``fname`` must be set. If all three are set, job_id & fname will
        be used.

        :Kwargs:
            - job_id (str): The ID of the job whose output will be checked.
              The default is None.
            - fname (str): The name of the output file to be downloaded.
            - url (str): The URL directly to the file to be checked.
              The default is None.

        :Returns:
            - :class:`.Response` with the requested output size in bytes (int)
              if the call was successful.
            - :class:`.Response` with :exc:`AttributeError` if the correct
              URL arguments are not supplied.
            - :class:`.Response` with :class:`.RestCallException` if the
              download failed.
        """
        self._log.debug("props_output_file, job_id={0}, " "fname={1}, url={2}".format(job_id, fname, url))
        if job_id and fname:
            url = self.url("jobs/{jobid}/outputs/files/{name}")
            url = url.format(jobid=job_id, name=fname)

        elif not url:
            return Response(
                False, RestCallException(AttributeError, "Either output url or job id and filename required.", None)
            )

        try:
            head_resp = rest_client.head(self._auth, url, self.headers)

        except RestCallException as exp:
            return Response(False, exp)

        else:
            return Response(True, head_resp)