MCPcopy
hub / github.com/assetnote/kiterunner / TestDoClientHostHeaderRequest

Function TestDoClientHostHeaderRequest

pkg/http/client_test.go:134–185  ·  view source on GitHub ↗

TestDoClientHostHeaderRequest should validate that a custom hostname for a target will be obeyed this will not have the port appended. You need to manually append the port if youw ant a custom host header

(t *testing.T)

Source from the content-addressed store, hash-verified

132// TestDoClientHostHeaderRequest should validate that a custom hostname for a target will be obeyed
133// this will not have the port appended. You need to manually append the port if youw ant a custom host header
134func TestDoClientHostHeaderRequest(t *testing.T) {
135 simpleServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
136 w.Header().Set("x-custom-header", "key")
137 w.Header().Set("x-request-url", r.URL.String())
138 w.Header().Set("x-host", r.Host)
139
140 w.WriteHeader(201)
141 body, err := httputil.DumpRequest(r, true)
142 assert.Nil(t, err)
143 w.Write(body)
144 }))
145
146 var (
147 path = "/foo"
148 client = NewHTTPClient(simpleServer.Listener.Addr().String(), false)
149 hostname = strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[0]
150 port, _ = strconv.Atoi(strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[1])
151 req = Request{
152 Route: &Route{Path: []byte( path )},
153 Target: &Target{
154 HostHeader: []byte("diff-hostname-123"),
155 IP: hostname,
156 Port: port,
157 },
158 }
159 resp = Response{}
160 config = Config{
161 Timeout: 1 * time.Second,
162 ReadHeaders: true,
163 ReadBody: true,
164 }
165 )
166 req.Target.ParseHostHeader()
167 resp, err := DoClient(client, req, &config)
168 assert.Nil(t, err)
169
170 // we expect the request uri for the first request to be empty
171 _ = fmt.Sprintf("%s%s", simpleServer.URL, path)
172 assert.Equal(t, "", string(resp.URI))
173
174 // we also expect our headers and body to come back as expected
175 expected := []*Header{
176 {"X-Custom-Header", "key"},
177 {"X-Request-Url", "/foo"},
178 {"X-Host", "diff-hostname-123"},
179 }
180 for _, v := range expected {
181 assert.Contains(t, resp.Headers, v)
182 }
183
184 assert.NotEqual(t, "", string(resp.Body))
185}
186
187// TestDoClientHostHeaderRequestCustom should validate that a custom hostname for a target will be obeyed
188// and will not include the port

Callers

nothing calls this directly

Calls 6

NewHTTPClientFunction · 0.85
DoClientFunction · 0.85
ParseHostHeaderMethod · 0.80
EqualMethod · 0.65
StringMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected