From 1925eb40819ff308e3540ffa7f637decf024dbbf Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Mon, 7 Apr 2025 11:58:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20Mock=20=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/mock/SaRequestForMock.java | 151 ++++++++++++++++++ .../context/mock/SaResponseForMock.java | 83 ++++++++++ .../context/mock/SaStorageForMock.java | 67 ++++++++ .../context/mock/SaTokenContextMockUtil.java | 75 +++++++++ 4 files changed, 376 insertions(+) create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaRequestForMock.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaResponseForMock.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaStorageForMock.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaTokenContextMockUtil.java diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaRequestForMock.java b/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaRequestForMock.java new file mode 100644 index 00000000..d4dd589f --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaRequestForMock.java @@ -0,0 +1,151 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * 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 cn.dev33.satoken.context.mock; + +import cn.dev33.satoken.application.ApplicationInfo; +import cn.dev33.satoken.context.model.SaRequest; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * 对 SaRequest 包装类的实现(Mock 版) + * + * @author click33 + * @since 1.42.0 + */ +public class SaRequestForMock implements SaRequest { + + public Map parameterMap = new LinkedHashMap<>(); + public Map headerMap = new LinkedHashMap<>(); + public Map cookieMap = new LinkedHashMap<>(); + public String requestPath; + public String url; + public String method; + public String host; + public String forwardTo; + + /** + * 获取底层源对象 + */ + @Override + public Object getSource() { + return null; + } + + /** + * 在 [请求体] 里获取一个值 + */ + @Override + public String getParam(String name) { + return parameterMap.get(name); + } + + /** + * 获取 [请求体] 里提交的所有参数名称 + * @return 参数名称列表 + */ + @Override + public Collection getParamNames(){ + return parameterMap.keySet(); + } + + /** + * 获取 [请求体] 里提交的所有参数 + * @return 参数列表 + */ + @Override + public Map getParamMap(){ + return parameterMap; + } + + /** + * 在 [请求头] 里获取一个值 + */ + @Override + public String getHeader(String name) { + return headerMap.get(name); + } + + /** + * 在 [Cookie作用域] 里获取一个值 + */ + @Override + public String getCookieValue(String name) { + return getCookieLastValue(name); + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + */ + @Override + public String getCookieFirstValue(String name){ + return cookieMap.get(name); + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + @Override + public String getCookieLastValue(String name){ + return cookieMap.get(name); + } + + /** + * 返回当前请求path (不包括上下文名称) + */ + @Override + public String getRequestPath() { + return ApplicationInfo.cutPathPrefix(requestPath); + } + + /** + * 返回当前请求的url,例:http://xxx.com/test + * @return see note + */ + public String getUrl() { + return url; + } + + /** + * 返回当前请求的类型 + */ + @Override + public String getMethod() { + return method; + } + + /** + * 查询请求 host + */ + @Override + public String getHost() { + return host; + } + + /** + * 转发请求 + */ + @Override + public Object forward(String path) { + this.forwardTo = path; + return null; + } + +} diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaResponseForMock.java b/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaResponseForMock.java new file mode 100644 index 00000000..c8bbb4c3 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaResponseForMock.java @@ -0,0 +1,83 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * 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 cn.dev33.satoken.context.mock; + +import cn.dev33.satoken.context.model.SaResponse; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * 对 SaResponse 包装类的实现(Mock 版) + * + * @author click33 + * @since 1.42.0 + */ +public class SaResponseForMock implements SaResponse { + + public int status; + public Map headerMap = new LinkedHashMap<>(); + public String redirectTo; + + + /** + * 获取底层源对象 + */ + @Override + public Object getSource() { + return null; + } + + /** + * 设置响应状态码 + */ + @Override + public SaResponse setStatus(int sc) { + this.status = sc; + return this; + } + + /** + * 在响应头里写入一个值 + */ + @Override + public SaResponse setHeader(String name, String value) { + headerMap.put(name, value); + return this; + } + + /** + * 在响应头里添加一个值 + * @param name 名字 + * @param value 值 + * @return 对象自身 + */ + public SaResponse addHeader(String name, String value) { + headerMap.put(name, value); + return this; + } + + /** + * 重定向 + */ + @Override + public Object redirect(String url) { + this.redirectTo = url; + return null; + } + + +} diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaStorageForMock.java b/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaStorageForMock.java new file mode 100644 index 00000000..b30dc7e1 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaStorageForMock.java @@ -0,0 +1,67 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * 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 cn.dev33.satoken.context.mock; + +import cn.dev33.satoken.context.model.SaStorage; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * 对 SaStorage 包装类的实现(Mock 版) + * + * @author click33 + * @since 1.42.0 + */ +public class SaStorageForMock implements SaStorage { + + public Map dataMap = new LinkedHashMap<>(); + + /** + * 获取底层源对象 + */ + @Override + public Object getSource() { + return null; + } + + /** + * 在 [Request作用域] 里写入一个值 + */ + @Override + public SaStorageForMock set(String key, Object value) { + dataMap.put(key, value); + return this; + } + + /** + * 在 [Request作用域] 里获取一个值 + */ + @Override + public Object get(String key) { + return dataMap.get(key); + } + + /** + * 在 [Request作用域] 里删除一个值 + */ + @Override + public SaStorageForMock delete(String key) { + dataMap.remove(key); + return this; + } + +} diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaTokenContextMockUtil.java b/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaTokenContextMockUtil.java new file mode 100644 index 00000000..1b0087ed --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/context/mock/SaTokenContextMockUtil.java @@ -0,0 +1,75 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * 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 cn.dev33.satoken.context.mock; + +import cn.dev33.satoken.SaManager; +import cn.dev33.satoken.fun.SaFunction; +import cn.dev33.satoken.fun.SaRetGenericFunction; + +/** + * Sa-Token Mock 上下文 操作工具类 + * + * @author click33 + * @since 1.42.0 + */ +public class SaTokenContextMockUtil { + + /** + * 写入 Mock 上下文 + */ + public static void setMockContext() { + SaRequestForMock request = new SaRequestForMock(); + SaResponseForMock response = new SaResponseForMock(); + SaStorageForMock storage = new SaStorageForMock(); + SaManager.getSaTokenContext().setContext(request, response, storage); + } + + /** + * 写入 Mock 上下文,并执行一段代码,执行完毕后清除上下文 + * + * @param fun / + */ + public static void setMockContext(SaFunction fun) { + try { + setMockContext(); + fun.run(); + } finally { + clearContext(); + } + } + + /** + * 写入 Mock 上下文,并执行一段代码,执行完毕后清除上下文 + * + * @param fun / + */ + public static T setMockContext(SaRetGenericFunction fun) { + try { + setMockContext(); + return fun.run(); + } finally { + clearContext(); + } + } + + /** + * 清除上下文 + */ + public static void clearContext() { + SaManager.getSaTokenContext().clearContext(); + } + +}