Skip to content

Commit

Permalink
update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
naah69 committed Oct 12, 2022
1 parent 4a73a65 commit 62ffede
Show file tree
Hide file tree
Showing 24 changed files with 1,573 additions and 247 deletions.
1 change: 0 additions & 1 deletion quarkus-satoken-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<artifactId>quarkus-satoken-main</artifactId>
<packaging>pom</packaging>
<modules>
<module>quarkus-satoken-vertx</module>
<module>quarkus-satoken-resteasy</module>
</modules>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkiverse.satoken.resteasy.it;

import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.annotations.QuarkusMain;

@QuarkusMain
public class TestApplication {
public static void main(String... args) {
Quarkus.run(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package io.quarkiverse.satoken.resteasy.it.configuration;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import io.quarkiverse.satoken.core.filter.SaRouteInterceptor;
import io.quarkus.arc.Priority;
import io.quarkus.arc.Unremovable;
import io.quarkus.arc.profile.IfBuildProfile;
import io.vertx.ext.web.RoutingContext;
import org.jboss.resteasy.reactive.server.core.CurrentRequestManager;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.CDI;
import java.util.Arrays;

/**
* RouterFilterConfiguration
*
* @author nayan
* @date 2022/10/9 16:20
*/
@Dependent
public class RouterFilterConfiguration {

@Produces
@Unremovable
@Priority(1)
public SaRouteInterceptor configure2() {
return new SaRouteInterceptor("/**", handle -> {

// 匹配 getInfo ,返回code=201
SaRouter.match("/**")
.match(SaHttpMethod.POST)
.matchMethod("POST")
.match(SaHolder.getRequest().getMethod().equals("POST"))
.match(r -> SaHolder.getRequest().isPath("/rt/getInfo"))
.match(r -> SaHolder.getRequest().isParam("name", "zhang"))
.back(SaResult.code(201));

// 匹配 getInfo2 ,返回code=202
SaRouter.match("/rt/getInfo2")
.match(Arrays.asList("/rt/getInfo2", "/rt/*"))
.notMatch("/rt/getInfo3")
.notMatch(false)
.notMatch(r -> false)
.notMatch(SaHttpMethod.GET)
.notMatchMethod("PUT")
.notMatch(Arrays.asList("/rt/getInfo4", "/rt/getInfo5"))
.back(SaResult.code(202));

// 匹配 getInfo3 ,返回code=203
SaRouter.match("/rt/getInfo3", "/rt/getInfo4", () -> SaRouter.back(SaResult.code(203)));
SaRouter.match("/rt/getInfo4", "/rt/getInfo5", r -> SaRouter.back(SaResult.code(204)));
SaRouter.match("/rt/getInfo5", () -> SaRouter.back(SaResult.code(205)));
SaRouter.match("/rt/getInfo6", r -> SaRouter.back(SaResult.code(206)));

// 通往 Controller
SaRouter.match(Arrays.asList("/rt/getInfo7")).stop();

// 通往 Controller
SaRouter.match("/rt/getInfo8", () -> SaRouter.stop());

SaRouter.matchMethod("POST").match("/rt/getInfo9").free(r -> SaRouter.back(SaResult.code(209)));
SaRouter.match(SaHttpMethod.POST).match("/rt/getInfo10").setHit(false).back();

// 11
SaRouter.notMatch("/rt/getInfo11").reset().match("/rt/getInfo11").back(SaResult.code(211));
SaRouter.notMatch(SaHttpMethod.GET).match("/rt/getInfo12").back(SaResult.code(212));
SaRouter.notMatch(Arrays.asList("/rt/getInfo12", "/rt/getInfo14")).match("/rt/getInfo13").back(SaResult.code(213));
SaRouter.notMatchMethod("GET", "PUT").match("/rt/getInfo14").back(SaResult.code(214));

// SaRouter.match(Arrays.asList("/rt/getInfo15", "/rt/getInfo16"))
ResteasyReactiveRequestContext resteasyReactiveRequestContext = CurrentRequestManager.get();
if (SaRouter.isMatchCurrURI("/rt/getInfo15")) {
if (SaHolder.getRequest().getCookieValue("ddd") == null
&& SaHolder.getStorage().getSource() ==resteasyReactiveRequestContext
&& SaHolder.getRequest().getSource() == resteasyReactiveRequestContext
&& SaHolder.getResponse().getSource() == resteasyReactiveRequestContext.serverResponse()
) {
SaRouter.newMatch().free(r -> SaRouter.back(SaResult.code(215)));
}
}

SaRouter.match("/rt/getInfo16", () -> {
try {
SaHolder.getResponse().redirect(null);
} catch (Exception e) {

}
SaHolder.getResponse().redirect("/rt/getInfo3");
});
});
}

@Produces
@Unremovable
@IfBuildProfile("route")
@Priority(100)
public SaRouteInterceptor configure31() {
return new SaRouteInterceptor("/rt/getInfo_200", handle -> {
SaRouter.stop();
});
}

@Produces
@Unremovable
@IfBuildProfile("route")
@Priority(100)
public SaRouteInterceptor configure32() {
return new SaRouteInterceptor("/rt/getInfo_201", handle -> {
StpUtil.checkLogin();
});
}

@Produces
@Unremovable
@IfBuildProfile("route")
@Priority(101)
public SaRouteInterceptor configure33() {
return new SaRouteInterceptor("/rt/getInfo_201", handle -> {
SaRouter.back(SaResult.code(201));
});
}

@Produces
@Unremovable
@IfBuildProfile("route")
@Priority(100)
public SaRouteInterceptor configure34() {
return new SaRouteInterceptor("/rt/getInfo_202", handle -> {
StpUtil.checkLogin();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.quarkiverse.satoken.resteasy.it.configuration;

import java.util.Arrays;
import java.util.List;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.util.SaFoxUtil;
import io.quarkus.arc.DefaultBean;
import io.quarkus.arc.Unremovable;
import io.quarkus.arc.profile.IfBuildProfile;

/**
* StpInterfaceConfiguration
*
* @author nayan
* @date 2022/10/8 18:55
*/

@Dependent
public class StpInterfaceConfiguration {

@Produces
@Unremovable
@IfBuildProfile("annotation")
public StpInterface annotation() {
return new StpInterface() {
/**
* 返回一个账号所拥有的权限码集合
*/
@Override
public List<String> getPermissionList(Object loginId, String loginType) {
int id = SaFoxUtil.getValueByType(loginId, int.class);
if (id == 10001) {
return Arrays.asList("user*", "art-add", "art-delete", "art-update", "art-get");
} else {
return null;
}
}

/**
* 返回一个账号所拥有的角色标识集合
*/
@Override
public List<String> getRoleList(Object loginId, String loginType) {
int id = SaFoxUtil.getValueByType(loginId, int.class);
if (id == 10001) {
return Arrays.asList("admin", "super-admin");
} else {
return null;
}
}
};
}

@Produces
@Unremovable
@DefaultBean
public StpInterface noopTracer() {
return new StpInterface() {
/**
* 返回一个账号所拥有的权限码集合
*/
@Override
public List<String> getPermissionList(Object loginId, String loginType) {
int id = SaFoxUtil.getValueByType(loginId, int.class);
if(id == 10001) {
return Arrays.asList("user*", "art-add", "art-delete", "art-update", "art-get");
} else {
return null;
}
}

/**
* 返回一个账号所拥有的角色标识集合
*/
@Override
public List<String> getRoleList(Object loginId, String loginType) {
int id = SaFoxUtil.getValueByType(loginId, int.class);
if(id == 10001) {
return Arrays.asList("admin", "super-admin");
} else {
return null;
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.quarkiverse.satoken.resteasy.it;
package io.quarkiverse.satoken.resteasy.it.integrate;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
Expand All @@ -27,7 +28,7 @@

@Path("/acc/")
@ApplicationScoped
public class SatokenResteasyResource {
public class LoginResource {

// 测试登录 ---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
@POST
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 io.quarkiverse.satoken.resteasy.it.integrate;

import cn.dev33.satoken.basic.SaBasicUtil;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

@Path("/more/")
@ApplicationScoped
public class MoreResource {

// 一些基本的测试
@POST
@Path("getInfo")
public SaResult getInfo() {
SaRequest req = SaHolder.getRequest();
boolean flag =
SaFoxUtil.equals(req.getParam("name"), "zhang")
&& SaFoxUtil.equals(req.getParam("name2", "li"), "li")
&& SaFoxUtil.equals(req.getParamNotNull("name"), "zhang")
&& req.isParam("name", "zhang")
&& req.isPath("/more/getInfo")
&& req.hasParam("name")
&& SaFoxUtil.equals(req.getHeader("div"), "val")
&& SaFoxUtil.equals(req.getHeader("div", "zhang"), "val")
&& SaFoxUtil.equals(req.getHeader("div2", "zhang"), "zhang");

SaHolder.getResponse().setServer("sa-server");
return SaResult.data(flag);
}

// Http Basic 认证
@POST
@Path("basicAuth")
public SaResult basicAuth() {
SaBasicUtil.check("sa:123456");
return SaResult.ok();
}
}
Loading

0 comments on commit 62ffede

Please sign in to comment.