Skip to content

Commit 079e477

Browse files
authored
Conformance: Adds Port Response Header to Echo Server (#4230)
* Conformance: Adds Port Response Header to Echo Server Signed-off-by: Daneyon Hansen <[email protected]> * Refactors to put port in resp body Signed-off-by: Daneyon Hansen <[email protected]> * Adds HTTPPort field to CapturedRequest - Required to support JSON-based echo response validation. Signed-off-by: Daneyon Hansen <[email protected]> --------- Signed-off-by: Daneyon Hansen <[email protected]>
1 parent e29b9a3 commit 079e477

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

conformance/echo-basic/echo-basic.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ import (
3939

4040
// RequestAssertions contains information about the request and the Ingress
4141
type RequestAssertions struct {
42-
Path string `json:"path"`
43-
Host string `json:"host"`
44-
Method string `json:"method"`
45-
Proto string `json:"proto"`
46-
Headers map[string][]string `json:"headers"`
42+
Path string `json:"path"`
43+
Host string `json:"host"`
44+
Method string `json:"method"`
45+
Proto string `json:"proto"`
46+
Headers map[string][]string `json:"headers"`
47+
HTTPPort string `json:"httpPort"`
4748

4849
Context `json:",inline"`
4950

@@ -76,15 +77,18 @@ type Context struct {
7677
Pod string `json:"pod"`
7778
}
7879

79-
var context Context
80+
var (
81+
context Context
82+
httpPort string
83+
)
8084

8185
func main() {
8286
if os.Getenv("GRPC_ECHO_SERVER") != "" {
8387
g.Main()
8488
return
8589
}
8690

87-
httpPort := os.Getenv("HTTP_PORT")
91+
httpPort = os.Getenv("HTTP_PORT")
8892
if httpPort == "" {
8993
httpPort = "3000"
9094
}
@@ -216,6 +220,7 @@ func echoHandler(w http.ResponseWriter, r *http.Request) {
216220
r.Method,
217221
r.Proto,
218222
r.Header,
223+
httpPort,
219224

220225
context,
221226

conformance/echo-basic/echo-basic_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,70 @@ func TestWriteEchoResponseHeaders(t *testing.T) {
228228
}
229229
}
230230

231+
func TestEchoHandler_IncludesHTTPPort(t *testing.T) {
232+
// Preserve global across tests.
233+
prevHTTPPort := httpPort
234+
defer func() { httpPort = prevHTTPPort }()
235+
236+
httpPort = "3002"
237+
238+
// Minimal context to satisfy marshaling (values don’t matter for this check).
239+
context = Context{
240+
Namespace: "testNamespace",
241+
Ingress: "testIngress",
242+
Service: "testService",
243+
Pod: "testPod",
244+
}
245+
246+
req := httptest.NewRequest("GET", "/", nil)
247+
rr := httptest.NewRecorder()
248+
249+
echoHandler(rr, req)
250+
251+
if status := rr.Code; status != http.StatusOK {
252+
t.Fatalf("expected status %d, got %d", http.StatusOK, status)
253+
}
254+
255+
var resp RequestAssertions
256+
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
257+
t.Fatalf("failed to unmarshal echo response: %v", err)
258+
}
259+
if resp.HTTPPort != "3002" {
260+
t.Errorf("expected HTTPPort %q, got %q", "3002", resp.HTTPPort)
261+
}
262+
}
263+
264+
func TestEchoHandler_HTTPPortReflectsChange(t *testing.T) {
265+
// Preserve global across tests.
266+
prevHTTPPort := httpPort
267+
defer func() { httpPort = prevHTTPPort }()
268+
269+
httpPort = "3004"
270+
context = Context{
271+
Namespace: "ns",
272+
Ingress: "ing",
273+
Service: "svc",
274+
Pod: "pod",
275+
}
276+
277+
req := httptest.NewRequest("GET", "/", nil)
278+
rr := httptest.NewRecorder()
279+
280+
echoHandler(rr, req)
281+
282+
if status := rr.Code; status != http.StatusOK {
283+
t.Fatalf("expected status %d, got %d", http.StatusOK, status)
284+
}
285+
286+
var resp RequestAssertions
287+
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
288+
t.Fatalf("failed to unmarshal echo response: %v", err)
289+
}
290+
if resp.HTTPPort != "3004" {
291+
t.Errorf("expected HTTPPort %q, got %q", "3004", resp.HTTPPort)
292+
}
293+
}
294+
231295
func TestProcessError(t *testing.T) {
232296
// Create a response recorder to capture the response
233297
rr := httptest.NewRecorder()

conformance/utils/http/http.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ type ExpectedRequest struct {
8787
// AbsentHeaders are names of headers that are expected
8888
// *not* to be present on the request.
8989
AbsentHeaders []string
90+
// If set, CompareRoundTrip asserts the echoed httpPort equals this value.
91+
HTTPPort string
9092
}
9193

9294
// Response defines expected properties of a response from a backend.
@@ -362,6 +364,10 @@ func CompareRoundTrip(t *testing.T, req *roundtripper.Request, cReq *roundtrippe
362364
}
363365
}
364366

367+
if expected.ExpectedRequest.HTTPPort != "" && expected.ExpectedRequest.HTTPPort != cReq.HTTPPort {
368+
return fmt.Errorf("expected httpPort %q, got %q", expected.ExpectedRequest.HTTPPort, cReq.HTTPPort)
369+
}
370+
365371
if expected.Response.Headers != nil {
366372
if cRes.Headers == nil {
367373
return fmt.Errorf("no headers captured, expected %v", len(expected.ExpectedRequest.Headers))

conformance/utils/roundtripper/roundtripper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type CapturedRequest struct {
8585
Method string `json:"method"`
8686
Protocol string `json:"proto"`
8787
Headers map[string][]string `json:"headers"`
88+
HTTPPort string `json:"httpPort,omitempty"`
8889

8990
Namespace string `json:"namespace"`
9091
Pod string `json:"pod"`

0 commit comments

Comments
 (0)