Best Python code snippet using tempest_python
test_object_formpost.py
Source:test_object_formpost.py  
...38        cls.account_client.delete_account_metadata(metadata=cls.metadata)39        cls.delete_containers(cls.containers)40        cls.data.teardown_all()41        super(ObjectFormPostTest, cls).tearDownClass()42    def get_multipart_form(self, expires=600):43        path = "%s/%s/%s" % (44            urlparse.urlparse(self.container_client.base_url).path,45            self.container_name,46            self.object_name)47        redirect = ''48        max_file_size = 10485760049        max_file_count = 1050        expires += int(time.time())51        hmac_body = '%s\n%s\n%s\n%s\n%s' % (path,52                                            redirect,53                                            max_file_size,54                                            max_file_count,55                                            expires)56        signature = hmac.new(self.key, hmac_body, hashlib.sha1).hexdigest()57        fields = {'redirect': redirect,58                  'max_file_size': str(max_file_size),59                  'max_file_count': str(max_file_count),60                  'expires': str(expires),61                  'signature': signature}62        boundary = '--boundary--'63        data = []64        for (key, value) in fields.items():65            data.append('--' + boundary)66            data.append('Content-Disposition: form-data; name="%s"' % key)67            data.append('')68            data.append(value)69        data.append('--' + boundary)70        data.append('Content-Disposition: form-data; '71                    'name="file1"; filename="testfile"')72        data.append('Content-Type: application/octet-stream')73        data.append('')74        data.append('hello world')75        data.append('--' + boundary + '--')76        data.append('')77        body = '\r\n'.join(data)78        content_type = 'multipart/form-data; boundary=%s' % boundary79        return body, content_type80    @attr(type='gate')81    def test_post_object_using_form(self):82        body, content_type = self.get_multipart_form()83        headers = {'Content-Type': content_type,84                   'Content-Length': str(len(body))}85        url = "%s/%s/%s" % (self.container_client.base_url,86                            self.container_name,87                            self.object_name)88        # Use a raw request, otherwise authentication headers are used89        resp, body = self.object_client.http_obj.request(url, "POST",90                                                         body, headers=headers)91        self.assertIn(int(resp['status']), HTTP_SUCCESS)92        # Ensure object is available93        resp, body = self.object_client.get("%s/%s%s" % (94            self.container_name, self.object_name, "testfile"))95        self.assertIn(int(resp['status']), HTTP_SUCCESS)96        self.assertEqual(body, "hello world")97    @attr(type=['gate', 'negative'])98    def test_post_object_using_form_expired(self):99        body, content_type = self.get_multipart_form(expires=1)100        time.sleep(2)101        headers = {'Content-Type': content_type,102                   'Content-Length': str(len(body))}103        url = "%s/%s/%s" % (self.container_client.base_url,104                            self.container_name,105                            self.object_name)106        # Use a raw request, otherwise authentication headers are used107        resp, body = self.object_client.http_obj.request(url, "POST",108                                                         body, headers=headers)109        self.assertEqual(int(resp['status']), 401)...request.py
Source:request.py  
...17    def recipients_as_list(cls, value) -> List:18        if isinstance(value, str):19            return value.split(",")20        return value21    def get_multipart_form(self) -> Tuple:22        form_data_dict = self.dict(exclude_none=True)23        multipart_list = []24        for key, value in form_data_dict.items():25            if key == "attachments":26                for v in value:27                    multipart_list.append((key, (Path(v).name, open(v))))28            elif isinstance(value, list):29                for v in value:30                    multipart_list.append((key, (None, v)))31            elif isinstance(value, str):32                multipart_list.append((key, (None, value)))33        return tuple(multipart_list)34    def submit(self) -> None:35        response = requests.post(url=str(self.request_uri), files=self.get_multipart_form())36        if response.ok:37            LOG.info(f"{response.status_code}: {response.text}")38        else:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
