Skip to content

Commit c827476

Browse files
authored
[wip] feat: add http proxy support (#597)
* feat: add http proxy support * feat: enhance server command to print local IPs on startup - Added a new function `printLocalIPs` to display available local IP addresses when the server starts. - Integrated the IP printing functionality into both the mock command and the server command, improving visibility of server accessibility. This change helps users easily identify the server's available addresses for better connectivity. * feat: support to set proxy on ui * update the test suite page * test pass with the http proxy mode * update grpc files * update grpc files * support insecure during testing * fix the unit tests * add more unit testing --------- Co-authored-by: rick <[email protected]>
1 parent 7b25fa3 commit c827476

28 files changed

+4331
-3752
lines changed

cmd/extension_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818

1919
import (
2020
"fmt"
21+
"io"
2122
"os"
2223
"testing"
2324

@@ -29,6 +30,7 @@ import (
2930
func TestExtensionCmd(t *testing.T) {
3031
t.Run("minimum one arg", func(t *testing.T) {
3132
command := createExtensionCommand(nil)
33+
command.SetOut(io.Discard)
3234
err := command.Execute()
3335
assert.Error(t, err)
3436
})
@@ -57,6 +59,7 @@ func TestExtensionCmd(t *testing.T) {
5759
assert.NoError(t, err)
5860

5961
command := createExtensionCommand(d)
62+
command.SetOut(io.Discard)
6063
command.SetArgs([]string{"git", "--output", tmpDownloadDir, "--registry", registry})
6164
err = command.Execute()
6265
assert.NoError(t, err)

cmd/mock.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"fmt"
21+
"net"
2022
"os"
2123
"os/signal"
2224
"syscall"
@@ -49,14 +51,13 @@ func createMockCmd() (c *cobra.Command) {
4951
func (o *mockOption) runE(c *cobra.Command, args []string) (err error) {
5052
reader := mock.NewLocalFileReader(args[0])
5153
server := mock.NewInMemoryServer(o.port)
52-
53-
c.Println("start listen", o.port)
5454
if err = server.Start(reader, o.prefix); err != nil {
5555
return
5656
}
5757

5858
clean := make(chan os.Signal, 1)
5959
signal.Notify(clean, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
60+
printLocalIPs(c, o.port)
6061

6162
select {
6263
case <-c.Context().Done():
@@ -65,3 +66,28 @@ func (o *mockOption) runE(c *cobra.Command, args []string) (err error) {
6566
err = server.Stop()
6667
return
6768
}
69+
70+
func printLocalIPs(c *cobra.Command, port int) {
71+
if ips, err := getLocalIPs(); err == nil {
72+
for _, ip := range ips {
73+
c.Printf("server is available at http://%s:%d\n", ip, port)
74+
}
75+
}
76+
}
77+
78+
func getLocalIPs() ([]string, error) {
79+
var ips []string
80+
addrs, err := net.InterfaceAddrs()
81+
if err != nil {
82+
return nil, fmt.Errorf("failed to get interface addresses: %v", err)
83+
}
84+
85+
for _, addr := range addrs {
86+
if ipNet, ok := addr.(*net.IPNet); ok {
87+
if ipNet.IP.To4() != nil && !ipNet.IP.IsLoopback() {
88+
ips = append(ips, ipNet.IP.String())
89+
}
90+
}
91+
}
92+
return ips, nil
93+
}

cmd/mock_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
"context"
21+
"github.com/linuxsuren/api-testing/pkg/server"
22+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
23+
"github.com/stretchr/testify/assert"
24+
"io"
25+
"testing"
26+
"time"
27+
)
28+
29+
func TestMockCommand(t *testing.T) {
30+
tt := []struct {
31+
name string
32+
args []string
33+
verify func(t *testing.T, err error)
34+
}{
35+
{
36+
name: "mock",
37+
args: []string{"mock"},
38+
verify: func(t *testing.T, err error) {
39+
assert.Error(t, err)
40+
},
41+
},
42+
{
43+
name: "mock with file",
44+
args: []string{"mock", "testdata/stores.yaml", "--port=0"},
45+
verify: func(t *testing.T, err error) {
46+
assert.NoError(t, err)
47+
},
48+
},
49+
}
50+
for _, tc := range tt {
51+
t.Run(tc.name, func(t *testing.T) {
52+
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
53+
root.SetOut(io.Discard)
54+
root.SetArgs(tc.args)
55+
ctx, cancel := context.WithCancel(context.TODO())
56+
root.SetContext(ctx)
57+
go func() {
58+
time.Sleep(time.Second * 2)
59+
cancel()
60+
}()
61+
err := root.Execute()
62+
tc.verify(t, err)
63+
})
64+
}
65+
}

cmd/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func TestPreRunE(t *testing.T) {
270270
func TestPrinter(t *testing.T) {
271271
buf := new(bytes.Buffer)
272272
c := &cobra.Command{}
273-
c.SetOutput(buf)
273+
c.SetOut(buf)
274274

275275
println(c, nil, "foo")
276276
assert.Empty(t, buf.String())

cmd/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
386386
serverLogger.Info("HTTP server started", "addr", httplis.Addr())
387387
serverLogger.Info("gRPC server started", "addr", lis.Addr())
388388
serverLogger.Info("Server is running.")
389+
printLocalIPs(cmd, o.httpPort)
389390

390391
err = o.httpServer.Serve(httplis)
391392
err = util.IgnoreErrServerClosed(err)

0 commit comments

Comments
 (0)