Skip to content

Commit 4a6bf6d

Browse files
committed
refactor: add test cases for serveHTTP
Signed-off-by: rMaxiQp <[email protected]>
1 parent 6300cae commit 4a6bf6d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

internal/cmd/run_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import (
66
"fmt"
77
"io"
88
"io/fs"
9+
"net"
10+
"net/http"
911
"os"
1012
"path"
1113
"path/filepath"
1214
"runtime"
1315
"testing"
16+
"time"
1417

1518
"go.k6.io/k6/errext"
1619

@@ -113,6 +116,48 @@ func TestHandleSummaryResultError(t *testing.T) {
113116
assertEqual(t, "file summary 2", files[filePath2])
114117
}
115118

119+
func TestServeHTTP(t *testing.T) {
120+
t.Parallel()
121+
testCases := []struct {
122+
name string
123+
addressSet bool
124+
}{
125+
{
126+
name: "address set",
127+
addressSet: true,
128+
},
129+
{
130+
name: "address not set",
131+
addressSet: false,
132+
},
133+
}
134+
135+
for _, tc := range testCases {
136+
t.Run(tc.name, func(t *testing.T) {
137+
t.Parallel()
138+
ts := tests.NewGlobalTestState(t)
139+
cmd := &cmdRun{
140+
gs: ts.GlobalState,
141+
}
142+
if tc.addressSet {
143+
ts.ExpectedExitCode = int(exitcodes.CannotStartRESTAPI)
144+
}
145+
146+
lis, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", ts.Flags.Address)
147+
require.NoError(t, err)
148+
t.Cleanup(func() {
149+
require.NoError(t, lis.Close())
150+
})
151+
152+
srv := &http.Server{
153+
Addr: lis.Addr().String(),
154+
ReadHeaderTimeout: time.Millisecond,
155+
}
156+
cmd.serveHTTP(srv, tc.addressSet)
157+
})
158+
}
159+
}
160+
116161
func TestRunScriptErrorsAndAbort(t *testing.T) {
117162
t.Parallel()
118163
testCases := []struct {

0 commit comments

Comments
 (0)