Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow RestAssured to test external servers #1009

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 original authors
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,31 +16,44 @@
package io.micronaut.test.rest.assured;

import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Prototype;
import io.micronaut.context.annotation.Requires;
import io.micronaut.runtime.server.EmbeddedServer;
import io.restassured.RestAssured;
import io.restassured.specification.RequestSpecification;

import static io.micronaut.test.support.server.TestEmbeddedServer.PROPERTY;

/**
* Helper class making it easier to create request specifications.
*
* @author gkrocher
* @since 3.4.0
* @since 3.4.0
*/
@Factory
@Requires(beans = EmbeddedServer.class)
@Requires(beans = EmbeddedServer.class)
public class RequestSpecificationFactory {

/**
* @param embeddedServer The embedded server
* @return A request specification for the current server.
*/
@Prototype
@Requires(beans = EmbeddedServer.class)
@Requires(beans = EmbeddedServer.class, missingProperty = PROPERTY)
RequestSpecification requestSpecification(EmbeddedServer embeddedServer) {
return RestAssured.given()
.port(embeddedServer.getPort());
}

/**
* @param url The optional URL for an external service
* @return A request specification for the external server.
*/
@Prototype
@Requires(property = PROPERTY)
RequestSpecification requestSpecificationWithURL(@Property(name = PROPERTY) String url) {
return RestAssured.given()
.baseUri(url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.micronaut.test.rest.assured;

import io.micronaut.context.annotation.Property;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasSize;

@MicronautTest
@Property(name = "micronaut.test.server.url", value = "https://jsonplaceholder.typicode.com")
class RestAssuredExternalServerTest {

@Test
void testSinglePost(RequestSpecification spec) {
spec
.when().get("/posts/1")
.then().statusCode(200)
.body("title", is("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"));
}

@Test
void testUsers(RequestSpecification spec) {
spec
.when().get("/users")
.then().statusCode(200)
.body("id", hasSize(10));
}
}
14 changes: 14 additions & 0 deletions test-rest-assured/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Loading