cucumber.api.spring.test.web.servlet.StatusResultMatchersStepdefs.java Source code

Java tutorial

Introduction

Here is the source code for cucumber.api.spring.test.web.servlet.StatusResultMatchersStepdefs.java

Source

/*
 * 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.en.Then;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @author Marcel Overdijk
 * @since 1.0.0
 */
public class StatusResultMatchersStepdefs {

    @Autowired
    private MockMvcStepdefs mockMvcStepdefs;

    /*
     * Response status step definitions
     */

    @Then("^the response status should be (\\d+)$")
    public void the_response_status_should_be(int status) throws Throwable {
        mockMvcStepdefs.getResultActions().andExpect(status().is(status));
    }

    @Then("^the response status should be \"(.*?)\"$")
    public void the_repsonse_status_should_be(String reasonPhrase) throws Throwable {
        for (HttpStatus status : HttpStatus.values()) {
            if (status.getReasonPhrase().equalsIgnoreCase(reasonPhrase)) {
                mockMvcStepdefs.getResultActions().andExpect(status().is(status.value()));
                return;
            }
        }
        throw new IllegalArgumentException("No matching status code found for reason phrase: " + reasonPhrase);
    }

    @Then("^the response status should be 1xx Informational$")
    public void the_response_status_should_be_1xx_informational() throws Throwable {
        mockMvcStepdefs.getResultActions().andExpect(status().is1xxInformational());
    }

    @Then("^the response status should be 2xx Successful$")
    public void the_response_status_should_be_2xx_successful() throws Throwable {
        mockMvcStepdefs.getResultActions().andExpect(status().is2xxSuccessful());
    }

    @Then("^the response status should be 3xx Redirection$")
    public void the_response_status_should_be_3xx_redirection() throws Throwable {
        mockMvcStepdefs.getResultActions().andExpect(status().is3xxRedirection());
    }

    @Then("^the response status should be 4xx Client Error$")
    public void the_response_status_should_be_4xx_client_error() throws Throwable {
        mockMvcStepdefs.getResultActions().andExpect(status().is4xxClientError());
    }

    @Then("^the response status should be 5xx Server Error$")
    public void the_response_status_should_be_5xx_server_error() throws Throwable {
        mockMvcStepdefs.getResultActions().andExpect(status().is5xxServerError());
    }
}