Skip to content

Commit

Permalink
Allow RestAssured to test external servers (#1009)
Browse files Browse the repository at this point in the history
Close: #1006
  • Loading branch information
timyates authored Apr 22, 2024
1 parent 37528ea commit e533055
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
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>

0 comments on commit e533055

Please sign in to comment.