Skip to content

Commit 5543dbf

Browse files
authored
feat: support to start compose mock proxy servers (#670)
* feat: support to start compose mock proxy servers * add comments --------- Co-authored-by: rick <[email protected]>
1 parent 109c5d9 commit 5543dbf

File tree

12 files changed

+751
-509
lines changed

12 files changed

+751
-509
lines changed

cmd/function.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package cmd
218

319
import (

cmd/init.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package cmd
218

319
import (

cmd/jsonschema.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package cmd
218

319
import (

cmd/jsonschema_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package cmd_test
218

319
import (

cmd/mock-compose.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"os"
21+
"os/signal"
22+
"syscall"
23+
24+
"github.com/linuxsuren/api-testing/pkg/mock"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
func createMockComposeCmd() (c *cobra.Command) {
29+
c = &cobra.Command{
30+
Use: "mock-compose",
31+
Short: "Mock multiple servers",
32+
Args: cobra.ExactArgs(1),
33+
RunE: func(cmd *cobra.Command, args []string) (err error) {
34+
reader := mock.NewLocalFileReader(args[0])
35+
36+
var server *mock.Server
37+
if server, err = reader.Parse(); err != nil {
38+
return
39+
}
40+
41+
var subServers []mock.DynamicServer
42+
for _, proxy := range server.Proxies {
43+
subProxy := &mock.Server{
44+
Proxies: []mock.Proxy{proxy},
45+
}
46+
47+
subReader := mock.NewObjectReader(subProxy)
48+
subServer := mock.NewInMemoryServer(c.Context(), proxy.Port)
49+
if err = subServer.Start(subReader, proxy.Prefix); err != nil {
50+
return
51+
}
52+
subServers = append(subServers, subServer)
53+
}
54+
55+
clean := make(chan os.Signal, 1)
56+
signal.Notify(clean, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
57+
select {
58+
case <-c.Context().Done():
59+
case <-clean:
60+
}
61+
for _, server := range subServers {
62+
server.Stop()
63+
}
64+
return
65+
},
66+
}
67+
return
68+
}

cmd/root.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package cmd
218

319
import (
@@ -20,7 +36,7 @@ func NewRootCmd(execer fakeruntime.Execer, httpServer server.HTTPServer) (c *cob
2036
c.SetOut(os.Stdout)
2137
c.Version = "\n" + version.GetDetailedVersion()
2238
c.AddCommand(createInitCommand(execer),
23-
createRunCommand(), createSampleCmd(),
39+
createRunCommand(), createSampleCmd(), createMockComposeCmd(),
2440
createServerCmd(execer, httpServer), createJSONSchemaCmd(),
2541
createServiceCommand(execer), createFunctionCmd(), createConvertCommand(),
2642
createMockCmd(), createExtensionCommand(downloader.NewStoreDownloader()))

cmd/root_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package cmd
218

319
import (

cmd/service.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
// Package cmd provides a service command
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
217
package cmd
318

419
import (
520
"fmt"
6-
"github.com/linuxsuren/api-testing/pkg/util/home"
721
"io"
822
"os"
923
"strings"
1024

25+
"github.com/linuxsuren/api-testing/pkg/util/home"
26+
1127
_ "embed"
1228

1329
"github.com/linuxsuren/api-testing/pkg/version"

docs/site/content/zh/latest/tasks/mock.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,32 @@ proxies:
151151
curl http://localhost:6060/mock/api/v1/projects
152152
```
153153

154+
如何希望把所有的请求都转发到某个地址,则可以使用通配符的方式:
155+
156+
```yaml
157+
proxies:
158+
- path: /{path:.*}
159+
target: http://192.168.123.58:9200
160+
```
161+
162+
## 代理多个服务
163+
164+
```shell
165+
atest mock-compose bin/compose.yaml
166+
```
167+
168+
执行上面的命令,会启动多个 Mock 代理服务,分别以不同的端口代理了 Elasticsearch 和 Eureka 服务:
169+
170+
```yaml
171+
proxies:
172+
- prefix: /
173+
port: 9200
174+
path: /{path:.*}
175+
target: http://192.168.123.58:9200
176+
- prefix: /
177+
port: 17001
178+
path: /{path:.*}
179+
target: http://192.168.123.58:17001
180+
```
181+
154182
> 更多 URL 中通配符的用法,请参考 https://github.com/gorilla/mux

0 commit comments

Comments
 (0)