Skip to content

Expose request host to http-server response templates #206

Description

@andrewkroh

Problem

http-server response templates can access .request.url, .request.headers, .request.vars, and .req_num, but they cannot directly access the inbound request host (http.Request.Host).

This makes it difficult for mock APIs to emit realistic absolute pagination URLs, such as RFC 5988 Link headers, that preserve the same origin the client used for the request.

Using {{ hostname }} returns the container's OS hostname. In elastic-package system tests, Elastic Agent often connects through a network alias such as svc-github, while the container hostname may be a generated container ID unless explicitly configured. That mismatch can make mocks emit pagination links whose origin differs from the configured request URL.

Proposed enhancement

Expose the inbound request host in the template context, for example:

data := map[string]interface{}{
    "req_num": count,
    "request": map[string]interface{}{
        "host":    r.Host,
        "vars":    mux.Vars(r),
        "url":     r.URL,
        "headers": r.Header,
    },
}

Example usage

responses:
  - status_code: 200
    headers:
      Link:
        - '<http://{{ .request.host }}/orgs/test/audit-log?after=abcd>; rel="next"'
    body: |
      []

This lets fixtures generate same-origin absolute URLs from the actual request instead of hardcoding a Docker network alias or relying on the container hostname.

Note

request.URL.Hostname() is not sufficient for this use case because server-side Go HTTP requests usually have a relative URL. The host is stored in http.Request.Host.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions