Skip to content

An easy way to test v2 handler plugins #3603

@00LT00

Description

@00LT00

I update my plugins from v1.0.5 to v2. But now I don't know how can I test it.
It's a test for v1.

type Authorizer struct {
	Next     httpserver.Handler
	Enforcer *casbin.Enforcer
}

func testRequest(t *testing.T, handler Authorizer, user string, path string, method string, code int) {
	r, _ := http.NewRequest(method, path, nil)
	r.SetBasicAuth(user, "123")
	w := httptest.NewRecorder()
	handler.ServeHTTP(w, r)

	if w.Code != code {
		t.Errorf("%s, %s, %s: %d, supposed to be %d", user, path, method, w.Code, code)
	}
}
func TestBasic(t *testing.T) {
	e, _ := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")

	handler := Authorizer{
		Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
			return http.StatusOK, nil
		}),
		Enforcer: e,
	}

	testRequest(t, handler, "alice", "/dataset1/resource1", "GET", 200)
	testRequest(t, handler, "alice", "/dataset1/resource1", "POST", 200)
	testRequest(t, handler, "alice", "/dataset1/resource2", "GET", 200)
	testRequest(t, handler, "alice", "/dataset1/resource2", "POST", 403)
}

Could you give me some examples about this plugin?
Thanks again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions