Skip to content

Commit

Permalink
test(http): fix falling test
Browse files Browse the repository at this point in the history
  • Loading branch information
tchiotludo committed Nov 30, 2023
1 parent 73ce33e commit 0d63fc8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 33 deletions.
17 changes: 14 additions & 3 deletions src/test/java/io/kestra/plugin/fs/http/DownloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ void run() throws Exception {

@Test
void noResponse() throws Exception {
EmbeddedServer embeddedServer = applicationContext.getBean(EmbeddedServer.class);
embeddedServer.start();

Download task = Download.builder()
.id(DownloadTest.class.getSimpleName())
.type(DownloadTest.class.getName())
.uri("https://run.mocky.io/v3/bd4e25ed-2de1-44c9-b613-9612c965684b")
.uri(embeddedServer.getURI() + "/204")
.build();

RunContext runContext = TestsUtils.mockRunContext(this.runContextFactory, task, ImmutableMap.of());
Expand All @@ -71,16 +74,19 @@ void noResponse() throws Exception {
() -> task.run(runContext)
);

assertThat(exception.getMessage(), is("Service Unavailable"));
assertThat(exception.getMessage(), is("No response from server"));
}

@Test
void allowNoResponse() throws IOException {
EmbeddedServer embeddedServer = applicationContext.getBean(EmbeddedServer.class);
embeddedServer.start();

Download task = Download.builder()
.id(DownloadTest.class.getSimpleName())
.failOnEmptyResponse(false)
.type(DownloadTest.class.getName())
.uri("https://run.mocky.io/v3/513a88cf-65fc-4819-9fbf-3a3216a998c4")
.uri(embeddedServer.getURI() + "/204")
.build();

RunContext runContext = TestsUtils.mockRunContext(this.runContextFactory, task, ImmutableMap.of());
Expand Down Expand Up @@ -117,5 +123,10 @@ public static class SlackWebController {
public HttpResponse<String> error() {
return HttpResponse.serverError();
}

@Get("204")
public HttpResponse<Void> noContent() {
return HttpResponse.noContent();
}
}
}
74 changes: 44 additions & 30 deletions src/test/java/io/kestra/plugin/fs/http/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import io.kestra.core.storages.StorageInterface;
import io.kestra.core.utils.TestsUtils;
import io.micronaut.context.ApplicationContext;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.*;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.multipart.StreamingFileUpload;
import io.micronaut.runtime.server.EmbeddedServer;
Expand Down Expand Up @@ -46,21 +44,24 @@ class RequestTest {

@Test
void run() throws Exception {
final String url = "http://www.mocky.io/v2/5ed0ce483500009300ff9f55";
try (
ApplicationContext applicationContext = ApplicationContext.run();
EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start();

Request task = Request.builder()
.id(RequestTest.class.getSimpleName())
.type(RequestTest.class.getName())
.uri(url)
.build();
) {
Request task = Request.builder()
.id(RequestTest.class.getSimpleName())
.type(RequestTest.class.getName())
.uri(server.getURL().toString() + "/hello")
.build();

RunContext runContext = TestsUtils.mockRunContext(this.runContextFactory, task, ImmutableMap.of());
RunContext runContext = TestsUtils.mockRunContext(this.runContextFactory, task, ImmutableMap.of());

Request.Output output = task.run(runContext);
Request.Output output = task.run(runContext);

assertThat(output.getUri(), is(URI.create(url)));
assertThat(output.getBody(), is("{ \"hello\": \"world\" }"));
assertThat(output.getCode(), is(200));
assertThat(output.getBody(), is("{ \"hello\": \"world\" }"));
assertThat(output.getCode(), is(200));
}
}

@Test
Expand All @@ -84,22 +85,25 @@ void head() throws Exception {

@Test
void failed() throws Exception {
final String url = "http://www.mocky.io/v2/5ed0d31c3500009300ff9f94";
try (
ApplicationContext applicationContext = ApplicationContext.run();
EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start();

Request task = Request.builder()
.id(RequestTest.class.getSimpleName())
.type(RequestTest.class.getName())
.uri(url)
.allowFailed(true)
.build();
) {
Request task = Request.builder()
.id(RequestTest.class.getSimpleName())
.type(RequestTest.class.getName())
.uri(server.getURL().toString() + "/hello417")
.allowFailed(true)
.build();

RunContext runContext = TestsUtils.mockRunContext(this.runContextFactory, task, ImmutableMap.of());
RunContext runContext = TestsUtils.mockRunContext(this.runContextFactory, task, ImmutableMap.of());

Request.Output output = task.run(runContext);
Request.Output output = task.run(runContext);

assertThat(output.getUri(), is(URI.create(url)));
assertThat(output.getBody(), is("{ \"hello\": \"world\" }"));
assertThat(output.getCode(), is(417));
assertThat(output.getBody(), is("{ \"hello\": \"world\" }"));
assertThat(output.getCode(), is(417));
}
}

@Test
Expand Down Expand Up @@ -218,14 +222,24 @@ void multipartCustomFilename() throws Exception {
}
}

@Controller(value = "/post", consumes = MediaType.APPLICATION_FORM_URLENCODED)
@Controller
static class MockController {
@Post("/simple")
@Get("/hello")
HttpResponse<String> hello() {
return HttpResponse.ok("{ \"hello\": \"world\" }");
}

@Get("/hello417")
HttpResponse<String> hello417() {
return HttpResponse.status(HttpStatus.EXPECTATION_FAILED).body("{ \"hello\": \"world\" }");
}

@Post(uri = "/post/simple", consumes = MediaType.APPLICATION_FORM_URLENCODED)
HttpResponse<String> simple(HttpRequest<?> request, String hello) {
return HttpResponse.ok(hello + " > " + request.getHeaders().get("test"));
}

@Post(uri = "/multipart", consumes = MediaType.MULTIPART_FORM_DATA)
@Post(uri = "/post/multipart", consumes = MediaType.MULTIPART_FORM_DATA)
Single<String> multipart(HttpRequest<?> request, String hello, StreamingFileUpload file) throws IOException {
File tempFile = File.createTempFile(file.getFilename(), "temp");

Expand Down

0 comments on commit 0d63fc8

Please sign in to comment.