Skip to content

Commit 4e9f910

Browse files
authored
Merge pull request #48 from xushiwei/test
jwtdemo
2 parents e17001a + ebd52f5 commit 4e9f910

File tree

5 files changed

+90
-6
lines changed

5 files changed

+90
-6
lines changed

ytest/auth/jwt/jwt.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ func newSign(method jwt.SigningMethod, secret any) *Signaturer {
7676
}
7777

7878
// HS256 creates a signing methods by using the HMAC-SHA256.
79-
func HS256(key []byte) *Signaturer {
80-
return newSign(jwt.SigningMethodHS256, key)
79+
func HS256(key string) *Signaturer {
80+
return newSign(jwt.SigningMethodHS256, []byte(key))
8181
}
8282

8383
// HS384 creates a signing methods by using the HMAC-SHA384.
84-
func HS384(key []byte) *Signaturer {
85-
return newSign(jwt.SigningMethodHS384, key)
84+
func HS384(key string) *Signaturer {
85+
return newSign(jwt.SigningMethodHS384, []byte(key))
8686
}
8787

8888
// HS512 creates a signing methods by using the HMAC-SHA512.
89-
func HS512(key []byte) *Signaturer {
90-
return newSign(jwt.SigningMethodHS512, key)
89+
func HS512(key string) *Signaturer {
90+
return newSign(jwt.SigningMethodHS512, []byte(key))
9191
}
9292

9393
// -----------------------------------------------------------------------------

ytest/demo/jwtdemo/gop_autogen.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import "github.com/goplus/yap"
4+
5+
const _ = true
6+
7+
type jwtdemo struct {
8+
yap.App
9+
}
10+
//line ytest/demo/jwtdemo/jwtdemo_yap.gox:1
11+
func (this *jwtdemo) MainEntry() {
12+
//line ytest/demo/jwtdemo/jwtdemo_yap.gox:1:1
13+
this.Get("/p/:id", func(ctx *yap.Context) {
14+
//line ytest/demo/jwtdemo/jwtdemo_yap.gox:2:1
15+
ctx.Json__1(map[string]string{"id": ctx.Param("id")})
16+
})
17+
//line ytest/demo/jwtdemo/jwtdemo_yap.gox:7:1
18+
this.Run(":8080")
19+
}
20+
func main() {
21+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:11:1
22+
yap.Gopt_App_Main(new(jwtdemo))
23+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"github.com/goplus/yap/ytest"
5+
"github.com/goplus/yap/ytest/auth/jwt"
6+
"testing"
7+
"time"
8+
)
9+
10+
type case_jwtdemo struct {
11+
ytest.Case
12+
}
13+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:7
14+
func (this *case_jwtdemo) Main() {
15+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:7:1
16+
this.Mock("foo.com", new(jwtdemo))
17+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:9:1
18+
testuser := jwt.HS256("<secret key>").Audience("testuser").Expiration(time.Now().Add(time.Hour))
19+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:11:1
20+
this.Run("test get /p/$id", func() {
21+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:12:1
22+
id := "123"
23+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:13:1
24+
this.Get("http://foo.com/p/" + id)
25+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:14:1
26+
this.Auth(testuser)
27+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:15:1
28+
this.RetWith(200)
29+
//line ytest/demo/jwtdemo/jwtdemo_ytest.gox:16:1
30+
this.Json(map[string]string{"id": id})
31+
})
32+
}
33+
func Test_jwtdemo(t *testing.T) {
34+
ytest.Gopt_Case_TestMain(new(case_jwtdemo), t)
35+
}

ytest/demo/jwtdemo/jwtdemo_yap.gox

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
get "/p/:id", ctx => {
2+
ctx.json {
3+
"id": ctx.param("id"),
4+
}
5+
}
6+
7+
run ":8080"

ytest/demo/jwtdemo/jwtdemo_ytest.gox

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import (
2+
"time"
3+
4+
"github.com/goplus/yap/ytest/auth/jwt"
5+
)
6+
7+
mock "foo.com", new(jwtdemo)
8+
9+
testuser := jwt.HS256("<secret key>").audience("testuser").expiration(time.now.add(time.Hour))
10+
11+
run "test get /p/$id", => {
12+
id := "123"
13+
get "http://foo.com/p/${id}"
14+
auth testuser
15+
ret 200
16+
json {
17+
"id": id,
18+
}
19+
}

0 commit comments

Comments
 (0)