Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cmd/function.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
Expand Down
16 changes: 16 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
Expand Down
16 changes: 16 additions & 0 deletions cmd/jsonschema.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
Expand Down
16 changes: 16 additions & 0 deletions cmd/jsonschema_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd_test

import (
Expand Down
68 changes: 68 additions & 0 deletions cmd/mock-compose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"os"
"os/signal"
"syscall"

"github.com/linuxsuren/api-testing/pkg/mock"
"github.com/spf13/cobra"
)

func createMockComposeCmd() (c *cobra.Command) {
c = &cobra.Command{
Use: "mock-compose",
Short: "Mock multiple servers",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
reader := mock.NewLocalFileReader(args[0])

var server *mock.Server
if server, err = reader.Parse(); err != nil {
return
}

var subServers []mock.DynamicServer
for _, proxy := range server.Proxies {
subProxy := &mock.Server{
Proxies: []mock.Proxy{proxy},
}

subReader := mock.NewObjectReader(subProxy)
subServer := mock.NewInMemoryServer(c.Context(), proxy.Port)
if err = subServer.Start(subReader, proxy.Prefix); err != nil {
return
}
subServers = append(subServers, subServer)
}

clean := make(chan os.Signal, 1)
signal.Notify(clean, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
select {
case <-c.Context().Done():
case <-clean:
}
for _, server := range subServers {
server.Stop()
}
return
},
}
return
}
18 changes: 17 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
Expand All @@ -20,7 +36,7 @@ func NewRootCmd(execer fakeruntime.Execer, httpServer server.HTTPServer) (c *cob
c.SetOut(os.Stdout)
c.Version = "\n" + version.GetDetailedVersion()
c.AddCommand(createInitCommand(execer),
createRunCommand(), createSampleCmd(),
createRunCommand(), createSampleCmd(), createMockComposeCmd(),
createServerCmd(execer, httpServer), createJSONSchemaCmd(),
createServiceCommand(execer), createFunctionCmd(), createConvertCommand(),
createMockCmd(), createExtensionCommand(downloader.NewStoreDownloader()))
Expand Down
16 changes: 16 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
Expand Down
20 changes: 18 additions & 2 deletions cmd/service.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// Package cmd provides a service command
/*
Copyright 2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"github.com/linuxsuren/api-testing/pkg/util/home"
"io"
"os"
"strings"

"github.com/linuxsuren/api-testing/pkg/util/home"

_ "embed"

"github.com/linuxsuren/api-testing/pkg/version"
Expand Down
28 changes: 28 additions & 0 deletions docs/site/content/zh/latest/tasks/mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,32 @@ proxies:
curl http://localhost:6060/mock/api/v1/projects
```

如何希望把所有的请求都转发到某个地址,则可以使用通配符的方式:

```yaml
proxies:
- path: /{path:.*}
target: http://192.168.123.58:9200
```

## 代理多个服务

```shell
atest mock-compose bin/compose.yaml
```

执行上面的命令,会启动多个 Mock 代理服务,分别以不同的端口代理了 Elasticsearch 和 Eureka 服务:

```yaml
proxies:
- prefix: /
port: 9200
path: /{path:.*}
target: http://192.168.123.58:9200
- prefix: /
port: 17001
path: /{path:.*}
target: http://192.168.123.58:17001
```

> 更多 URL 中通配符的用法,请参考 https://github.com/gorilla/mux
Loading
Loading