Exemplo n.º 1
0
func appUserToken(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	// get t from ctx
	t := ctx.Value("test").(*testing.T)

	// Test whether the user is authenticated
	if r.Header.Get("X-Identity-Status") != "Confirmed" {
		t.Errorf("At the app, the user token should already be confirmed, got: %v", r.Header.Get("X-Identity-Status"))
	}
	if r.Header.Get("X-User-Id") != "10a2e6e717a245d9acad3e5f97aeca3d" {
		t.Errorf("At the app, the user should be 10a2e6e717a245d9acad3e5f97aeca3d, got: %v", r.Header.Get("X-User-Id"))
	}
	if r.Header.Get("X-User-Name") != "testuser" {
		t.Errorf("At the app, the user should be testuser, got: %v", r.Header.Get("X-User-Name"))
	}
	if r.Header.Get("X-Domain-Id") != "default" {
		t.Errorf("At the app, the user should be default, got: %v", r.Header.Get("X-Domain-Id"))
	}

	// Test ctx's Context Parameter with key "UserAccessInfo"
	value := router.MiddlewareParam(ctx, UserAccessInfoKey)
	if value == nil {
		t.Error("ctx should contain user access info")
	} else {
		access, ok := value.(*client.AccessInfo)
		if !ok {
			t.Error("it is not accessinfo, what is it?")
		}
		if access.Token != "usertoken" || access.TokenInfo.User.Domain.Name != "Default" {
			t.Error("ctx's accessinfo contains wrong information")
		}
	}

	w.Write([]byte("Test success!"))
}
Exemplo n.º 2
0
func appUserTokenInvalid(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	// get t from ctx
	t := ctx.Value("test").(*testing.T)

	// Test whether the user is authenticated
	if r.Header.Get("X-Identity-Status") != "Invalid" {
		t.Errorf("At the app, the user token should already be invalid, got: %v", r.Header.Get("X-Identity-Status"))
	}
	if r.Header.Get("X-User-Id") != "" {
		t.Errorf("At the app, the user should be empty, got: %v", r.Header.Get("X-User-Id"))
	}

	// Test ctx's Context Parameter with key "UserAccessInfo"
	value := router.MiddlewareParam(ctx, UserAccessInfoKey)
	if value != nil {
		t.Error("ctx should not contain user access info")
	}

	w.Write([]byte("Test success!"))
}
Exemplo n.º 3
0
func EndLogger(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
	endTime := time.Now()
	var buf bytes.Buffer

	buf.WriteString("Returning ")
	buf.WriteString(" in ")

	startTime, ok := router.MiddlewareParam(ctx, "start_time").(time.Time)
	if ok {
		dt := endTime.Sub(startTime)
		if dt < 500*time.Millisecond {
			cW(&buf, nGreen, "%s", dt)
		} else if dt < 5*time.Second {
			cW(&buf, nYellow, "%s", dt)
		} else {
			cW(&buf, nRed, "%s", dt)
		}
	} else {
		buf.WriteString("unknown time")
	}

	log.Print(buf.String())
	return ctx
}