测试内容
测试AdroitFisherman分发包中SHAUtil模块。
测试用具
Django==5.0.3框架,AdroitFisherman==0.0.31
项目结构
路由设置
总路由
from django.contrib import admin
from django.urls import path,include
from Base64Util import urls
urlpatterns = [path('admin/', admin.site.urls),path('sha/',include(urls))
]
sha分路由
from django.urls import path
from . import views
urlpatterns=[path('index/',views.index),path('sha_api/',views.get_sha512)
]
路由逻辑实现
from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators import csrf
from AdroitFisherman.Utilities.SHAUtil import SHA512Encoder
def index(request):return render(template_name="test_page.html",request=request)
@csrf.csrf_exempt
def get_sha512(request):if request.method=='POST':encoder=SHA512Encoder()data=request.POST.get('data')en_data=encoder.sha512_encode(data)return JsonResponse(data={"status":"206","encode":en_data})else:return JsonResponse(data={"status":"408"})
test_page.html模板
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><script type="text/javascript" src="/static/js/jquery-3.6.0.min.js"></script></head>
<body>
<div>{% csrf_token %}<table><tr><td>请输入文字</td><td><input type="button" value="提交" name="submit"></td></tr><tr><td><textarea name="txt" cols="100" rows="10"></textarea></td></tr><tr><td><textarea name="encode" cols="100" rows="10"></textarea></td></tr></table><script>var jquery=$.noConflict();jquery("[name='submit']").click(function () {jquery.post("/sha/sha_api/",{"data":jquery("[name='txt']").val()},function (response){jquery("[name='encode']").val(response.encode);})});</script>
</div>
</body>
</html>