Skip to content

Commit ea25f04

Browse files
committed
feat(mimetype/toml): 添加 TOML 支持
1 parent 841a40b commit ea25f04

File tree

6 files changed

+135
-4
lines changed

6 files changed

+135
-4
lines changed

mimetype/cbor/cbor_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
// SPDX-FileCopyrightText: 2024 caixw
1+
// SPDX-FileCopyrightText: 2024-2025 caixw
22
//
33
// SPDX-License-Identifier: MIT
44

55
package cbor
66

7-
import "github.com/issue9/web"
7+
import (
8+
"testing"
9+
10+
"github.com/issue9/assert/v4"
11+
12+
"github.com/issue9/web"
13+
"github.com/issue9/web/mimetype/mimetypetest"
14+
)
815

916
var (
1017
_ web.MarshalFunc = Marshal
1118
_ web.UnmarshalFunc = Unmarshal
1219
)
20+
21+
func TestCBOR(t *testing.T) {
22+
a := assert.New(t, false)
23+
mimetypetest.Test(a, Marshal, Unmarshal)
24+
}

mimetype/gob/gob_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2018-2024 caixw
1+
// SPDX-FileCopyrightText: 2018-2025 caixw
22
//
33
// SPDX-License-Identifier: MIT
44

@@ -11,6 +11,7 @@ import (
1111
"github.com/issue9/assert/v4"
1212

1313
"github.com/issue9/web"
14+
"github.com/issue9/web/mimetype/mimetypetest"
1415
)
1516

1617
var (
@@ -43,4 +44,6 @@ func TestGOB(t *testing.T) {
4344

4445
data, err = Marshal(nil, nil)
4546
a.Error(err).Nil(data)
47+
48+
mimetypetest.Test(a, Marshal, Unmarshal)
4649
}

mimetype/mimetypetest/mimetypetest.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-FileCopyrightText: 2025 caixw
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
// Package mimetypetest 提供了用于测试 mimetype 的函数
6+
package mimetypetest
7+
8+
import (
9+
"bytes"
10+
"net/http"
11+
12+
"github.com/issue9/assert/v4"
13+
"golang.org/x/text/language"
14+
15+
"github.com/issue9/web"
16+
"github.com/issue9/web/server"
17+
"github.com/issue9/web/server/servertest"
18+
)
19+
20+
var inst = &web.Problem{
21+
Type: "https://example.com/probs/test",
22+
Title: "test",
23+
Status: 200,
24+
Detail: "test",
25+
Instance: "instance",
26+
}
27+
28+
func Test(a *assert.Assertion, m web.MarshalFunc, u web.UnmarshalFunc) {
29+
s, err := server.NewHTTP("test", "1.0", &server.Options{
30+
HTTPServer: &http.Server{Addr: ":8080"},
31+
Language: language.English,
32+
})
33+
a.NotError(err).NotNil(s)
34+
35+
s.Routers().New("main", nil).Get("/path", func(ctx *web.Context) web.Responser {
36+
data, err := m(ctx, inst)
37+
a.NotError(err).NotNil(data)
38+
39+
inst2 := &web.Problem{}
40+
a.NotError(u(bytes.NewBuffer(data), inst2))
41+
a.Equal(inst, inst2)
42+
43+
return web.OK(nil)
44+
})
45+
46+
defer servertest.Run(a, s)()
47+
defer s.Close(0)
48+
49+
servertest.Get(a, "http://localhost:8080/path").
50+
Do(nil).
51+
Status(http.StatusOK)
52+
}

mimetype/toml/toml.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-FileCopyrightText: 2025 caixw
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
// Package toml [TOML] 编码
6+
//
7+
// [TOML]: https://toml.io/cn/
8+
package toml
9+
10+
import (
11+
"io"
12+
13+
"github.com/BurntSushi/toml"
14+
15+
"github.com/issue9/web"
16+
)
17+
18+
const (
19+
Mimetype = "application/toml"
20+
ProblemMimetype = "application/problem+toml"
21+
)
22+
23+
func Marshal(_ *web.Context, v any) ([]byte, error) { return toml.Marshal(v) }
24+
25+
func Unmarshal(r io.Reader, v any) error {
26+
_, err := toml.NewDecoder(r).Decode(v)
27+
return err
28+
}

mimetype/toml/toml_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-FileCopyrightText: 2025 caixw
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
package toml
6+
7+
import (
8+
"testing"
9+
10+
"github.com/issue9/assert/v4"
11+
12+
"github.com/issue9/web"
13+
"github.com/issue9/web/mimetype/mimetypetest"
14+
)
15+
16+
var (
17+
_ web.MarshalFunc = Marshal
18+
_ web.UnmarshalFunc = Unmarshal
19+
)
20+
21+
func TestTOML(t *testing.T) {
22+
a := assert.New(t, false)
23+
mimetypetest.Test(a, Marshal, Unmarshal)
24+
}

mimetype/yaml/yaml_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@
44

55
package yaml
66

7-
import "github.com/issue9/web"
7+
import (
8+
"testing"
9+
10+
"github.com/issue9/assert/v4"
11+
12+
"github.com/issue9/web"
13+
"github.com/issue9/web/mimetype/mimetypetest"
14+
)
815

916
var (
1017
_ web.MarshalFunc = Marshal
1118
_ web.UnmarshalFunc = Unmarshal
1219
)
20+
21+
func TestYAML(t *testing.T) {
22+
a := assert.New(t, false)
23+
mimetypetest.Test(a, Marshal, Unmarshal)
24+
}

0 commit comments

Comments
 (0)