1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| public interface BasicMockRequest {
/** * 发送get请求 * * @param uri * @return */ default RequestBuilder get(String uri) { return MockMvcRequestBuilders.get(uri) .accept(MediaType.APPLICATION_JSON_UTF8); }
/** * 发送get请求并携带token * * @param uri * @param token * @return */ default RequestBuilder getAndTakeToken(String uri, String token) { return MockMvcRequestBuilders.get(uri) .header("token", token) .accept(MediaType.APPLICATION_JSON_UTF8); }
/** * 发送post请求 * * @param uri * @return */ default RequestBuilder post(String uri) { return MockMvcRequestBuilders.post(uri) .accept(MediaType.APPLICATION_JSON_UTF8); }
/** * 发送post请求并携带token和请求体 * * @param uri * @param token * @param data * @return */ default RequestBuilder postAndTakeTokenAndData(String uri, String token, Object data) { String content = Optional.ofNullable(data) .map(JsonUtils::quietlySerialize) .orElse(""); return MockMvcRequestBuilders.post(uri) .header("token", token) .accept(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8) .content(content); }
/** * 发送post请求并携带请求参数 * * @param uri * @param data * @return */ default RequestBuilder postAndTakeRequestParameter(String uri, Object data) { MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(uri) .accept(MediaType.APPLICATION_JSON_UTF8);
Map<String, String> parameterMap = ObjectAttribute.toParameterMap(data);
parameterMap.forEach((k, v) -> requestBuilder.param(k, v));
return requestBuilder; }
/** * 发送post请求并携带token和请求参数 * * @param uri * @param token * @param data * @return */ default RequestBuilder postAndTokenAndRequestParameter(String uri, String token, Object data) { return ((MockHttpServletRequestBuilder) postAndTakeRequestParameter(uri, data)) .header("token", token); }
/** * 发送post请求并携带请求体 * * @param uri * @param data * @return */ default RequestBuilder postAndTakeRequestBody(String uri, Object data) { return MockMvcRequestBuilders.post(uri) .accept(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8) .content(JsonUtils.quietlySerialize(data)); } }
|