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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ COPY console/atest-ui .
RUN npm install --ignore-scripts --registry=https://registry.npmmirror.com
RUN npm run build-only

FROM docker.io/golang:1.22.4 AS builder
FROM docker.io/golang:1.23 AS builder

ARG VERSION
ARG GOPROXY
Expand Down
2 changes: 1 addition & 1 deletion cmd/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func TestConvert(t *testing.T) {
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"},
c := cmd.NewRootCmd(&fakeruntime.FakeExecer{ExpectOS: "linux"},
server.NewFakeHTTPServer())
c.SetOut(io.Discard)

Expand Down
2 changes: 1 addition & 1 deletion cmd/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestCreateFunctionCommand(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"},
c := cmd.NewRootCmd(&fakeruntime.FakeExecer{ExpectOS: "linux"},
server.NewFakeHTTPServer())

buf := new(bytes.Buffer)
Expand Down
2 changes: 1 addition & 1 deletion cmd/jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func TestJSONSchemaCmd(t *testing.T) {
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"},
c := cmd.NewRootCmd(&fakeruntime.FakeExecer{ExpectOS: "linux"},
server.NewFakeHTTPServer())

buf := new(bytes.Buffer)
Expand Down
79 changes: 38 additions & 41 deletions cmd/mock-compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,49 @@ limitations under the License.
package cmd

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

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

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])
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 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},
}
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)
}
subReader := mock.NewObjectReader(subProxy)
subServer := mock.NewInMemoryServer(c.Context(), proxy.Port)
go subServer.Start(subReader, proxy.Prefix)
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
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
}
2 changes: 1 addition & 1 deletion cmd/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestMockCommand(t *testing.T) {
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
root := NewRootCmd(&fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
root.SetOut(io.Discard)
root.SetArgs(tc.args)
ctx, cancel := context.WithCancel(context.TODO())
Expand Down
4 changes: 2 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestCreateRunCommand(t *testing.T) {
execer := fakeruntime.FakeExecer{}
execer := &fakeruntime.FakeExecer{}

cmd := createRunCommand()
assert.Equal(t, "run", cmd.Use)
Expand All @@ -45,7 +45,7 @@ func TestCreateRunCommand(t *testing.T) {
}

func TestRootCmd(t *testing.T) {
c := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
c := NewRootCmd(&fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
assert.NotNil(t, c)
assert.Equal(t, "atest", c.Use)
}
2 changes: 1 addition & 1 deletion cmd/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func TestSampleCmd(t *testing.T) {
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"},
c := cmd.NewRootCmd(&fakeruntime.FakeExecer{ExpectOS: "linux"},
server.NewFakeHTTPServer())

buf := new(bytes.Buffer)
Expand Down
12 changes: 6 additions & 6 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestPrintProto(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := new(bytes.Buffer)
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
root := NewRootCmd(&fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
root.SetOut(buf)
root.SetArgs(append(tt.args, "--dry-run"))
err := root.Execute()
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestFrontEndHandlerWithLocation(t *testing.T) {

t.Run("download atest", func(t *testing.T) {
opt := &serverOption{
execer: fakeruntime.FakeExecer{
execer: &fakeruntime.FakeExecer{
ExpectOS: "linux",
ExpectLookPathError: errors.New("fake"),
},
Expand All @@ -199,7 +199,7 @@ func TestFrontEndHandlerWithLocation(t *testing.T) {

t.Run("download atest, failed to read", func(t *testing.T) {
opt := &serverOption{
execer: fakeruntime.FakeExecer{
execer: &fakeruntime.FakeExecer{
ExpectOS: "linux",
},
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestOAuth(t *testing.T) {
}}
for i, tt := range tests {
buf := new(bytes.Buffer)
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
root := NewRootCmd(&fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
root.SetOut(buf)
root.SetArgs(append(tt.args, "--dry-run"))
err := root.Execute()
Expand All @@ -294,7 +294,7 @@ func TestStartPlugins(t *testing.T) {
}
rootCmd.SetOut(io.Discard)
rootCmd.AddCommand(createServerCmd(
fakeruntime.FakeExecer{ExpectOS: "linux", ExpectLookPathError: errors.New("not-found")},
&fakeruntime.FakeExecer{ExpectOS: "linux", ExpectLookPathError: errors.New("not-found")},
server.NewFakeHTTPServer(),
))

Expand All @@ -310,7 +310,7 @@ func TestStartPlugins(t *testing.T) {
}
rootCmd.SetOut(io.Discard)
rootCmd.AddCommand(createServerCmd(
fakeruntime.FakeExecer{ExpectOS: "linux", ExpectLookPathError: errors.New("not-found")},
&fakeruntime.FakeExecer{ExpectOS: "linux", ExpectLookPathError: errors.New("not-found")},
httpServer,
))

Expand Down
18 changes: 17 additions & 1 deletion docs/site/content/zh/latest/tasks/mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ docker pull localhost:6060/repo/name:tag
* 面向对象的 CRUD
* 自定义 HTTP 服务

### 面对对象
### 面向对象

```yaml
#!api-testing-mock
Expand Down Expand Up @@ -160,6 +160,16 @@ proxies:
target: http://192.168.123.58:9200
```

## TCP 协议代理

```yaml
proxies:
- protocol: tcp
port: 3306
path: /
target: 192.168.123.58:33060
```

## 代理多个服务

```shell
Expand All @@ -178,6 +188,12 @@ proxies:
port: 17001
path: /{path:.*}
target: http://192.168.123.58:17001
- protocol: tcp
port: 33060
path: /
target: 192.168.123.58:33060
```

当前代理支持 HTTP 和 TCP 协议,上面的例子中代理了 MySQL 的 `33060` 端口。

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