Skip to content

Commit

Permalink
[wip] feat: add http proxy support (#597)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Jan 20, 2025
1 parent 7b25fa3 commit c827476
Show file tree
Hide file tree
Showing 28 changed files with 4,331 additions and 3,752 deletions.
3 changes: 3 additions & 0 deletions cmd/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"fmt"
"io"
"os"
"testing"

Expand All @@ -29,6 +30,7 @@ import (
func TestExtensionCmd(t *testing.T) {
t.Run("minimum one arg", func(t *testing.T) {
command := createExtensionCommand(nil)
command.SetOut(io.Discard)
err := command.Execute()
assert.Error(t, err)
})
Expand Down Expand Up @@ -57,6 +59,7 @@ func TestExtensionCmd(t *testing.T) {
assert.NoError(t, err)

command := createExtensionCommand(d)
command.SetOut(io.Discard)
command.SetArgs([]string{"git", "--output", tmpDownloadDir, "--registry", registry})
err = command.Execute()
assert.NoError(t, err)
Expand Down
30 changes: 28 additions & 2 deletions cmd/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package cmd

import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -49,14 +51,13 @@ func createMockCmd() (c *cobra.Command) {
func (o *mockOption) runE(c *cobra.Command, args []string) (err error) {
reader := mock.NewLocalFileReader(args[0])
server := mock.NewInMemoryServer(o.port)

c.Println("start listen", o.port)
if err = server.Start(reader, o.prefix); err != nil {
return
}

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

select {
case <-c.Context().Done():
Expand All @@ -65,3 +66,28 @@ func (o *mockOption) runE(c *cobra.Command, args []string) (err error) {
err = server.Stop()
return
}

func printLocalIPs(c *cobra.Command, port int) {
if ips, err := getLocalIPs(); err == nil {
for _, ip := range ips {
c.Printf("server is available at http://%s:%d\n", ip, port)
}
}
}

func getLocalIPs() ([]string, error) {
var ips []string
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, fmt.Errorf("failed to get interface addresses: %v", err)
}

for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok {
if ipNet.IP.To4() != nil && !ipNet.IP.IsLoopback() {
ips = append(ips, ipNet.IP.String())
}
}
}
return ips, nil
}
65 changes: 65 additions & 0 deletions cmd/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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 (
"context"
"github.com/linuxsuren/api-testing/pkg/server"
fakeruntime "github.com/linuxsuren/go-fake-runtime"
"github.com/stretchr/testify/assert"
"io"
"testing"
"time"
)

func TestMockCommand(t *testing.T) {
tt := []struct {
name string
args []string
verify func(t *testing.T, err error)
}{
{
name: "mock",
args: []string{"mock"},
verify: func(t *testing.T, err error) {
assert.Error(t, err)
},
},
{
name: "mock with file",
args: []string{"mock", "testdata/stores.yaml", "--port=0"},
verify: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
root.SetOut(io.Discard)
root.SetArgs(tc.args)
ctx, cancel := context.WithCancel(context.TODO())
root.SetContext(ctx)
go func() {
time.Sleep(time.Second * 2)
cancel()
}()
err := root.Execute()
tc.verify(t, err)
})
}
}
2 changes: 1 addition & 1 deletion cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func TestPreRunE(t *testing.T) {
func TestPrinter(t *testing.T) {
buf := new(bytes.Buffer)
c := &cobra.Command{}
c.SetOutput(buf)
c.SetOut(buf)

println(c, nil, "foo")
assert.Empty(t, buf.String())
Expand Down
1 change: 1 addition & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
serverLogger.Info("HTTP server started", "addr", httplis.Addr())
serverLogger.Info("gRPC server started", "addr", lis.Addr())
serverLogger.Info("Server is running.")
printLocalIPs(cmd, o.httpPort)

err = o.httpServer.Serve(httplis)
err = util.IgnoreErrServerClosed(err)
Expand Down
Loading

0 comments on commit c827476

Please sign in to comment.