Skip to content

Commit 07ce20a

Browse files
authored
Merge pull request #706 from marle3003/develop
Develop
2 parents 9735c88 + db6a7d0 commit 07ce20a

File tree

211 files changed

+8763
-1991
lines changed

Some content is hidden

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

211 files changed

+8763
-1991
lines changed

acceptance/acceptance_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package acceptance
22

33
import (
4-
"github.com/stretchr/testify/require"
5-
"github.com/stretchr/testify/suite"
4+
"io"
65
"mokapi/config/static"
76
"testing"
8-
"time"
7+
8+
"github.com/sirupsen/logrus"
9+
"github.com/stretchr/testify/require"
10+
"github.com/stretchr/testify/suite"
911
)
1012

1113
func TestAcceptance(t *testing.T) {
@@ -21,15 +23,12 @@ type BaseSuite struct {
2123
}
2224

2325
func (suite *BaseSuite) initCmd(cfg *static.Config) {
24-
//logrus.SetOutput(io.Discard)
26+
logrus.SetOutput(io.Discard)
2527

2628
suite.cfg = cfg
2729
cmd, err := Start(cfg)
2830
require.NoError(suite.T(), err)
2931
suite.cmd = cmd
30-
31-
// wait for server start
32-
time.Sleep(2 * time.Second)
3332
}
3433

3534
func (suite *BaseSuite) TearDownSuite() {

acceptance/cmd_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"mokapi/providers/swagger"
1717
"mokapi/runtime"
1818
"mokapi/safe"
19+
"mokapi/schema/json/generator"
1920
"mokapi/server"
2021
"mokapi/server/cert"
2122
"mokapi/version"
@@ -28,7 +29,6 @@ type Cmd struct {
2829
App *runtime.App
2930

3031
server *server.Server
31-
cancel context.CancelFunc
3232
}
3333

3434
func Start(cfg *static.Config) (*Cmd, error) {
@@ -42,6 +42,7 @@ func Start(cfg *static.Config) (*Cmd, error) {
4242

4343
registerDynamicTypes()
4444
app := runtime.New(cfg)
45+
generator.SetConfig(cfg.DataGen)
4546

4647
watcher := server.NewConfigWatcher(cfg)
4748

@@ -77,19 +78,21 @@ func Start(cfg *static.Config) (*Cmd, error) {
7778
}
7879

7980
pool := safe.NewPool(context.Background())
80-
ctx, cancel := context.WithCancel(context.Background())
8181
s := server.NewServer(pool, app, watcher, kafka, http, mailManager, ldap, scriptEngine)
82-
s.StartAsync(ctx)
82+
go func() {
83+
err := s.Start()
84+
if err != nil {
85+
panic(err)
86+
}
87+
}()
8388

8489
return &Cmd{
8590
App: app,
8691
server: s,
87-
cancel: cancel,
8892
}, nil
8993
}
9094

9195
func (cmd *Cmd) Stop() {
92-
cmd.cancel()
9396
cmd.server.Close()
9497
}
9598

acceptance/ldap_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type LdapSuite struct {
1818

1919
func (suite *LdapSuite) SetupSuite() {
2020
cfg := static.NewConfig()
21+
port := try.GetFreePort()
22+
cfg.Api.Port = fmt.Sprintf("%v", port)
2123
cfg.Providers.File.Directories = []string{"./ldap"}
2224
suite.initCmd(cfg)
2325
// ensure scripts are executed
@@ -29,6 +31,7 @@ func (suite *LdapSuite) SetupSuite() {
2931

3032
func (suite *LdapSuite) TearDownSuite() {
3133
suite.Client.Close()
34+
suite.BaseSuite.TearDownSuite()
3235
}
3336

3437
func (suite *LdapSuite) AfterTest(_, _ string) {

acceptance/mail_test.go

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"crypto/tls"
55
"encoding/json"
66
"fmt"
7-
"github.com/stretchr/testify/require"
87
"mokapi/config/static"
98
"mokapi/server/cert"
109
"mokapi/smtp/smtptest"
@@ -14,6 +13,10 @@ import (
1413
"os"
1514
"path"
1615
"testing"
16+
"time"
17+
18+
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
1720
)
1821

1922
type MailSuite struct{ BaseSuite }
@@ -161,10 +164,10 @@ func (suite *MailSuite) TestSearch() {
161164
try.AssertBody(func(t *testing.T, body string) {
162165
var data map[string]any
163166
err := json.Unmarshal([]byte(body), &data)
164-
require.NoError(t, err)
165-
require.NotNil(t, data)
167+
assert.NoError(t, err)
168+
assert.NotNil(t, data)
166169

167-
require.Equal(t, float64(5), data["total"])
170+
assert.Equal(t, float64(5), data["total"])
168171
}),
169172
)
170173
}
@@ -220,15 +223,15 @@ It can be any text data.
220223
try.AssertBody(func(t *testing.T, body string) {
221224
var v any
222225
err = json.Unmarshal([]byte(body), &v)
223-
require.NoError(suite.T(), err)
226+
assert.NoError(suite.T(), err)
224227
a := v.([]any)
225228
m := a[0].(map[string]any)
226-
require.Len(t, m, 5)
227-
require.NotEmpty(t, m["messageId"])
228-
require.NotEmpty(t, m["date"])
229-
require.Equal(t, []any{map[string]any{"address": "[email protected]"}}, m["from"])
230-
require.Equal(t, []any{map[string]any{"address": "[email protected]"}}, m["to"])
231-
require.Equal(t, "Example multipart/mixed message", m["subject"])
229+
assert.Len(t, m, 5)
230+
assert.NotEmpty(t, m["messageId"])
231+
assert.NotEmpty(t, m["date"])
232+
assert.Equal(t, []any{map[string]any{"address": "[email protected]"}}, m["from"])
233+
assert.Equal(t, []any{map[string]any{"address": "[email protected]"}}, m["to"])
234+
assert.Equal(t, "Example multipart/mixed message", m["subject"])
232235
messageId = m["messageId"].(string)
233236
}),
234237
)
@@ -237,38 +240,46 @@ It can be any text data.
237240
try.AssertBody(func(t *testing.T, body string) {
238241
var v any
239242
err = json.Unmarshal([]byte(body), &v)
240-
require.NoError(suite.T(), err)
243+
assert.NoError(suite.T(), err)
241244
m := v.(map[string]any)
242245
m = m["data"].(map[string]any)
243-
require.Len(t, m, 10)
244-
require.Regexp(t, ".*:8030", m["server"])
245-
require.Equal(t, []any{map[string]any{"address": "[email protected]"}}, m["from"])
246-
require.Equal(t, []any{map[string]any{"address": "[email protected]"}}, m["to"])
247-
require.Equal(t, []any{
246+
assert.Len(t, m, 10)
247+
assert.Regexp(t, ".*:8030", m["server"])
248+
assert.Equal(t, []any{map[string]any{"address": "[email protected]"}}, m["from"])
249+
assert.Equal(t, []any{map[string]any{"address": "[email protected]"}}, m["to"])
250+
assert.Equal(t, []any{
248251
map[string]any{
249252
"contentType": "text/plain",
250253
"name": "example.txt",
251254
"size": float64(64),
252255
},
253256
}, m["attachments"])
254-
require.NotContains(t, m, "sender")
255-
require.NotContains(t, m, "replyTo")
256-
require.NotContains(t, m, "cc")
257-
require.NotContains(t, m, "bcc")
258-
require.NotEmpty(t, m["messageId"])
259-
require.NotContains(t, m, "inReplyTo")
260-
require.NotEmpty(t, m["date"])
261-
require.Equal(t, "Example multipart/mixed message", m["subject"])
262-
require.Equal(t, "text/plain; charset=\"UTF-8\"", m["contentType"])
263-
require.NotContains(t, m, "contentTransferEncoding")
264-
require.Equal(t, "Hello Bob,\n\nThis is the plain text part of the email.\n", m["body"])
265-
require.Greater(t, m["size"], float64(0))
257+
assert.NotContains(t, m, "sender")
258+
assert.NotContains(t, m, "replyTo")
259+
assert.NotContains(t, m, "cc")
260+
assert.NotContains(t, m, "bcc")
261+
assert.NotEmpty(t, m["messageId"])
262+
assert.NotContains(t, m, "inReplyTo")
263+
assert.NotEmpty(t, m["date"])
264+
assert.Equal(t, "Example multipart/mixed message", m["subject"])
265+
assert.Equal(t, "text/plain; charset=\"UTF-8\"", m["contentType"])
266+
assert.NotContains(t, m, "contentTransferEncoding")
267+
assert.Equal(t, "Hello Bob,\n\nThis is the plain text part of the email.\n", m["body"])
268+
assert.Greater(t, m["size"], float64(0))
266269
}),
267270
)
268271
}
269272

270273
func (suite *MailSuite) TestCustomServerCertificate() {
271-
conn, err := net.Dial("tcp", "localhost:8993")
274+
var conn net.Conn
275+
var err error
276+
for attempt := 1; attempt <= 10; attempt++ {
277+
conn, err = net.Dial("tcp", "localhost:8993")
278+
if err == nil {
279+
break
280+
}
281+
time.Sleep(200 * time.Millisecond)
282+
}
272283
require.NoError(suite.T(), err)
273284
defer func() { _ = conn.Close() }()
274285

acceptance/petstore_test.go

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ package acceptance
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/brianvoe/gofakeit/v6"
7-
"github.com/stretchr/testify/require"
86
"mokapi/config/static"
97
"mokapi/kafka"
108
"mokapi/kafka/fetch"
119
"mokapi/kafka/kafkatest"
1210
"mokapi/kafka/metaData"
1311
"mokapi/kafka/produce"
12+
"mokapi/schema/json/generator"
1413
"mokapi/try"
1514
"net/http"
1615
"testing"
1716
"time"
17+
18+
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
1820
)
1921

2022
type PetStoreSuite struct{ BaseSuite }
@@ -29,7 +31,7 @@ func (suite *PetStoreSuite) SetupSuite() {
2931
}
3032

3133
func (suite *PetStoreSuite) SetupTest() {
32-
gofakeit.Seed(11)
34+
generator.Seed(11)
3335
}
3436

3537
func (suite *PetStoreSuite) TestApi() {
@@ -50,9 +52,6 @@ func (suite *PetStoreSuite) TestApi() {
5052
})
5153

5254
suite.T().Run("get AsyncAPI service", func(t *testing.T) {
53-
// ensure scripts are executed
54-
time.Sleep(5 * time.Second)
55-
5655
expected := map[string]interface{}{
5756
"version": "1.0.0",
5857
"name": "A sample AsyncApi Kafka streaming api",
@@ -128,11 +127,23 @@ func (suite *PetStoreSuite) TestApi() {
128127
}},
129128
}
130129

131-
try.GetRequest(t, fmt.Sprintf("http://127.0.0.1:%v/api/services/kafka/A%%20sample%%20AsyncApi%%20Kafka%%20streaming%%20api", suite.cfg.Api.Port),
132-
nil,
133-
try.HasStatusCode(http.StatusOK),
134-
try.BodyContainsData(expected),
135-
)
130+
deadline := time.Now().Add(5 * time.Second)
131+
for time.Now().Before(deadline) {
132+
try.GetRequest(t, fmt.Sprintf("http://127.0.0.1:%v/api/services/kafka/A%%20sample%%20AsyncApi%%20Kafka%%20streaming%%20api", suite.cfg.Api.Port),
133+
nil,
134+
try.HasStatusCode(http.StatusOK),
135+
try.BodyContainsData(expected),
136+
)
137+
if !t.Failed() {
138+
break
139+
}
140+
if time.Now().After(deadline) {
141+
t.FailNow()
142+
}
143+
// reset test failure state before retrying
144+
t.Cleanup(func() {})
145+
time.Sleep(100 * time.Millisecond)
146+
}
136147
})
137148
}
138149

@@ -158,7 +169,7 @@ func (suite *PetStoreSuite) TestJsHttpHandler() {
158169
try.GetRequest(suite.T(), "http://127.0.0.1:18080/pet/5",
159170
map[string]string{"Accept": "application/json", "api_key": "123"},
160171
try.HasStatusCode(http.StatusOK),
161-
try.BodyContains(`},"name":"Zoe","photoUrls":`))
172+
try.BodyContains(`"name":"Zoe"`))
162173

163174
// test http metrics
164175
try.GetRequest(suite.T(), fmt.Sprintf("http://127.0.0.1:%s/api/metrics/http?path=/pet/{petId}", suite.cfg.Api.Port), nil,
@@ -179,12 +190,13 @@ func (suite *PetStoreSuite) TestGetOrderById() {
179190
try.GetRequest(suite.T(), "http://127.0.0.1:18080/store/order/1",
180191
map[string]string{"Accept": "application/json"},
181192
try.HasStatusCode(http.StatusOK),
182-
try.HasBody(`{"id":98266,"petId":23377,"quantity":92,"shipDate":"2012-01-30T07:58:01Z","status":"approved","complete":false}`))
193+
try.HasBody(`{"petId":23377,"quantity":92,"shipDate":"2012-01-30T07:58:01Z","complete":false}`))
183194

184195
try.GetRequest(suite.T(), "https://localhost:18443/store/order/10",
185196
map[string]string{"Accept": "application/json"},
186197
try.HasStatusCode(http.StatusOK),
187-
try.HasBody(`{"id":12545,"petId":20895,"quantity":16,"shipDate":"2027-11-26T16:57:16Z","status":"approved","complete":true}`))
198+
// properties like id or petId are optional
199+
try.HasBody(`{"id":93761,"petId":83318,"quantity":27,"shipDate":"2014-02-04T10:00:17Z","status":"placed","complete":true}`))
188200
}
189201

190202
func (suite *PetStoreSuite) TestTls() {
@@ -341,17 +353,17 @@ func (suite *PetStoreSuite) TestSearch_Paging() {
341353
try.AssertBody(func(t *testing.T, body string) {
342354
var data map[string]any
343355
err := json.Unmarshal([]byte(body), &data)
344-
require.NoError(t, err)
345-
require.NotNil(t, data)
356+
assert.NoError(t, err)
357+
assert.NotNil(t, data)
346358

347-
require.Equal(t, float64(35), data["total"])
359+
assert.Equal(t, float64(35), data["total"])
348360

349361
items := data["results"].([]any)
350-
require.Len(t, items, 10)
362+
assert.Len(t, items, 10)
351363
evt := items[0].(map[string]interface{})
352-
require.Equal(t, "HTTP", evt["type"])
353-
require.Equal(t, "Swagger Petstore", evt["title"])
354-
require.NotContains(t, evt, "domain")
364+
assert.Equal(t, "HTTP", evt["type"])
365+
assert.Equal(t, "Swagger Petstore", evt["title"])
366+
assert.NotContains(t, evt, "domain")
355367
}),
356368
)
357369

@@ -361,17 +373,17 @@ func (suite *PetStoreSuite) TestSearch_Paging() {
361373
try.AssertBody(func(t *testing.T, body string) {
362374
var data map[string]any
363375
err := json.Unmarshal([]byte(body), &data)
364-
require.NoError(t, err)
365-
require.NotNil(t, data)
376+
assert.NoError(t, err)
377+
assert.NotNil(t, data)
366378

367-
require.Equal(t, float64(35), data["total"])
379+
assert.Equal(t, float64(35), data["total"])
368380

369381
items := data["results"].([]any)
370-
require.Len(t, items, 10)
382+
assert.Len(t, items, 10)
371383
evt := items[0].(map[string]interface{})
372-
require.Equal(t, "HTTP", evt["type"])
373-
require.Equal(t, "GET /pet/{petId}", evt["title"])
374-
require.Equal(t, "Swagger Petstore", evt["domain"])
384+
assert.Equal(t, "HTTP", evt["type"])
385+
assert.Equal(t, "GET /pet/{petId}", evt["title"])
386+
assert.Equal(t, "Swagger Petstore", evt["domain"])
375387
}),
376388
)
377389
}

0 commit comments

Comments
 (0)