Add a sample demonstrating how to use REST Assured

Closes gh-198
This commit is contained in:
Andy Wilkinson
2016-02-15 17:12:16 +00:00
parent 7e2a3dcd18
commit c8ecd25591
11 changed files with 489 additions and 14 deletions

View File

@@ -0,0 +1,26 @@
= Spring REST Docs REST Assured Sample
Andy Wilkinson;
:doctype: book
:icons: font
:source-highlighter: highlightjs
Sample application demonstrating how to use Spring REST Docs with REST Assured.
`SampleRestAssuredApplicationTests` makes a call to a very simple service. The service
that is being tested is running on a random port on `localhost`. The tests make use of a
preprocessor to modify the request so that it appears to have been sent to
`https://api.example.com`. If your service includes URIs in its responses, for example
because it uses hypermedia, similar preprocessing can be applied to the response before
it is documented.
Three snippets are produced. One showing how to make a request using cURL:
include::{snippets}/sample/curl-request.adoc[]
One showing the HTTP request:
include::{snippets}/sample/http-request.adoc[]
And one showing the HTTP response:
include::{snippets}/sample/http-response.adoc[]

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.restassured;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class SampleRestAssuredApplication {
public static void main(String[] args) {
new SpringApplication(SampleRestAssuredApplication.class).run(args);
}
@RestController
private static class SampleController {
@RequestMapping("/")
public String index() {
return "Hello, World";
}
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.restassured;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document;
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.restassured.operation.preprocess.RestAssuredPreprocessors.modifyUris;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.specification.RequestSpecification;
@SpringApplicationConfiguration(classes=SampleRestAssuredApplication.class)
@WebIntegrationTest("server.port=0")
@RunWith(SpringJUnit4ClassRunner.class)
public class SampleRestAssuredApplicationTests {
@Rule
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("build/generated-snippets");
private RequestSpecification documentationSpec;
@Value("${local.server.port}")
private int port;
@Before
public void setUp() {
this.documentationSpec = new RequestSpecBuilder()
.addFilter(documentationConfiguration(restDocumentation)).build();
}
@Test
public void sample() throws Exception {
given(this.documentationSpec)
.accept("text/plain")
.filter(document("sample",
preprocessRequest(modifyUris()
.scheme("https")
.host("api.example.com")
.removePort())))
.when()
.port(this.port)
.get("/")
.then()
.assertThat().statusCode(is(200));
}
}