001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.test;
017
018import com.alibaba.fastjson.JSON;
019import com.alibaba.fastjson.JSONObject;
020import io.jboot.test.web.MockHttpServletResponse;
021
022import javax.servlet.http.Cookie;
023import java.util.*;
024
025public class MockMvcResult {
026
027    final MockHttpServletResponse response;
028    private JSONObject jsonObject;
029
030    public MockMvcResult(MockHttpServletResponse response) {
031        this.response = response;
032    }
033
034    public String getContent() {
035        return response.getContentString();
036    }
037
038    public JSONObject getContentAsJSONObject() {
039        if (jsonObject == null) {
040            jsonObject = JSON.parseObject(getContent());
041        }
042        return jsonObject;
043    }
044
045    public String getContentType() {
046        return response.getContentType();
047    }
048
049    public int getStatus() {
050        return response.getStatus();
051    }
052
053    public String getHeader(String name) {
054        return response.getHeader(name);
055    }
056
057    public Map<String, String> getHeaders() {
058        Map<String, String> headers = new HashMap<>();
059        Collection<String> headerNames = response.getHeaderNames();
060        if (headerNames != null) {
061            for (String headerName : headerNames) {
062                headers.put(headerName, response.getHeader(headerName));
063            }
064        }
065        return headers;
066    }
067
068    public Set<Cookie> getCookies() {
069        return response.getCookies();
070    }
071
072    public String getCookie(String name) {
073        for (Cookie cookie : response.getCookies()) {
074            if (Objects.equals(name, cookie.getName())) {
075                return cookie.getValue();
076            }
077        }
078        return null;
079    }
080
081
082    public MockHttpServletResponse getResponse() {
083        return response;
084    }
085
086    public MockMvcResult printResult() {
087        System.out.println(this);
088        return this;
089    }
090
091    public MockMvcResult printContent() {
092        System.out.println(getContent());
093        return this;
094    }
095
096    public MockMvcResult assertThat(MockMvcAsserter mockMvcAssert) {
097        mockMvcAssert.doAssert(this);
098        return this;
099    }
100
101    public MockMvcResult assertJson(MockMvcJsonAsserter mockMvcAssert) {
102        mockMvcAssert.doAssert(this.getContentAsJSONObject());
103        return this;
104    }
105
106
107    public MockMvcResult assertTrue(MockMvcTrueAsserter mockMvcTrueAsserter) {
108        return assertTrue(mockMvcTrueAsserter, "MockMvc result can not match the asserter.");
109    }
110
111
112    public MockMvcResult assertTrue(MockMvcTrueAsserter mockMvcTrueAsserter, String message) {
113        if (!mockMvcTrueAsserter.doAssert(this)) {
114            throw new AssertionError(message);
115        }
116        return this;
117    }
118
119
120    @Override
121    public String toString() {
122        StringBuilder builder = new StringBuilder();
123        builder.append("Response:\n");
124        builder.append("Headers >>> ").append(getHeaders()).append("\n");
125        builder.append("Status  >>> ").append(getStatus()).append("\n");
126        builder.append("Content >>> ").append(getContent()).append("\n");
127        return builder.toString();
128    }
129}