Skip to content

Commit

Permalink
Renew Session 支持 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash authored Mar 3, 2024
1 parent f43be2d commit e5e81e8
Show file tree
Hide file tree
Showing 17 changed files with 448 additions and 105 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,5 @@ jobs:
- name: Build
run: go build -v ./...

- name: Test
run: go test -race -coverprofile=cover.out -v ./...

- name: Post Coverage
uses: codecov/codecov-action@v2
- name: Unit Test
run: go test -race -v ./...
5 changes: 4 additions & 1 deletion .github/workflows/integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ jobs:
go-version: '1.21.1'

- name: Test
run: make e2e
run: make e2e

- name: Post Coverage
uses: codecov/codecov-action@v2
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
# Go workspace file
go.work

.idea
.idea
*.out
.run
2 changes: 1 addition & 1 deletion .script/integrate_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
set -e
docker compose -f .script/integration_test_compose.yml down
docker compose -f .script/integration_test_compose.yml up -d
go test ./... -tags=e2e
go test -race -coverprofile=cover.out -tags=e2e ./...
docker compose -f .script/integration_test_compose.yml down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

//go:build e2e

package integration
package e2e

import (
"context"
Expand All @@ -25,19 +25,13 @@ import (
"time"

"github.com/ecodeclub/ginx/middlewares/activelimit/redislimit"
"github.com/redis/go-redis/v9"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBuilder_e2e_ActiveRedisLimit(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:16379",
Password: "",
DB: 0,
})
redisClient := newRedisTestClient()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
err := redisClient.Ping(ctx).Err()
Expand Down Expand Up @@ -160,6 +154,7 @@ func TestBuilder_e2e_ActiveRedisLimit(t *testing.T) {
time.Sleep(time.Millisecond * 100)
redisClient.Del(context.Background(), tc.key)
fmt.Println(redisClient.Get(context.Background(), tc.key).Int64())
tc := tc
t.Run(tc.name, func(t *testing.T) {

server := gin.Default()
Expand Down
37 changes: 37 additions & 0 deletions internal/e2e/base_suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 ecodeclub
//
// Licensed 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.

//go:build e2e

package e2e

import (
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/suite"
)

type BaseSuite struct {
suite.Suite
RDB redis.Cmdable
}

func (s *BaseSuite) SetupSuite() {
s.RDB = newRedisTestClient()
}

func (s *BaseSuite) TearDownSuite() {
if s.RDB != nil {
s.RDB.(*redis.Client).Close()
}
}
29 changes: 29 additions & 0 deletions internal/e2e/dependency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 ecodeclub
//
// Licensed 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.

//go:build e2e

package e2e

import "github.com/redis/go-redis/v9"

var redisCfg = &redis.Options{
Addr: "localhost:16379",
Password: "",
DB: 0,
}

func newRedisTestClient() *redis.Client {
return redis.NewClient(redisCfg)
}
61 changes: 61 additions & 0 deletions internal/e2e/gin_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2023 ecodeclub
//
// Licensed 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 e2e

import (
"bufio"
"net"
"net/http"
)

type GinResponseWriter struct {
http.ResponseWriter
}

func (g *GinResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
panic("implement me")
}

func (g *GinResponseWriter) Flush() {
panic("implement me")
}

func (g *GinResponseWriter) CloseNotify() <-chan bool {
panic("implement me")
}

func (g *GinResponseWriter) Status() int {
panic("implement me")
}

func (g *GinResponseWriter) Size() int {
panic("implement me")
}

func (g *GinResponseWriter) WriteString(s string) (int, error) {
panic("implement me")
}

func (g *GinResponseWriter) Written() bool {
panic("implement me")
}

func (g *GinResponseWriter) WriteHeaderNow() {
panic("implement me")
}

func (g *GinResponseWriter) Pusher() http.Pusher {
panic("implement me")
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

//go:build e2e

package integration
package e2e

import (
"context"
Expand Down
4 changes: 4 additions & 0 deletions session/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ func SetDefaultProvider(sp Provider) {
func CheckLoginMiddleware() gin.HandlerFunc {
return (&MiddlewareBuilder{sp: defaultProvider}).Build()
}

func RenewAccessToken(ctx *gctx.Context) error {
return defaultProvider.RenewAccessToken(ctx)
}
11 changes: 11 additions & 0 deletions session/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@ import (
"github.com/ecodeclub/ginx/internal/errs"
)

var _ Session = &MemorySession{}

// MemorySession 一般用于测试
type MemorySession struct {
data map[string]any
claims Claims
}

func (m *MemorySession) Destroy(ctx context.Context) error {
return nil
}

func (m *MemorySession) Del(ctx context.Context, key string) error {
delete(m.data, key)
return nil
}

func NewMemorySession(cl Claims) *MemorySession {
return &MemorySession{
data: map[string]any{},
Expand Down
42 changes: 42 additions & 0 deletions session/provider.mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e5e81e8

Please sign in to comment.