Java tutorial
/* * Copyright 2014 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 cucumber.api.spring.test.web.servlet; import cucumber.api.java.After; import cucumber.api.java.Before; import cucumber.api.java.en.Given; import cucumber.api.java.en.When; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import java.util.List; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request; /** * @author Marcel Overdijk * @since 1.0.0 */ public class MockMvcStepdefs { @Autowired private MockMvc mockMvc; private MockHttpServletRequestBuilder requestBuilder; private ResultActions resultActions; private boolean print = false; @Before public void beforeScenario() { requestBuilder = get("/"); resultActions = null; } @Before("@print-mvcresult") public void doPrint() { print = true; } @After("@print-mvcresult") public void donePrint() { print = false; } public MockHttpServletRequestBuilder getRequestBuilder() { return requestBuilder; } public ResultActions getResultActions() { return resultActions; } @Given("^a new request$") public void a_new_request() throws Throwable { requestBuilder = get("/"); resultActions = null; } /* * Request body step definitions */ @Given("^the request content is:$") public void the_request_content_is(String content) throws Throwable { requestBuilder.content(content.getBytes()); } /* * Request header step definitions */ @Given("^the request header \"(.*?)\" has the value \"(.*?)\"$") public void the_request_header_has_the_value(String name, String value) throws Throwable { requestBuilder.header(name, value); } @Given("^the request header \"(.*?)\" has the values \"(.*?)\"$") public void the_request_header_has_the_values(String name, List<String> values) throws Throwable { requestBuilder.header(name, values); } @Given("^the request content type is \"(.*?)\"$") public void the_request_content_type_is(String contentType) throws Throwable { // https://jira.spring.io/browse/SPR-12405 requestBuilder.contentType(MediaType.parseMediaType(contentType)); } @Given("^the request accepts the content type \"(.*?)\"$") public void the_request_accepts(String contentType) throws Throwable { requestBuilder.accept(contentType); } @Given("^the request accepts the content types \"(.*?)\"$") public void the_request_accepts_the_content_types(List<String> contentTypes) throws Throwable { requestBuilder.accept(contentTypes.toArray(new String[contentTypes.size()])); } // @Given("^the basic authentication request header with username \"(.*?)\" and password \"(.*?)\"$") // public void the_basic_authentication_request_header_with_username_and_password(String username, String password) throws Throwable { // byte[] toEncode; // try { // toEncode = (username + ":" + password).getBytes("UTF-8"); // } catch (UnsupportedEncodingException e) { // throw new RuntimeException(e); // } // requestBuilder.addHeader("Authorization", "Basic " + new String(Base64.encode(toEncode))); // } /* * Perform request step definitions */ private void i_perform_a_request_on(HttpMethod httpMethod, String uri) throws Throwable { resultActions = mockMvc.perform((RequestBuilder) request(httpMethod, uri).merge(requestBuilder)); } @When("^I perform a DELETE request on \"(.*?)\"$") public void i_perform_a_delete_request_on(String uri) throws Throwable { i_perform_a_request_on(HttpMethod.DELETE, uri); } @When("^I perform a GET request on \"(.*?)\"$") public void i_perform_a_get_request_on(String uri) throws Throwable { i_perform_a_request_on(HttpMethod.GET, uri); } @When("^I perform a HEAD request on \"(.*?)\"$") public void i_perform_a_head_request_on(String uri) throws Throwable { i_perform_a_request_on(HttpMethod.HEAD, uri); } @When("^I perform an OPTIONS request on \"(.*?)\"$") public void i_perform_an_options_request_on(String uri) throws Throwable { i_perform_a_request_on(HttpMethod.OPTIONS, uri); } @When("^I perform a PATCH request on \"(.*?)\"$") public void i_perform_a_patch_request_on(String uri) throws Throwable { i_perform_a_request_on(HttpMethod.PATCH, uri); } @When("^I perform a POST request on \"(.*?)\"$") public void i_perform_a_post_request_on(String uri) throws Throwable { i_perform_a_request_on(HttpMethod.POST, uri); } @When("^I perform a PUT request on \"(.*?)\"$") public void i_perform_a_put_request_on(String uri) throws Throwable { i_perform_a_request_on(HttpMethod.PUT, uri); } }