Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b53a5c

Browse files
committed
httptest
1 parent 9f5e500 commit 0b53a5c

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

cli/ssh_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"fmt"
1111
"io"
1212
"net"
13+
"net/http"
14+
"net/http/httptest"
1315
"os"
1416
"os/exec"
1517
"path/filepath"
@@ -408,6 +410,58 @@ func TestSSH(t *testing.T) {
408410
<-cmdDone
409411
})
410412

413+
t.Run("RemoteForward", func(t *testing.T) {
414+
if runtime.GOOS == "windows" {
415+
t.Skip("Test not supported on windows")
416+
}
417+
418+
t.Parallel()
419+
420+
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
421+
w.Write([]byte("hello world"))
422+
}))
423+
defer httpServer.Close()
424+
425+
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
426+
427+
agentClient := agentsdk.New(client.URL)
428+
agentClient.SetSessionToken(agentToken)
429+
agentCloser := agent.New(agent.Options{
430+
Client: agentClient,
431+
Logger: slogtest.Make(t, nil).Named("agent"),
432+
})
433+
defer agentCloser.Close()
434+
435+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
436+
defer cancel()
437+
438+
inv, root := clitest.New(t,
439+
"ssh",
440+
workspace.Name,
441+
"--remote-forward",
442+
"8222:"+httpServer.Listener.Addr().String(),
443+
)
444+
clitest.SetupConfig(t, client, root)
445+
pty := ptytest.New(t).Attach(inv)
446+
inv.Stderr = pty.Output()
447+
cmdDone := tGo(t, func() {
448+
err := inv.WithContext(ctx).Run()
449+
assert.NoError(t, err, "ssh command failed")
450+
})
451+
452+
// Wait for the prompt or any output really to indicate the command has
453+
// started and accepting input on stdin.
454+
_ = pty.Peek(ctx, 1)
455+
456+
// Download the test page
457+
pty.WriteLine("curl localhost:8222")
458+
pty.ExpectMatch("hello world")
459+
460+
// And we're done.
461+
pty.WriteLine("exit")
462+
<-cmdDone
463+
})
464+
411465
t.Run("FileLogging", func(t *testing.T) {
412466
t.Parallel()
413467

0 commit comments

Comments
 (0)