Skip to content

Commit

Permalink
Merge pull request #1115 from jcmoraisjr/jm-test-http-headers
Browse files Browse the repository at this point in the history
Add tests for http header generation
  • Loading branch information
jcmoraisjr authored May 4, 2024
2 parents c52386c + fff7ca8 commit c71f3a8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
12 changes: 11 additions & 1 deletion tests/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package framework

import (
"context"
"crypto/tls"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -217,14 +218,23 @@ func (f *framework) Request(ctx context.Context, t *testing.T, method, host, pat
t.Logf("request method=%s host=%s path=%s\n", method, host, path)
opt := options.ParseRequestOptions(o...)

req, err := http.NewRequestWithContext(ctx, method, "http://127.0.0.1:18080", nil)
url := "http://127.0.0.1:18080"
if opt.HTTPS {
url = "https://127.0.0.1:18443"
}
req, err := http.NewRequestWithContext(ctx, method, url, nil)
require.NoError(t, err)
req.Host = host
req.URL.Path = path
cli := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: opt.TLSSkipVerify,
},
},
}
var res *http.Response
if opt.ExpectResponseCode > 0 {
Expand Down
9 changes: 9 additions & 0 deletions tests/framework/options/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ func ExpectResponseCode(code int) Request {
}
}

func HTTPSRequest(skipVerify bool) Request {
return func(o *requestOpt) {
o.HTTPS = true
o.TLSSkipVerify = skipVerify
}
}

type requestOpt struct {
ExpectResponseCode int
HTTPS bool
TLSSkipVerify bool
}

func ParseRequestOptions(opts ...Request) (opt requestOpt) {
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,41 @@ func TestIntegrationIngress(t *testing.T) {
assert.Equal(t, fmt.Sprintf("https://%s/", hostname), res.HTTPResponse.Header.Get("location"))
})

t.Run("should send default http headers on http request", func(t *testing.T) {
t.Parallel()
svc := f.CreateService(ctx, t, httpServerPort)
_, hostname := f.CreateIngress(ctx, t, svc)
res := f.Request(ctx, t, http.MethodGet, hostname, "/", options.ExpectResponseCode(http.StatusOK))
assert.True(t, res.EchoResponse)
reqHeaders := map[string]string{
"accept-encoding": "gzip",
"user-agent": "Go-http-client/1.1",
"x-forwarded-for": "127.0.0.1",
"x-forwarded-proto": "http",
"x-real-ip": "127.0.0.1",
}
assert.Equal(t, reqHeaders, res.ReqHeaders)
})

t.Run("should send default http headers on https request", func(t *testing.T) {
t.Parallel()
svc := f.CreateService(ctx, t, httpServerPort)
_, hostname := f.CreateIngress(ctx, t, svc, options.DefaultHostTLS())
res := f.Request(ctx, t, http.MethodGet, hostname, "/",
options.ExpectResponseCode(http.StatusOK),
options.HTTPSRequest(true),
)
assert.True(t, res.EchoResponse)
reqHeaders := map[string]string{
"accept-encoding": "gzip",
"user-agent": "Go-http-client/1.1",
"x-forwarded-for": "127.0.0.1",
"x-forwarded-proto": "https",
"x-real-ip": "127.0.0.1",
}
assert.Equal(t, reqHeaders, res.ReqHeaders)
})

t.Run("should take leader", func(t *testing.T) {
t.Parallel()
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
Expand Down

0 comments on commit c71f3a8

Please sign in to comment.