|
| 1 | +package org.springframework.graphql.client; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.springframework.core.io.ClassPathResource; |
| 5 | +import org.springframework.graphql.server.webflux.GraphQlHttpHandler; |
| 6 | +import org.springframework.http.codec.multipart.FilePart; |
| 7 | +import org.springframework.http.server.reactive.HttpHandler; |
| 8 | +import org.springframework.test.web.reactive.server.HttpHandlerConnector; |
| 9 | +import org.springframework.web.reactive.function.client.WebClient; |
| 10 | +import org.springframework.web.reactive.function.server.HandlerStrategies; |
| 11 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 12 | +import org.springframework.web.reactive.function.server.RouterFunctions; |
| 13 | +import org.springframework.web.reactive.function.server.ServerResponse; |
| 14 | + |
| 15 | +import java.time.Duration; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.List; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.springframework.web.reactive.function.server.RouterFunctions.route; |
| 23 | + |
| 24 | +public class HttpGraphQlClientTests { |
| 25 | + |
| 26 | + private static final String DOCUMENT = "{ Mutation }"; |
| 27 | + |
| 28 | + private static final Duration TIMEOUT = Duration.ofSeconds(5); |
| 29 | + |
| 30 | + @Test |
| 31 | + void shouldSendOneFile() { |
| 32 | + MultipartHttpBuilderSetup clientSetup = new MultipartHttpBuilderSetup(); |
| 33 | + |
| 34 | + // Original header value |
| 35 | + HttpGraphQlClient.Builder<?> builder = clientSetup.initBuilder(); |
| 36 | + |
| 37 | + HttpGraphQlClient client = builder.build(); |
| 38 | + client.document(DOCUMENT) |
| 39 | + .variable("existingVar", "itsValue") |
| 40 | + .fileVariable("fileInput", new ClassPathResource("/foo.txt")) |
| 41 | + .executeFileUpload().block(TIMEOUT); |
| 42 | + assertThat(clientSetup.getActualRequest().getVariables().get("existingVar")).isEqualTo("itsValue"); |
| 43 | + assertThat(clientSetup.getActualRequest().getVariables().get("fileInput")).isNotNull(); |
| 44 | + assertThat(((FilePart)clientSetup.getActualRequest().getVariables().get("fileInput")).filename()).isEqualTo("foo.txt"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void shouldSendOneCollectionOfFiles() { |
| 49 | + MultipartHttpBuilderSetup clientSetup = new MultipartHttpBuilderSetup(); |
| 50 | + |
| 51 | + // Original header value |
| 52 | + HttpGraphQlClient.Builder<?> builder = clientSetup.initBuilder(); |
| 53 | + |
| 54 | + HttpGraphQlClient client = builder.build(); |
| 55 | + List<ClassPathResource> resources = new ArrayList<>(); |
| 56 | + resources.add(new ClassPathResource("/foo.txt")); |
| 57 | + resources.add(new ClassPathResource("/bar.txt")); |
| 58 | + |
| 59 | + client.document(DOCUMENT) |
| 60 | + .variable("existingVar", "itsValue") |
| 61 | + .fileVariable("filesInput", resources) |
| 62 | + .executeFileUpload().block(TIMEOUT); |
| 63 | + assertThat(clientSetup.getActualRequest().getVariables().get("existingVar")).isEqualTo("itsValue"); |
| 64 | + assertThat(clientSetup.getActualRequest().getVariables().get("filesInput")).isNotNull(); |
| 65 | + assertThat(((Collection<FilePart>)clientSetup.getActualRequest().getVariables().get("filesInput")).size()).isEqualTo(2); |
| 66 | + assertThat(((Collection<FilePart>)clientSetup.getActualRequest().getVariables().get("filesInput")).stream().map(filePart -> filePart.filename()).collect(Collectors.toSet())).contains("foo.txt", "bar.txt"); |
| 67 | + } |
| 68 | + |
| 69 | + private static class MultipartHttpBuilderSetup extends WebGraphQlClientBuilderTests.AbstractBuilderSetup { |
| 70 | + |
| 71 | + @Override |
| 72 | + public HttpGraphQlClient.Builder<?> initBuilder() { |
| 73 | + GraphQlHttpHandler handler = new GraphQlHttpHandler(webGraphQlHandler()); |
| 74 | + RouterFunction<ServerResponse> routerFunction = route().POST("/**", handler::handleMultipartRequest).build(); |
| 75 | + HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction, HandlerStrategies.withDefaults()); |
| 76 | + HttpHandlerConnector connector = new HttpHandlerConnector(httpHandler); |
| 77 | + return HttpGraphQlClient.builder(WebClient.builder().clientConnector(connector)); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | +} |
0 commit comments