Search in sources :

Example 11 with MultiPartSpecBuilder

use of io.restassured.builder.MultiPartSpecBuilder in project rest-assured by rest-assured.

the class MultiPartITest method defaultControlNameIsUsedWhenNoControlNameIsDefinedInMultiPartSpecBuilder.

@Test
public void defaultControlNameIsUsedWhenNoControlNameIsDefinedInMultiPartSpecBuilder() throws Exception {
    // Given
    final InputStream is = getClass().getResourceAsStream("/car-records.xsd");
    // When
    given().queryParam("controlName", "file2").config(RestAssuredConfig.config().multiPartConfig(multiPartConfig().defaultControlName("file2"))).multiPart(new MultiPartSpecBuilder(is).build()).when().post("/multipart/file").then().statusCode(200).body(is(IOUtils.toString(getClass().getResourceAsStream("/car-records.xsd"))));
}
Also used : InputStream(java.io.InputStream) MultiPartSpecBuilder(io.restassured.builder.MultiPartSpecBuilder) Test(org.junit.Test)

Example 12 with MultiPartSpecBuilder

use of io.restassured.builder.MultiPartSpecBuilder in project rest-assured by rest-assured.

the class MultiPartITest method controlNameInMultiPartSpecBuilderHasPrecedenceOverDefaultWhenConfigured.

@Test
public void controlNameInMultiPartSpecBuilderHasPrecedenceOverDefaultWhenConfigured() throws Exception {
    // Given
    final InputStream is = getClass().getResourceAsStream("/car-records.xsd");
    // When
    given().config(RestAssuredConfig.config().multiPartConfig(multiPartConfig().defaultControlName("something-else"))).queryParam("controlName", "file2").multiPart(new MultiPartSpecBuilder(is).controlName("file2").build()).when().post("/multipart/file").then().statusCode(200).body(is(IOUtils.toString(getClass().getResourceAsStream("/car-records.xsd"))));
}
Also used : InputStream(java.io.InputStream) MultiPartSpecBuilder(io.restassured.builder.MultiPartSpecBuilder) Test(org.junit.Test)

Example 13 with MultiPartSpecBuilder

use of io.restassured.builder.MultiPartSpecBuilder in project rest-assured by rest-assured.

the class MockMvcRequestSenderImpl method logRequestIfApplicable.

private void logRequestIfApplicable(HttpMethod method, String uri, String originalPath, Object[] unnamedPathParams) {
    if (requestLoggingFilter == null) {
        return;
    }
    final RequestSpecificationImpl reqSpec = new RequestSpecificationImpl("http://localhost", RestAssured.UNDEFINED_PORT, "", new NoAuthScheme(), Collections.<Filter>emptyList(), null, true, ConfigConverter.convertToRestAssuredConfig(config), logRepository, null);
    reqSpec.setMethod(method.toString());
    reqSpec.path(uri);
    reqSpec.buildUnnamedPathParameterTuples(unnamedPathParams);
    if (params != null) {
        new ParamLogger(params) {

            protected void logParam(String paramName, Object paramValue) {
                reqSpec.param(paramName, paramValue);
            }
        }.logParams();
    }
    if (queryParams != null) {
        new ParamLogger(queryParams) {

            protected void logParam(String paramName, Object paramValue) {
                reqSpec.queryParam(paramName, paramValue);
            }
        }.logParams();
    }
    if (formParams != null) {
        new ParamLogger(formParams) {

            protected void logParam(String paramName, Object paramValue) {
                reqSpec.formParam(paramName, paramValue);
            }
        }.logParams();
    }
    if (headers != null) {
        for (Header header : headers) {
            reqSpec.header(header);
        }
    }
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            reqSpec.cookie(cookie);
        }
    }
    if (requestBody != null) {
        if (requestBody instanceof byte[]) {
            reqSpec.body((byte[]) requestBody);
        } else if (requestBody instanceof File) {
            String contentType = findContentType();
            String charset = null;
            if (StringUtils.isNotBlank(contentType)) {
                charset = CharsetExtractor.getCharsetFromContentType(contentType);
            }
            if (charset == null) {
                charset = Charset.defaultCharset().toString();
            }
            String string = fileToString((File) requestBody, charset);
            reqSpec.body(string);
        } else {
            reqSpec.body(requestBody);
        }
    }
    if (multiParts != null) {
        for (MockMvcMultiPart multiPart : multiParts) {
            reqSpec.multiPart(new MultiPartSpecBuilder(multiPart.getContent()).controlName(multiPart.getControlName()).fileName(multiPart.getFileName()).mimeType(multiPart.getMimeType()).build());
        }
    }
    String uriPath = PathSupport.getPath(uri);
    String originalUriPath = PathSupport.getPath(originalPath);
    requestLoggingFilter.filter(reqSpec, null, new FilterContextImpl(uri, originalUriPath, uriPath, uri, uri, new Object[0], method.toString(), null, Collections.<Filter>emptyList().iterator(), new HashMap<String, Object>()));
}
Also used : FilterContextImpl(io.restassured.internal.filter.FilterContextImpl) NoAuthScheme(io.restassured.authentication.NoAuthScheme) RequestSpecificationImpl(io.restassured.internal.RequestSpecificationImpl) MultiPartSpecBuilder(io.restassured.builder.MultiPartSpecBuilder) MockMultipartFile(org.springframework.mock.web.MockMultipartFile)

Example 14 with MultiPartSpecBuilder

use of io.restassured.builder.MultiPartSpecBuilder in project rest-assured by rest-assured.

the class MultiPartITest method byteArrayUploadingWhenUsingMultiPartSpecification.

@Test
public void byteArrayUploadingWhenUsingMultiPartSpecification() throws Exception {
    // Given
    final byte[] bytes = IOUtils.toByteArray(getClass().getResourceAsStream("/car-records.xsd"));
    // When
    given().multiPart(new MultiPartSpecBuilder(bytes).build()).expect().statusCode(200).body(is(new String(bytes))).when().post("/multipart/file");
}
Also used : MultiPartSpecBuilder(io.restassured.builder.MultiPartSpecBuilder) Test(org.junit.Test)

Example 15 with MultiPartSpecBuilder

use of io.restassured.builder.MultiPartSpecBuilder in project rest-assured by rest-assured.

the class MultiPartITest method fileNameInMultiPartSpecBuilderHasPrecedenceOverDefaultWhenConfigured.

@Test
public void fileNameInMultiPartSpecBuilderHasPrecedenceOverDefaultWhenConfigured() throws Exception {
    // Given
    final InputStream is = getClass().getResourceAsStream("/car-records.xsd");
    // When
    given().config(RestAssuredConfig.config().multiPartConfig(multiPartConfig().defaultFileName("something-else"))).multiPart(new MultiPartSpecBuilder(is).fileName("file2.txt").build()).when().post("/multipart/filename").then().statusCode(200).body(equalTo("file2.txt"));
    ;
}
Also used : InputStream(java.io.InputStream) MultiPartSpecBuilder(io.restassured.builder.MultiPartSpecBuilder) Test(org.junit.Test)

Aggregations

MultiPartSpecBuilder (io.restassured.builder.MultiPartSpecBuilder)18 Test (org.junit.Test)17 InputStream (java.io.InputStream)9 Greeting (io.restassured.itest.java.objects.Greeting)6 NoAuthScheme (io.restassured.authentication.NoAuthScheme)1 RequestSpecificationImpl (io.restassured.internal.RequestSpecificationImpl)1 FilterContextImpl (io.restassured.internal.filter.FilterContextImpl)1 Jackson2Mapper (io.restassured.internal.mapping.Jackson2Mapper)1 DefaultJackson2ObjectMapperFactory (io.restassured.mapper.factory.DefaultJackson2ObjectMapperFactory)1 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)1