From 4953f83835904a2334707495f764f73ff385094a Mon Sep 17 00:00:00 2001 From: karldoenitz Date: Thu, 12 Oct 2023 15:52:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_case/utils_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test_case/utils_test.go diff --git a/test_case/utils_test.go b/test_case/utils_test.go new file mode 100644 index 0000000..794b0d7 --- /dev/null +++ b/test_case/utils_test.go @@ -0,0 +1,26 @@ +package test_case + +import ( + "Tigo/TigoWeb" + "testing" +) + +func TestEncryptDecrypt(t *testing.T) { + originData := "test data" + key := "key" + encryptData, err := TigoWeb.Encrypt([]byte(originData), []byte(key)) + if err != nil { + t.Error(err.Error()) + return + } + decryptData, err := TigoWeb.Decrypt([]byte(encryptData), []byte(key)) + if err != nil { + t.Error(err.Error()) + return + } + if string(decryptData) != originData { + t.Error("data invalid") + return + } + t.Log("success") +} From 3d29a3fe1f46cc84718596be890ad79fc2931e73 Mon Sep 17 00:00:00 2001 From: karldoenitz Date: Fri, 13 Oct 2023 22:04:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9method=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TigoWeb/enums.go | 44 +++++++++++++++++++++++-------------------- TigoWeb/urlpattern.go | 12 ++++++------ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/TigoWeb/enums.go b/TigoWeb/enums.go index 5dc496f..23288f7 100644 --- a/TigoWeb/enums.go +++ b/TigoWeb/enums.go @@ -1,23 +1,27 @@ package TigoWeb -// MethodMapping http请求方式的一个映射 -var MethodMapping = map[string]string{ - "get": "Get", - "head": "Head", - "post": "Post", - "put": "Put", - "delete": "Delete", - "connect": "Connect", - "options": "Options", - "trace": "Trace", - "GET": "Get", - "HEAD": "Head", - "POST": "Post", - "PUT": "Put", - "DELETE": "Delete", - "CONNECT": "Connect", - "OPTIONS": "Options", - "TRACE": "Trace", -} - const Version = "1.6.5" + +// MethodEnum 根据http请求方式获取对应的函数名 +// - httpMethod: http请求方式 +func MethodEnum(httpMethod string) string { + var MethodMapping = map[string]string{ + "get": "Get", + "head": "Head", + "post": "Post", + "put": "Put", + "delete": "Delete", + "connect": "Connect", + "options": "Options", + "trace": "Trace", + "GET": "Get", + "HEAD": "Head", + "POST": "Post", + "PUT": "Put", + "DELETE": "Delete", + "CONNECT": "Connect", + "OPTIONS": "Options", + "TRACE": "Trace", + } + return MethodMapping[httpMethod] +} diff --git a/TigoWeb/urlpattern.go b/TigoWeb/urlpattern.go index 47d359a..8963962 100644 --- a/TigoWeb/urlpattern.go +++ b/TigoWeb/urlpattern.go @@ -22,11 +22,11 @@ type UrlPatternHandle struct { } // Handle 封装HTTP请求的中间件,主要有以下功能: -// - 1、根据反射找到挂载的handler; -// - 2、调用handler的InitHandler方法; -// - 3、进行HTTP请求预处理,包括判断请求方式是否合法等; -// - 4、调用handler中的功能方法; -// - 5、进行HTTP请求结束处理。 +// - 1、根据反射找到挂载的handler; +// - 2、调用handler的InitHandler方法; +// - 3、进行HTTP请求预处理,包括判断请求方式是否合法等; +// - 4、调用handler中的功能方法; +// - 5、进行HTTP请求结束处理。 func (urlPatternMidWare UrlPatternHandle) Handle(responseWriter http.ResponseWriter, request *http.Request) { handlerType := reflect.TypeOf(urlPatternMidWare.Handler) if handlerType.Kind() == reflect.Ptr { @@ -41,7 +41,7 @@ func (urlPatternMidWare UrlPatternHandle) Handle(responseWriter http.ResponseWri // 调用BeforeRequest方法 VoidFuncCall(handler, FnBeforeRequest) // 根据http请求方式调用相关方法 - VoidFuncCall(handler, MethodMapping[request.Method]) + VoidFuncCall(handler, MethodEnum(request.Method)) // 调用TeardownRequest方法 VoidFuncCall(handler, FnTeardownRequest) }