Skip to content

Commit ab1047c

Browse files
authored
Merge pull request #754 from marle3003/develop
Develop
2 parents c8e0f5a + 117ceff commit ab1047c

File tree

101 files changed

+3510
-780
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+3510
-780
lines changed

acceptance/mail_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type MailSuite struct{ BaseSuite }
2323

2424
func (suite *MailSuite) SetupSuite() {
2525
cfg := static.NewConfig()
26+
port := try.GetFreePort()
27+
cfg.Api.Port = fmt.Sprintf("%v", port)
2628
wd, err := os.Getwd()
2729
require.NoError(suite.T(), err)
2830
cfg.ConfigFile = path.Join(wd, "mokapi.yaml")
@@ -47,16 +49,16 @@ func (suite *MailSuite) TestSendMail() {
4749
require.NoError(suite.T(), err)
4850

4951
// test mail API
50-
try.GetRequest(suite.T(), "http://localhost:8080/api/services/mail/Mokapi%20MailServer/mailboxes", nil,
52+
try.GetRequest(suite.T(), fmt.Sprintf("http://localhost:%v/api/services/mail/Mokapi%%20MailServer/mailboxes", suite.cfg.Api.Port), nil,
5153
try.HasStatusCode(200),
5254
try.HasBody(`[{"name":"[email protected]","numMessages":1}]`),
5355
)
54-
try.GetRequest(suite.T(), "http://localhost:8080/api/services/mail/Mokapi%20MailServer/mailboxes/[email protected]", nil,
56+
try.GetRequest(suite.T(), fmt.Sprintf("http://localhost:%v/api/services/mail/Mokapi%%20MailServer/mailboxes/[email protected]", suite.cfg.Api.Port), nil,
5557
try.HasStatusCode(200),
5658
try.HasBody(`{"name":"[email protected]","numMessages":1,"folders":["INBOX"]}`),
5759
)
5860
var messageId string
59-
try.GetRequest(suite.T(), "http://localhost:8080/api/services/mail/Mokapi%20MailServer/mailboxes/[email protected]/messages", nil,
61+
try.GetRequest(suite.T(), fmt.Sprintf("http://localhost:%v/api/services/mail/Mokapi%%20MailServer/mailboxes/[email protected]/messages", suite.cfg.Api.Port), nil,
6062
try.HasStatusCode(200),
6163
try.AssertBody(func(t *testing.T, body string) {
6264
var v any
@@ -73,7 +75,7 @@ func (suite *MailSuite) TestSendMail() {
7375
messageId = m["messageId"].(string)
7476
}),
7577
)
76-
try.GetRequest(suite.T(), "http://localhost:8080/api/services/mail/messages/"+messageId, nil,
78+
try.GetRequest(suite.T(), fmt.Sprintf("http://localhost:%v/api/services/mail/messages/%v", suite.cfg.Api.Port, messageId), nil,
7779
try.HasStatusCode(200),
7880
try.AssertBody(func(t *testing.T, body string) {
7981
var v any
@@ -209,16 +211,16 @@ It can be any text data.
209211
require.NoError(suite.T(), err)
210212

211213
// test mail API
212-
try.GetRequest(suite.T(), "http://localhost:8080/api/services/mail/Mokapi%20MailServer%20Old/mailboxes", nil,
214+
try.GetRequest(suite.T(), fmt.Sprintf("http://localhost:%v/api/services/mail/Mokapi%%20MailServer%%20Old/mailboxes", suite.cfg.Api.Port), nil,
213215
try.HasStatusCode(200),
214216
try.HasBody(`[{"name":"[email protected]","numMessages":1}]`),
215217
)
216-
try.GetRequest(suite.T(), "http://localhost:8080/api/services/mail/Mokapi%20MailServer%20Old/mailboxes/[email protected]", nil,
218+
try.GetRequest(suite.T(), fmt.Sprintf("http://localhost:%v/api/services/mail/Mokapi%%20MailServer%%20Old/mailboxes/[email protected]", suite.cfg.Api.Port), nil,
217219
try.HasStatusCode(200),
218220
try.HasBody(`{"name":"[email protected]","numMessages":1,"folders":["INBOX"]}`),
219221
)
220222
var messageId string
221-
try.GetRequest(suite.T(), "http://localhost:8080/api/services/mail/Mokapi%20MailServer%20Old/mailboxes/[email protected]/messages", nil,
223+
try.GetRequest(suite.T(), fmt.Sprintf("http://localhost:%v/api/services/mail/Mokapi%%20MailServer%%20Old/mailboxes/[email protected]/messages", suite.cfg.Api.Port), nil,
222224
try.HasStatusCode(200),
223225
try.AssertBody(func(t *testing.T, body string) {
224226
var v any
@@ -235,7 +237,7 @@ It can be any text data.
235237
messageId = m["messageId"].(string)
236238
}),
237239
)
238-
try.GetRequest(suite.T(), "http://localhost:8080/api/services/mail/messages/"+messageId, nil,
240+
try.GetRequest(suite.T(), fmt.Sprintf("http://localhost:%v/api/services/mail/messages/%v", suite.cfg.Api.Port, messageId), nil,
239241
try.HasStatusCode(200),
240242
try.AssertBody(func(t *testing.T, body string) {
241243
var v any

api/handler_kafka.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (h *handler) handleKafka(w http.ResponseWriter, r *http.Request) {
214214
}
215215
c := store.NewClient(k.Store, h.app.Monitor.Kafka)
216216
ct := media.ParseContentType(r.Header.Get("Content-Type"))
217-
result, err := c.Write(topicName, records, &ct)
217+
result, err := c.Write(topicName, records, ct)
218218
if err != nil {
219219
if errors.Is(err, store.TopicNotFound) || errors.Is(err, store.PartitionNotFound) {
220220
writeError(w, err, http.StatusNotFound)
@@ -288,7 +288,7 @@ func (h *handler) handleKafka(w http.ResponseWriter, r *http.Request) {
288288
}
289289
c := store.NewClient(k.Store, h.app.Monitor.Kafka)
290290
ct := media.ParseContentType(r.Header.Get("Content-Type"))
291-
result, err := c.Write(topicName, records, &ct)
291+
result, err := c.Write(topicName, records, ct)
292292
if err != nil {
293293
if errors.Is(err, store.TopicNotFound) || errors.Is(err, store.PartitionNotFound) {
294294
writeError(w, err, http.StatusNotFound)

api/handler_kafka_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
661661
Key: "foo",
662662
Value: map[string]interface{}{"value": "bar"},
663663
},
664-
}, &ct)
664+
}, ct)
665665

666666
return app
667667
},
@@ -700,7 +700,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
700700
Key: "foo",
701701
Value: map[string]interface{}{"value": "bar"},
702702
},
703-
}, &ct)
703+
}, ct)
704704

705705
return app
706706
},

api/handler_schema.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type schemaInfo struct {
2424

2525
type requestExample struct {
2626
Name string
27+
Path []string
2728
Format string
2829
Schema interface{}
2930
ContentTypes []string
@@ -61,10 +62,20 @@ func (h *handler) getExampleData(w http.ResponseWriter, r *http.Request) {
6162
re.ContentTypes = []string{"application/json"}
6263
}
6364

65+
req := &generator.Request{}
66+
if re.Name != "" {
67+
req.Path = []string{re.Name}
68+
} else if re.Path != nil {
69+
req.Path = re.Path
70+
}
71+
6472
var s *jsonSchema.Schema
6573
switch t := re.Schema.(type) {
6674
case *openApiSchema.Schema:
6775
s = openApiSchema.ConvertToJsonSchema(t)
76+
if t.Xml != nil && t.Xml.Name != "" {
77+
req.Path = append(req.Path, t.Xml.Name)
78+
}
6879
case *avro.Schema:
6980
s = avro.ConvertToJsonSchema(t)
7081
default:
@@ -75,11 +86,9 @@ func (h *handler) getExampleData(w http.ResponseWriter, r *http.Request) {
7586
return
7687
}
7788
}
89+
req.Schema = s
7890

79-
rnd, err := generator.New(&generator.Request{
80-
Path: []string{re.Name},
81-
Schema: s,
82-
})
91+
rnd, err := generator.New(req)
8392
if err != nil {
8493
http.Error(w, err.Error(), http.StatusBadRequest)
8594
return
@@ -254,6 +263,11 @@ func (r *requestExample) UnmarshalJSON(data []byte) error {
254263
return err
255264
}
256265
r.Name = t.(string)
266+
case "path":
267+
err = d.Decode(&r.Path)
268+
if err != nil {
269+
return err
270+
}
257271
case "format":
258272
t, err = d.Token()
259273
if err != nil {
@@ -270,6 +284,12 @@ func (r *requestExample) UnmarshalJSON(data []byte) error {
270284
if err != nil {
271285
return err
272286
}
287+
default:
288+
var discard json.RawMessage
289+
err = d.Decode(&discard)
290+
if err != nil {
291+
return err
292+
}
273293
}
274294
}
275295

api/handler_schema_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,29 @@ func TestHandler_Schema_Example(t *testing.T) {
430430
)
431431
},
432432
},
433+
{
434+
name: "pet with path",
435+
app: &runtime.App{},
436+
fn: func(t *testing.T, h http.Handler, app *runtime.App) {
437+
try.Handler(t,
438+
http.MethodGet,
439+
"http://foo.api/api/schema/example",
440+
nil,
441+
`{"path": ["pet", "category", "name"], "schema": {"type": ["string"]}, "contentTypes": ["application/json"]}`,
442+
h,
443+
try.HasStatusCode(200),
444+
try.HasHeader("Content-Type", "application/json"),
445+
try.AssertBody(func(t *testing.T, body string) {
446+
var data []map[string]any
447+
err := json.Unmarshal([]byte(body), &data)
448+
require.NoError(t, err)
449+
b, err := base64.StdEncoding.DecodeString(data[0]["value"].(string))
450+
require.NoError(t, err)
451+
require.Equal(t, `"turtle"`, string(b))
452+
}),
453+
)
454+
},
455+
},
433456
}
434457

435458
for _, tc := range testcases {

engine/common/host.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ type JobOptions struct {
2727

2828
type Host interface {
2929
Logger
30+
SetEventLogger(func(level, message string))
31+
3032
Every(every string, do func(), opt JobOptions) (int, error)
3133
Cron(expr string, do func(), opt JobOptions) (int, error)
3234
Cancel(jobId int) error
3335

3436
OpenFile(file string, hint string) (*dynamic.Config, error)
3537

36-
On(event string, do func(args ...interface{}) (bool, error), tags map[string]string)
38+
On(event string, do EventHandler, tags map[string]string)
3739

3840
KafkaClient() KafkaClient
3941
HttpClient(HttpClientOptions) HttpClient
@@ -182,3 +184,10 @@ type Store interface {
182184
Keys() []string
183185
Namespace(name string) Store
184186
}
187+
188+
type EventHandler func(ctx *EventContext) (bool, error)
189+
190+
type EventContext struct {
191+
EventLogger func(level, message string)
192+
Args []any
193+
}

engine/common/http.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
11
package common
22

33
import (
4+
"fmt"
45
"reflect"
56
"strings"
67
)
78

89
type EventResponse struct {
9-
Headers map[string]string `json:"headers"`
10-
StatusCode int `json:"statusCode"`
11-
Body string `json:"body"`
12-
Data interface{} `json:"data"`
10+
Headers map[string]any `json:"headers"`
11+
StatusCode int `json:"statusCode"`
12+
Body string `json:"body"`
13+
Data any `json:"data"`
1314
}
1415

1516
type EventRequest struct {
16-
Method string `json:"method"`
17-
Url Url `json:"url"`
18-
Body interface{} `json:"body"`
19-
Path map[string]interface{} `json:"path"`
20-
Query map[string]interface{} `json:"query"`
21-
Header map[string]interface{} `json:"header"`
22-
Cookie map[string]interface{} `json:"cookie"`
23-
QueryString any `json:"querystring"`
17+
Method string `json:"method"`
18+
Url Url `json:"url"`
19+
Body interface{} `json:"body"`
20+
Path map[string]any `json:"path"`
21+
Query map[string]any `json:"query"`
22+
Header map[string]any `json:"header"`
23+
Cookie map[string]any `json:"cookie"`
24+
QueryString any `json:"querystring"`
2425

26+
Api string `json:"api"`
2527
Key string `json:"key"`
2628
OperationId string `json:"operationId"`
2729
}
2830

2931
type Url struct {
3032
Scheme string `json:"scheme"`
3133
Host string `json:"host"`
34+
Port int `json:"port"`
3235
Path string `json:"path"`
3336
Query string `json:"query"`
3437
}
3538

3639
func (r *EventRequest) String() string {
37-
return r.Method + " " + r.Url.String()
40+
s := r.Method + " " + r.Url.String()
41+
if r.Api != "" {
42+
s += fmt.Sprintf(" [API: %s]", r.Api)
43+
}
44+
if r.OperationId != "" {
45+
s += fmt.Sprintf(" [OperationId: %s]", r.OperationId)
46+
}
47+
return s
3848
}
3949

4050
func (u Url) String() string {
@@ -45,15 +55,17 @@ func (u Url) String() string {
4555
}
4656
sb.WriteString(u.Host)
4757
sb.WriteString(u.Path)
48-
sb.WriteString("?" + u.Query)
58+
if len(u.Query) > 0 {
59+
sb.WriteString("?" + u.Query)
60+
}
4961
return sb.String()
5062
}
5163

5264
func (r *EventResponse) HasBody() bool {
5365
return len(r.Body) > 0 || r.Data != nil
5466
}
5567

56-
func EventHandler(req *EventRequest, res *EventResponse, resources interface{}) (bool, error) {
68+
func HttpEventHandler(req *EventRequest, res *EventResponse, resources interface{}) (bool, error) {
5769
resource := getResource(req.Url, resources)
5870
if resource == nil {
5971
return false, nil

engine/common/http_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package common
22

33
import (
4-
"github.com/stretchr/testify/require"
54
"testing"
5+
6+
"github.com/stretchr/testify/require"
67
)
78

89
func TestEventRequest_String(t *testing.T) {
@@ -58,7 +59,7 @@ func TestHttpResource(t *testing.T) {
5859
Url: tc.url,
5960
}
6061
res := &EventResponse{}
61-
b, err := EventHandler(req, res, tc.resource)
62+
b, err := HttpEventHandler(req, res, tc.resource)
6263
tc.test(t, b, res.Data, err)
6364
})
6465
}

0 commit comments

Comments
 (0)