Skip to content

Commit c52f12c

Browse files
committed
Rewrite hellohttp2 test service to use stdlib
1 parent 9818c20 commit c52f12c

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

test/e2e/http2_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/http"
2525
"testing"
2626

27+
v1 "k8s.io/api/core/v1"
2728
pkgTest "knative.dev/pkg/test"
2829
"knative.dev/pkg/test/spoof"
2930
rtesting "knative.dev/serving/pkg/testing/v1"
@@ -49,7 +50,13 @@ func TestHelloHTTP2WithPortNameH2C(t *testing.T) {
4950

5051
t.Log("Creating a new Service")
5152

52-
resources, err := v1test.CreateServiceReady(t, clients, &names, rtesting.WithNamedPort("h2c"))
53+
resources, err := v1test.CreateServiceReady(
54+
t, clients, &names, rtesting.WithNamedPort("h2c"),
55+
rtesting.WithEnv(v1.EnvVar{
56+
Name: "RESPONSE",
57+
Value: test.HelloHTTP2Text,
58+
}),
59+
)
5360
if err != nil {
5461
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err)
5562
}

test/test_images/hellohttp2/hellohttp2.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,43 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"flag"
2120
"fmt"
2221
"log"
2322
"net/http"
2423
"os"
25-
26-
"knative.dev/pkg/network"
2724
)
2825

26+
func main() {
27+
log.Print("hellohttp2 app started.")
28+
29+
mux := http.NewServeMux()
30+
mux.HandleFunc("/", handler)
31+
mux.HandleFunc("/healthz", healthzHandler)
32+
33+
protocols := &http.Protocols{}
34+
protocols.SetHTTP1(true)
35+
protocols.SetUnencryptedHTTP2(true)
36+
37+
s := &http.Server{
38+
Addr: ":" + os.Getenv("PORT"),
39+
Handler: mux,
40+
Protocols: protocols,
41+
}
42+
log.Fatal(s.ListenAndServe())
43+
}
44+
2945
func handler(w http.ResponseWriter, r *http.Request) {
30-
if r.ProtoMajor == 2 {
31-
log.Print("hellohttp2 received an http2 request.")
32-
fmt.Fprintln(w, "Hello, New World! How about donuts and coffee?")
46+
log.Printf("Request: proto=%s method=%s path=%s", r.Proto, r.Method, r.URL.Path)
47+
48+
if res, ok := os.LookupEnv("RESPONSE"); ok {
49+
fmt.Fprint(w, res)
3350
} else {
34-
log.Print("hellohttp2 received an HTTP 1.1 request.")
35-
w.WriteHeader(http.StatusUpgradeRequired)
51+
fmt.Fprintf(w, "proto=%s method=%s path=%s\n", r.Proto, r.Method, r.URL.Path)
3652
}
3753
}
3854

39-
func main() {
40-
flag.Parse()
41-
log.Print("hellohttp2 app started.")
42-
43-
s := network.NewServer(":"+os.Getenv("PORT"), http.HandlerFunc(handler))
44-
log.Fatal(s.ListenAndServe())
55+
func healthzHandler(w http.ResponseWriter, r *http.Request) {
56+
log.Printf("Health check: proto=%s method=%s path=%s", r.Proto, r.Method, r.URL.Path)
57+
w.WriteHeader(http.StatusOK)
58+
fmt.Fprintln(w, "OK")
4559
}

test/test_images/hellohttp2/service.yaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ metadata:
55
namespace: default
66
spec:
77
template:
8+
metadata:
9+
annotations:
10+
# Example: Override the queue sidecar image with a custom image
11+
# queue.sidecar.serving.knative.dev/image: ko://knative.dev/serving/cmd/queue
812
spec:
913
containers:
10-
- image: ko://knative.dev/serving/test/test_images/hellohttp2
11-
ports:
12-
- name: h2c
13-
containerPort: 8080
14+
- image: ko://knative.dev/serving/test/test_images/hellohttp2
15+
ports:
16+
- name: h2c
17+
containerPort: 8080
18+
readinessProbe:
19+
httpGet:
20+
path: /healthz
21+
port: 8080

0 commit comments

Comments
 (0)