def test_files_are_included(self):
        parameter = make_string()
        contents = os.urandom(5)
        filename = self.make_file(contents=contents)
        # Writing the parameter as "parameter@=filename" on the
        # command-line causes name_value_pair() to return a `name,
        # opener` tuple, where `opener` is a callable that returns an
        # open file handle.
        data = [(parameter, partial(open, filename, "rb"))]
        uri, body, headers = utils.prepare_payload(
            op=None, method="POST", uri="http://localhost", data=data)

        expected_body_template = """\
            --...
            Content-Transfer-Encoding: base64
            Content-Disposition: form-data; ...name="%s"; ...name="%s"
            MIME-Version: 1.0
            Content-Type: application/octet-stream

            %s
            --...--
            """
        expected_body = expected_body_template % (
            parameter, parameter, base64.b64encode(contents).decode("ascii"))

        self.assertDocTestMatches(expected_body, body.decode("ascii"))
    def test_files_are_included(self):
        parameter = make_string()
        contents = os.urandom(5)
        filename = self.make_file(contents=contents)
        # Writing the parameter as "parameter@=filename" on the
        # command-line causes name_value_pair() to return a `name,
        # opener` tuple, where `opener` is a callable that returns an
        # open file handle.
        data = [(parameter, partial(open, filename, "rb"))]
        uri, body, headers = utils.prepare_payload(
            op=None, method="POST", uri="http://localhost", data=data)

        expected_body_template = """\
            --...
            Content-Transfer-Encoding: base64
            Content-Disposition: form-data; ...name="%s"; ...name="%s"
            MIME-Version: 1.0
            Content-Type: application/octet-stream

            %s
            --...--
            """
        expected_body = expected_body_template % (
            parameter, parameter, base64.b64encode(contents).decode("ascii"))

        self.assertDocTestMatches(expected_body, body.decode("ascii"))
 def test_encode_multipart_data_closes_with_closing_boundary_line(self):
     data = {'foo': make_string().encode('ascii')}
     files = {'bar': BytesIO(make_string().encode('ascii'))}
     body, headers = encode_multipart_data(data, files)
     self.assertThat(body, EndsWith(b'--'))
 def test_encode_multipart_data_produces_bytes(self):
     data = {make_string(): make_string().encode('ascii')}
     files = {make_string(): BytesIO(make_string().encode('ascii'))}
     body, headers = encode_multipart_data(data, files)
     self.assertIsInstance(body, bytes)
 def test_encode_multipart_data_closes_with_closing_boundary_line(self):
     data = {'foo': make_string().encode('ascii')}
     files = {'bar': BytesIO(make_string().encode('ascii'))}
     body, headers = encode_multipart_data(data, files)
     self.assertThat(body, EndsWith(b'--'))
 def test_encode_multipart_data_produces_bytes(self):
     data = {make_string(): make_string().encode('ascii')}
     files = {make_string(): BytesIO(make_string().encode('ascii'))}
     body, headers = encode_multipart_data(data, files)
     self.assertIsInstance(body, bytes)