def test_content_multipart_form_data_serialization(self):
     payload = dict(some_null=None,
                    some_bool=True,
                    some_str='a',
                    some_int=1,
                    some_float=3.14,
                    some_list=[],
                    some_dict={},
                    some_bytes=b'abc')
     request_body = api_client.RequestBody(
         content={
             'multipart/form-data':
             api_client.MediaType(schema=schemas.AnyTypeSchema)
         })
     serialization = request_body.serialize(payload, 'multipart/form-data')
     self.assertEqual(
         serialization,
         dict(fields=(
             api_client.RequestField(
                 name='some_null',
                 data='null',
                 headers={'Content-Type': 'application/json'}),
             api_client.RequestField(
                 name='some_bool',
                 data='true',
                 headers={'Content-Type': 'application/json'}),
             api_client.RequestField(name='some_str',
                                     data='a',
                                     headers={'Content-Type': 'text/plain'
                                              }),
             api_client.RequestField(
                 name='some_int',
                 data='1',
                 headers={'Content-Type': 'application/json'}),
             api_client.RequestField(
                 name='some_float',
                 data='3.14',
                 headers={'Content-Type': 'application/json'}),
             api_client.RequestField(
                 name='some_list',
                 data='[]',
                 headers={'Content-Type': 'application/json'}),
             api_client.RequestField(
                 name='some_dict',
                 data='{}',
                 headers={'Content-Type': 'application/json'}),
             api_client.RequestField(
                 name='some_bytes',
                 data=b'abc',
                 headers={'Content-Type': 'application/octet-stream'}))))
    def test_upload_files(self):
        """Test case for upload_files
        uploads files using multipart/form-data  # noqa: E501
        """
        import os
        test_file_dir = os.path.realpath(
            os.path.join(os.path.dirname(__file__), "..", "testfiles"))
        file_name = '1px_pic1.png'
        file_path1 = os.path.join(test_file_dir, file_name)

        with open(file_path1, "rb") as some_file:
            file_bytes = some_file.read()
        file1 = open(file_path1, "rb")
        response_json = {
            'code': 200,
            'type': 'blah',
            'message': 'file upload succeeded'
        }
        try:
            with patch.object(RESTClientObject, 'request') as mock_request:
                mock_request.return_value = self.__response(
                    self.__json_bytes(response_json))
                api_response = self.api.upload_files(
                    body={'files': [file1, file1]})
                self.__assert_request_called_with(
                    mock_request,
                    'http://petstore.swagger.io:80/v2/fake/uploadFiles',
                    fields=(
                        api_client.RequestField(name='files',
                                                data=file_bytes,
                                                filename=file_name,
                                                headers={
                                                    'Content-Type':
                                                    'application/octet-stream'
                                                }),
                        api_client.RequestField(name='files',
                                                data=file_bytes,
                                                filename=file_name,
                                                headers={
                                                    'Content-Type':
                                                    'application/octet-stream'
                                                }),
                    ),
                    content_type='multipart/form-data')
                self.assertEqual(api_response.body, response_json)
        except petstore_api.ApiException as e:
            self.fail("upload_file() raised {0} unexpectedly".format(type(e)))
        finally:
            file1.close()

        # sending just bytes works also
        with patch.object(RESTClientObject, 'request') as mock_request:
            mock_request.return_value = self.__response(
                self.__json_bytes(response_json))
            api_response = self.api.upload_files(
                body={'files': [file_bytes, file_bytes]})
            self.__assert_request_called_with(
                mock_request,
                'http://petstore.swagger.io:80/v2/fake/uploadFiles',
                fields=(
                    api_client.RequestField(
                        name='files',
                        data=file_bytes,
                        headers={'Content-Type': 'application/octet-stream'}),
                    api_client.RequestField(
                        name='files',
                        data=file_bytes,
                        headers={'Content-Type': 'application/octet-stream'}),
                ),
                content_type='multipart/form-data')
            self.assertEqual(api_response.body, response_json)
Example #3
0
    def test_inline_composition(self, mock_request):
        """Test case for inline_composition

        testing composed schemas at inline locations  # noqa: E501
        """
        single_char_str = 'a'
        json_bytes = self.__json_bytes(single_char_str)

        # tx and rx json with composition at root level of schema for request + response body
        content_type = 'application/json'
        mock_request.return_value = self.__response(
            json_bytes
        )
        api_response = self.api.inline_composition(
            body=single_char_str,
            query_params={
                'compositionAtRoot': single_char_str,
                'compositionInProperty': {'someProp': single_char_str}
            },
            accept_content_types=(content_type,)
        )
        self.__assert_request_called_with(
            mock_request,
            'http://petstore.swagger.io:80/v2/fake/inlineComposition/',
            accept_content_type=content_type,
            content_type=content_type,
            query_params=(('compositionAtRoot', 'a'), ('someProp', 'a')),
            body=json_bytes
        )
        self.assertEqual(api_response.body, single_char_str)
        self.assertTrue(isinstance(api_response.body, schemas.StrSchema))

        # tx and rx json with composition at property level of schema for request + response body
        content_type = 'multipart/form-data'
        multipart_response = self.__encode_multipart_formdata(fields={'someProp': single_char_str})
        mock_request.return_value = self.__response(
            bytes(multipart_response),
            content_type=multipart_response.get_content_type()
        )
        api_response = self.api.inline_composition(
            body={'someProp': single_char_str},
            query_params={
                'compositionAtRoot': single_char_str,
                'compositionInProperty': {'someProp': single_char_str}
            },
            content_type=content_type,
            accept_content_types=(content_type,)
        )
        self.__assert_request_called_with(
            mock_request,
            'http://petstore.swagger.io:80/v2/fake/inlineComposition/',
            accept_content_type=content_type,
            content_type=content_type,
            query_params=(('compositionAtRoot', 'a'), ('someProp', 'a')),
            fields=(
                api_client.RequestField(
                    name='someProp',
                    data=single_char_str,
                    headers={'Content-Type': 'text/plain'}
                ),
            ),
        )
        self.assertEqual(api_response.body, {'someProp': single_char_str})
        self.assertTrue(isinstance(api_response.body.someProp, schemas.StrSchema))

        # error thrown when a str is input which doesn't meet the composed schema length constraint
        invalid_value = ''
        variable_locations = 4
        for invalid_index in range(variable_locations):
            values = [single_char_str]*variable_locations
            values[invalid_index] = invalid_value
            with self.assertRaises(exceptions.ApiValueError):
                multipart_response = self.__encode_multipart_formdata(fields={'someProp': values[0]})
                mock_request.return_value = self.__response(
                    bytes(multipart_response),
                    content_type=multipart_response.get_content_type()
                )
                self.api.inline_composition(
                    body={'someProp': values[1]},
                    query_params={
                        'compositionAtRoot': values[2],
                        'compositionInProperty': {'someProp': values[3]}
                    },
                    content_type=content_type,
                    accept_content_types=(content_type,)
                )