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

Function TestDoClientHostHeaderRequestCustom

pkg/http/client_test.go:189–241  ·  view source on GitHub ↗

TestDoClientHostHeaderRequestCustom should validate that a custom hostname for a target will be obeyed and will not include the port

(t *testing.T)

Source from the content-addressed store, hash-verified

187// TestDoClientHostHeaderRequestCustom should validate that a custom hostname for a target will be obeyed
188// and will not include the port
189func TestDoClientHostHeaderRequestCustom(t *testing.T) {
190 simpleServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
191 w.Header().Set("x-custom-header", "key")
192 w.Header().Set("x-request-url", r.URL.String())
193 w.Header().Set("x-host", r.Host)
194
195 w.WriteHeader(201)
196 body, err := httputil.DumpRequest(r, true)
197 assert.Nil(t, err)
198 w.Write(body)
199 }))
200
201 var (
202 path = "/foo"
203 client = NewHTTPClient(simpleServer.Listener.Addr().String(), false)
204 hostname = strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[0]
205 port, _ = strconv.Atoi(strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[1])
206 req = Request{
207 Route: &Route{Path: []byte( path )},
208 Target: &Target{
209 HostHeader: []byte("no-port-host-header"),
210 Hostname: "diff-hostname-123",
211 IP: hostname,
212 Port: port,
213 },
214 }
215 resp = Response{}
216 config = Config{
217 Timeout: 1 * time.Second,
218 ReadHeaders: true,
219 ReadBody: true,
220 }
221 )
222 req.Target.ParseHostHeader()
223 resp, err := DoClient(client, req, &config)
224 assert.Nil(t, err)
225
226 // we expect the request uri for the first request to be empty
227 _ = fmt.Sprintf("%s%s", simpleServer.URL, path)
228 assert.Equal(t, "", string(resp.URI))
229
230 // we also expect our headers and body to come back as expected
231 expected := []*Header{
232 {"X-Custom-Header", "key"},
233 {"X-Request-Url", "/foo"},
234 {"X-Host", "no-port-host-header"},
235 }
236 for _, v := range expected {
237 assert.Contains(t, resp.Headers, v)
238 }
239
240 assert.NotEqual(t, "", string(resp.Body))
241}
242
243func TestDoClientSimpleRequest(t *testing.T) {
244 simpleServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

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