Skip to content

Commit a060c50

Browse files
authored
Merge pull request #1377 from loadimpact/http2TestServer
Add a test http2 server and replace akamai's in tests
2 parents 8507e1d + fde11bf commit a060c50

File tree

3 files changed

+27
-51
lines changed

3 files changed

+27
-51
lines changed

js/modules/k6/http/request_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,15 @@ func TestRequestAndBatch(t *testing.T) {
393393
})
394394
t.Run("HTTP/2", func(t *testing.T) {
395395
stats.GetBufferedSamples(samples) // Clean up buffered samples from previous tests
396-
_, err := common.RunString(rt, `
397-
let res = http.request("GET", "https://http2.akamai.com/demo");
396+
_, err := common.RunString(rt, sr(`
397+
let res = http.request("GET", "HTTP2BIN_URL/get");
398398
if (res.status != 200) { throw new Error("wrong status: " + res.status) }
399399
if (res.proto != "HTTP/2.0") { throw new Error("wrong proto: " + res.proto) }
400-
`)
400+
`))
401401
assert.NoError(t, err)
402402

403403
bufSamples := stats.GetBufferedSamples(samples)
404-
assertRequestMetricsEmitted(t, bufSamples, "GET", "https://http2.akamai.com/demo", "", 200, "")
404+
assertRequestMetricsEmitted(t, bufSamples, "GET", sr("HTTP2BIN_URL/get"), "", 200, "")
405405
for _, sampleC := range bufSamples {
406406
for _, sample := range sampleC.GetSamples() {
407407
proto, ok := sample.Tags.Get("proto")

js/runner_test.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -941,53 +941,6 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
941941
}
942942
}
943943

944-
func TestVUIntegrationHTTP2(t *testing.T) {
945-
r1, err := getSimpleRunner("/script.js", `
946-
import http from "k6/http";
947-
export default function() {
948-
let res = http.request("GET", "https://http2.akamai.com/demo");
949-
if (res.status != 200) { throw new Error("wrong status: " + res.status) }
950-
if (res.proto != "HTTP/2.0") { throw new Error("wrong proto: " + res.proto) }
951-
}
952-
`)
953-
if !assert.NoError(t, err) {
954-
return
955-
}
956-
require.NoError(t, r1.SetOptions(lib.Options{
957-
Throw: null.BoolFrom(true),
958-
SystemTags: stats.NewSystemTagSet(stats.TagProto),
959-
}))
960-
961-
r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
962-
if !assert.NoError(t, err) {
963-
return
964-
}
965-
966-
runners := map[string]*Runner{"Source": r1, "Archive": r2}
967-
for name, r := range runners {
968-
t.Run(name, func(t *testing.T) {
969-
samples := make(chan stats.SampleContainer, 100)
970-
vu, err := r.NewVU(samples)
971-
if !assert.NoError(t, err) {
972-
return
973-
}
974-
err = vu.RunOnce(context.Background())
975-
assert.NoError(t, err)
976-
977-
protoFound := false
978-
for _, sampleC := range stats.GetBufferedSamples(samples) {
979-
for _, sample := range sampleC.GetSamples() {
980-
if proto, ok := sample.Tags.Get("proto"); ok {
981-
protoFound = true
982-
assert.Equal(t, "HTTP/2.0", proto)
983-
}
984-
}
985-
}
986-
assert.True(t, protoFound)
987-
})
988-
}
989-
}
990-
991944
func TestVUIntegrationOpenFunctionError(t *testing.T) {
992945
r, err := getSimpleRunner("/script.js", `
993946
export default function() { open("/tmp/foo") }

lib/testutils/httpmultibin/httpmultibin.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type HTTPMultiBin struct {
8787
Mux *http.ServeMux
8888
ServerHTTP *httptest.Server
8989
ServerHTTPS *httptest.Server
90+
ServerHTTP2 *httptest.Server
9091
Replacer *strings.Replacer
9192
TLSClientConfig *tls.Config
9293
Dialer *netext.Dialer
@@ -226,6 +227,20 @@ func NewHTTPMultiBin(t testing.TB) *HTTPMultiBin {
226227
require.NotNil(t, httpsIP)
227228
tlsConfig := GetTLSClientConfig(t, httpsSrv)
228229

230+
// Initialize the HTTP2 server, with a copy of the https tls config
231+
http2Srv := httptest.NewUnstartedServer(mux)
232+
err = http2.ConfigureServer(http2Srv.Config, &http2.Server{
233+
IdleTimeout: 30,
234+
})
235+
http2Srv.TLS = &(*tlsConfig) // copy it
236+
http2Srv.TLS.NextProtos = []string{http2.NextProtoTLS}
237+
require.NoError(t, err)
238+
http2Srv.StartTLS()
239+
http2URL, err := url.Parse(http2Srv.URL)
240+
require.NoError(t, err)
241+
http2IP := net.ParseIP(http2URL.Hostname())
242+
require.NotNil(t, http2IP)
243+
229244
// Set up the dialer with shorter timeouts and the custom domains
230245
dialer := netext.NewDialer(net.Dialer{
231246
Timeout: 2 * time.Second,
@@ -249,6 +264,7 @@ func NewHTTPMultiBin(t testing.TB) *HTTPMultiBin {
249264
Mux: mux,
250265
ServerHTTP: httpSrv,
251266
ServerHTTPS: httpsSrv,
267+
ServerHTTP2: http2Srv,
252268
Replacer: strings.NewReplacer(
253269
"HTTPBIN_IP_URL", httpSrv.URL,
254270
"HTTPBIN_DOMAIN", httpDomain,
@@ -262,12 +278,19 @@ func NewHTTPMultiBin(t testing.TB) *HTTPMultiBin {
262278
"WSSBIN_URL", fmt.Sprintf("wss://%s:%s", httpsDomain, httpsURL.Port()),
263279
"HTTPSBIN_IP", httpsIP.String(),
264280
"HTTPSBIN_PORT", httpsURL.Port(),
281+
282+
"HTTP2BIN_IP_URL", http2Srv.URL,
283+
"HTTP2BIN_DOMAIN", httpsDomain,
284+
"HTTP2BIN_URL", fmt.Sprintf("https://%s:%s", httpsDomain, http2URL.Port()),
285+
"HTTP2BIN_IP", http2IP.String(),
286+
"HTTP2BIN_PORT", http2URL.Port(),
265287
),
266288
TLSClientConfig: tlsConfig,
267289
Dialer: dialer,
268290
HTTPTransport: transport,
269291
Context: ctx,
270292
Cleanup: func() {
293+
http2Srv.Close()
271294
httpsSrv.Close()
272295
httpSrv.Close()
273296
ctxCancel()

0 commit comments

Comments
 (0)