pub struct MultiPartFormDataBuilder { /* private fields */ }
Expand description

Simple builder for multipart/form-data test

Examples

#[cfg(test)]
mod tests {
    use actix_multipart_test::MultiPartFormDataBuilder;
    use actix_web::{test, App};
    use super::*;

    #[actix_web::test]
    async fn test_should_upload_file() {

        let mut app =
            test::init_service(
                    App::new()
                    .service(yourmultipartformhandler)
                )
                .await;

        let mut multipart_form_data_builder = MultiPartFormDataBuilder::new();
        multipart_form_data_builder.with_file("tests/sample.png", "sample", "image/png", "sample.png");
        multipart_form_data_builder.with_text("name", "some_name");
        let (header, body) = multipart_form_data_builder.build();


        let req = test::TestRequest::post()
            .uri("/someurl")
            .insert_header(header)
            .set_payload(body)
            .to_request();
        let resp = test::call_service(&mut app, req).await;

        assert!(resp.status().is_success());

    }
}

Implementations§

source§

impl MultiPartFormDataBuilder

source

pub fn new() -> MultiPartFormDataBuilder

Create new MultiPartFormDataBuilder

source

pub fn with_text( &mut self, name: impl Into<String>, value: impl Into<String> ) -> &mut MultiPartFormDataBuilder

Add text to multipart/form-data

name is form name

value is form value

Returns &mut MultiPartFormDataBuilder

source

pub fn with_file( &mut self, path: impl AsRef<Path> + 'static, name: impl Into<String>, content_type: impl Into<String>, file_name: impl Into<String> ) -> &mut MultiPartFormDataBuilder

Add file to multipart/form-data

path is file path

name is form name

content_type is file content type

file_name is file name

source

pub fn build(&self) -> ((String, String), Vec<u8>)

Build multipart/form-data

Returns ((header_name, header_value), body)

header_name is “Content-Type”

header_value is “multipart/form-data; boundary=…”

body is binary data

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.