mirror of
https://github.com/kataras/iris.git
synced 2026-05-14 18:13:49 +00:00
add the stale release
This commit is contained in:
@@ -8,8 +8,8 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -3,7 +3,7 @@ package basicauth
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12/core/router"
|
||||
"github.com/kataras/iris/core/router"
|
||||
)
|
||||
|
||||
// New returns a new gRPC Iris router wrapper for a gRPC server.
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
// The Iris server SHOULD run under HTTP/2 and clients too.
|
||||
//
|
||||
// Usage:
|
||||
// import grpcWrapper "github.com/kataras/iris/v12/middleware/grpc"
|
||||
// import grpcWrapper "github.com/kataras/iris/middleware/grpc"
|
||||
// [...]
|
||||
// app := iris.New()
|
||||
// grpcServer := grpc.NewServer()
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/square/go-jose/v3"
|
||||
"github.com/square/go-jose/v3/jwt"
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
// Package jwt_test contains simple Iris jwt tests. Most of the jwt functionality is already tested inside the jose package itself.
|
||||
package jwt_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
"github.com/kataras/iris/v12/middleware/jwt"
|
||||
)
|
||||
|
||||
type userClaims struct {
|
||||
jwt.Claims
|
||||
Username string
|
||||
}
|
||||
|
||||
const testMaxAge = 3 * time.Second
|
||||
|
||||
// Random RSA verification and encryption.
|
||||
func TestRSA(t *testing.T) {
|
||||
j := jwt.RSA(testMaxAge)
|
||||
t.Cleanup(func() {
|
||||
os.Remove(jwt.DefaultSignFilename)
|
||||
os.Remove(jwt.DefaultEncFilename)
|
||||
})
|
||||
testWriteVerifyToken(t, j)
|
||||
}
|
||||
|
||||
// HMAC verification and encryption.
|
||||
func TestHMAC(t *testing.T) {
|
||||
j := jwt.HMAC(testMaxAge, "secret", "itsa16bytesecret")
|
||||
testWriteVerifyToken(t, j)
|
||||
}
|
||||
|
||||
func TestNew_HMAC(t *testing.T) {
|
||||
j, err := jwt.New(testMaxAge, jwt.HS256, []byte("secret"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = j.WithEncryption(jwt.A128GCM, jwt.DIRECT, []byte("itsa16bytesecret"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
testWriteVerifyToken(t, j)
|
||||
}
|
||||
|
||||
// HMAC verification only (unecrypted).
|
||||
func TestVerify(t *testing.T) {
|
||||
j, err := jwt.New(testMaxAge, jwt.HS256, []byte("another secret"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
testWriteVerifyToken(t, j)
|
||||
}
|
||||
|
||||
func testWriteVerifyToken(t *testing.T, j *jwt.JWT) {
|
||||
t.Helper()
|
||||
|
||||
j.Extractors = append(j.Extractors, jwt.FromJSON("access_token"))
|
||||
standardClaims := jwt.Claims{Issuer: "an-issuer", Audience: jwt.Audience{"an-audience"}}
|
||||
expectedClaims := userClaims{
|
||||
Claims: j.Expiry(standardClaims),
|
||||
Username: "kataras",
|
||||
}
|
||||
|
||||
app := iris.New()
|
||||
app.Get("/auth", func(ctx iris.Context) {
|
||||
j.WriteToken(ctx, expectedClaims)
|
||||
})
|
||||
|
||||
app.Post("/restricted", func(ctx iris.Context) {
|
||||
var claims userClaims
|
||||
if err := j.VerifyToken(ctx, &claims); err != nil {
|
||||
ctx.StopWithStatus(iris.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(claims)
|
||||
})
|
||||
|
||||
app.Post("/restricted_middleware_readclaims", j.Verify, func(ctx iris.Context) {
|
||||
var claims userClaims
|
||||
if err := jwt.ReadClaims(ctx, &claims); err != nil {
|
||||
ctx.StopWithStatus(iris.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(claims)
|
||||
})
|
||||
|
||||
app.Post("/restricted_middleware_get", j.Verify, func(ctx iris.Context) {
|
||||
claims, err := jwt.Get(ctx)
|
||||
if err != nil {
|
||||
ctx.StopWithStatus(iris.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(claims)
|
||||
})
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
// Get token.
|
||||
rawToken := e.GET("/auth").Expect().Status(httptest.StatusOK).Body().Raw()
|
||||
if rawToken == "" {
|
||||
t.Fatalf("empty token")
|
||||
}
|
||||
|
||||
restrictedPaths := [...]string{"/restricted", "/restricted_middleware_readclaims", "/restricted_middleware_get"}
|
||||
|
||||
now := time.Now()
|
||||
for _, path := range restrictedPaths {
|
||||
// Authorization Header.
|
||||
e.POST(path).WithHeader("Authorization", "Bearer "+rawToken).Expect().
|
||||
Status(httptest.StatusOK).JSON().Equal(expectedClaims)
|
||||
|
||||
// URL Query.
|
||||
e.POST(path).WithQuery("token", rawToken).Expect().
|
||||
Status(httptest.StatusOK).JSON().Equal(expectedClaims)
|
||||
|
||||
// JSON Body.
|
||||
e.POST(path).WithJSON(iris.Map{"access_token": rawToken}).Expect().
|
||||
Status(httptest.StatusOK).JSON().Equal(expectedClaims)
|
||||
|
||||
// Missing "Bearer".
|
||||
e.POST(path).WithHeader("Authorization", rawToken).Expect().
|
||||
Status(httptest.StatusUnauthorized)
|
||||
}
|
||||
expireRemDur := testMaxAge - time.Since(now)
|
||||
|
||||
// Expiration.
|
||||
time.Sleep(expireRemDur /* -end */)
|
||||
for _, path := range restrictedPaths {
|
||||
e.POST(path).WithQuery("token", rawToken).Expect().Status(httptest.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package logger
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// The SkipperFunc signature, used to serve the main request without logs.
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/ryanuber/columnize"
|
||||
)
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/core/router"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/router"
|
||||
)
|
||||
|
||||
type options struct {
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
package methodoverride_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
"github.com/kataras/iris/v12/middleware/methodoverride"
|
||||
)
|
||||
|
||||
func TestMethodOverrideWrapper(t *testing.T) {
|
||||
app := iris.New()
|
||||
|
||||
mo := methodoverride.New(
|
||||
// Defaults to nil.
|
||||
//
|
||||
methodoverride.SaveOriginalMethod("_originalMethod"),
|
||||
// Default values.
|
||||
//
|
||||
// methodoverride.Methods(http.MethodPost),
|
||||
// methodoverride.Headers("X-HTTP-Method", "X-HTTP-Method-Override", "X-Method-Override"),
|
||||
// methodoverride.FormField("_method"),
|
||||
// methodoverride.Query("_method"),
|
||||
)
|
||||
// Register it with `WrapRouter`.
|
||||
app.WrapRouter(mo)
|
||||
|
||||
var (
|
||||
expectedDelResponse = "delete resp"
|
||||
expectedPostResponse = "post resp"
|
||||
)
|
||||
|
||||
app.Post("/path", func(ctx iris.Context) {
|
||||
ctx.WriteString(expectedPostResponse)
|
||||
})
|
||||
|
||||
app.Delete("/path", func(ctx iris.Context) {
|
||||
ctx.WriteString(expectedDelResponse)
|
||||
})
|
||||
|
||||
app.Delete("/path2", func(ctx iris.Context) {
|
||||
_, err := ctx.Writef("%s%s", expectedDelResponse, ctx.Request().Context().Value("_originalMethod"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
// Test headers.
|
||||
e.POST("/path").WithHeader("X-HTTP-Method", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
e.POST("/path").WithHeader("X-HTTP-Method-Override", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
e.POST("/path").WithHeader("X-Method-Override", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
|
||||
// Test form field value.
|
||||
e.POST("/path").WithFormField("_method", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
|
||||
// Test URL Query (although it's the same as form field in this case).
|
||||
e.POST("/path").WithQuery("_method", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
|
||||
// Test saved original method and
|
||||
// Test without registered "POST" route.
|
||||
e.POST("/path2").WithQuery("_method", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse + iris.MethodPost)
|
||||
|
||||
// Test simple POST request without method override fields.
|
||||
e.POST("/path").Expect().Status(iris.StatusOK).Body().Equal(expectedPostResponse)
|
||||
|
||||
// Test simple DELETE request.
|
||||
e.DELETE("/path").Expect().Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
}
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
rpprof "runtime/pprof"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/core/handlerconv"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/handlerconv"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/core/netutil"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/netutil"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"encoding/hex"
|
||||
"net/http/httputil"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package requestid_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
"github.com/kataras/iris/v12/middleware/requestid"
|
||||
)
|
||||
|
||||
func TestRequestID(t *testing.T) {
|
||||
app := iris.New()
|
||||
h := func(ctx iris.Context) {
|
||||
ctx.WriteString(requestid.Get(ctx))
|
||||
}
|
||||
|
||||
def := app.Party("/default")
|
||||
{
|
||||
def.Use(requestid.New())
|
||||
def.Get("/", h)
|
||||
}
|
||||
|
||||
const expectedCustomID = "my_id"
|
||||
custom := app.Party("/custom")
|
||||
{
|
||||
customGen := func(ctx *context.Context) string {
|
||||
return expectedCustomID
|
||||
}
|
||||
|
||||
custom.Use(requestid.New(customGen))
|
||||
custom.Get("/", h)
|
||||
}
|
||||
|
||||
const expectedErrMsg = "no id"
|
||||
customWithErr := app.Party("/custom_err")
|
||||
{
|
||||
customGen := func(ctx *context.Context) string {
|
||||
ctx.StopWithText(iris.StatusUnauthorized, expectedErrMsg)
|
||||
return ""
|
||||
}
|
||||
|
||||
customWithErr.Use(requestid.New(customGen))
|
||||
customWithErr.Get("/", h)
|
||||
}
|
||||
|
||||
const expectedCustomIDFromOtherMiddleware = "my custom id"
|
||||
changeID := app.Party("/custom_change_id")
|
||||
{
|
||||
changeID.Use(func(ctx iris.Context) {
|
||||
ctx.SetID(expectedCustomIDFromOtherMiddleware)
|
||||
ctx.Next()
|
||||
})
|
||||
changeID.Use(requestid.New())
|
||||
changeID.Get("/", h)
|
||||
}
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/default").Expect().Status(httptest.StatusOK).Body().NotEmpty()
|
||||
e.GET("/custom").Expect().Status(httptest.StatusOK).Body().Equal(expectedCustomID)
|
||||
e.GET("/custom_err").Expect().Status(httptest.StatusUnauthorized).Body().Equal(expectedErrMsg)
|
||||
e.GET("/custom_change_id").Expect().Status(httptest.StatusOK).Body().Equal(expectedCustomIDFromOtherMiddleware)
|
||||
}
|
||||
Reference in New Issue
Block a user