토리맘의 한글라이즈 프로젝트 logo 토리맘의 한글라이즈 프로젝트

스프링 클라우드 컨트랙트 공식 레퍼런스를 한글로 번역한 문서입니다.

전체 목차는 여기에 있습니다.


이 모드는 아직 실험적인 단계이며, 향후 변경될 수 있다.

Spring Cloud Contract를 사용할 땐 org.springframework.cloud.contract.verifier.http.HttpVerifier를 직접 구현해 커스텀할 수 있다. 이렇게 하면 원하는 클라이언트로 요청을 주고받을 수 있다. Spring Cloud Contract가 사용하는 디폴트 구현체는 OkHttpHttpVerifier이며, 이 구현체는 OkHttp3 http 클라이언트를 사용한다.

먼저 testModeCUSTOM으로 변경한다:

testMode = 'CUSTOM'

그러면 다음과 같은 테스트가 만들어진다:

import com.example.BeerRestBase;
import javax.inject.Inject;
import org.springframework.cloud.contract.verifier.http.HttpVerifier;
import org.springframework.cloud.contract.verifier.http.Request;
import org.springframework.cloud.contract.verifier.http.Response;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat;
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.*;
import static org.springframework.cloud.contract.verifier.http.Request.given;

public class RestTest extends BeerRestBase {
	@Inject HttpVerifier httpVerifier;

	@Test
	public void validate_shouldGrantABeerIfOldEnough() throws Exception {
		// given:
			Request request = given()
					.post("/beer.BeerService/check")
					.scheme("HTTP")
					.protocol("h2_prior_knowledge")
					.header("Content-Type", "application/grpc")
					.header("te", "trailers")
					.body(fileToBytes(this, "shouldGrantABeerIfOldEnough_request_PersonToCheck_old_enough.bin"))
					.build();


		// when:
			Response response = httpVerifier.exchange(request);


		// then:
			assertThat(response.statusCode()).isEqualTo(200);
			assertThat(response.header("Content-Type")).matches("application/grpc.*");
			assertThat(response.header("grpc-encoding")).isEqualTo("identity");
			assertThat(response.header("grpc-accept-encoding")).isEqualTo("gzip");

		// and:
			assertThat(response.getBody().asByteArray()).isEqualTo(fileToBytes(this, "shouldGrantABeerIfOldEnough_response_Response_old_enough.bin"));
	}

}

이에 맞는 베이스 클래스는 다음과 같다:

@SpringBootTest(classes = BeerRestBase.Config.class,
		webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class BeerRestBase {

	@Configuration
	@EnableAutoConfiguration
	static class Config {

		@Bean
		ProducerController producerController(PersonCheckingService personCheckingService) {
			return new ProducerController(personCheckingService);
		}

		@Bean
		PersonCheckingService testPersonCheckingService() {
			return argument -> argument.getAge() >= 20;
		}

		@Bean
		HttpVerifier httpOkVerifier(@LocalServerPort int port) {
			return new OkHttpHttpVerifier("localhost:" + port);
		}

	}
}

Next :
3.4.5. Working with Context Paths
Context Path 사용하기

전체 목차는 여기에 있습니다.

<< >>

TOP