Accurately reflect the canonical casing of API-Version and OS-Type headers

Go automatically canonicalises HTTP headers, meaning the string `API-Version` passed as a header has always been returned as `Api-Version`. Similarly, `OSType` is returned as `Ostype`.

This commit updates the documentation to reflect this behaviour and modifies the codebase to ensure that input strings are aligned with their canonical output values.

Signed-off-by: maggie44 <64841595+maggie44@users.noreply.github.com>
(cherry picked from commit 76a5ca1d4d)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
maggie44
2024-12-08 22:23:57 +00:00
committed by Sebastiaan van Stijn
parent 24f2f4fe06
commit 5b92b3e7e0
7 changed files with 14 additions and 14 deletions

View File

@@ -67,8 +67,8 @@ func (e versionUnsupportedError) InvalidParameter() {}
func (v VersionMiddleware) WrapHandler(handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
w.Header().Set("Server", fmt.Sprintf("Docker/%s (%s)", v.serverVersion, runtime.GOOS))
w.Header().Set("API-Version", v.defaultAPIVersion)
w.Header().Set("OSType", runtime.GOOS)
w.Header().Set("Api-Version", v.defaultAPIVersion)
w.Header().Set("Ostype", runtime.GOOS)
apiVersion := vars["version"]
if apiVersion == "" {

View File

@@ -141,6 +141,6 @@ func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
hdr := resp.Result().Header
assert.Check(t, is.Contains(hdr.Get("Server"), "Docker/1.2.3"))
assert.Check(t, is.Contains(hdr.Get("Server"), runtime.GOOS))
assert.Check(t, is.Equal(hdr.Get("API-Version"), api.DefaultVersion))
assert.Check(t, is.Equal(hdr.Get("OSType"), runtime.GOOS))
assert.Check(t, is.Equal(hdr.Get("Api-Version"), api.DefaultVersion))
assert.Check(t, is.Equal(hdr.Get("Ostype"), runtime.GOOS))
}

View File

@@ -9561,7 +9561,7 @@ paths:
type: "string"
example: "OK"
headers:
API-Version:
Api-Version:
type: "string"
description: "Max API Version the server supports"
Builder-Version:
@@ -9617,7 +9617,7 @@ paths:
type: "string"
example: "(empty)"
headers:
API-Version:
Api-Version:
type: "string"
description: "Max API Version the server supports"
Builder-Version:

View File

@@ -371,7 +371,7 @@ func TestNegotiateAPIVersionAutomatic(t *testing.T) {
var pingVersion string
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) {
resp := &http.Response{StatusCode: http.StatusOK, Header: http.Header{}}
resp.Header.Set("API-Version", pingVersion)
resp.Header.Set("Api-Version", pingVersion)
resp.Body = io.NopCloser(strings.NewReader("OK"))
return resp, nil
})

View File

@@ -56,8 +56,8 @@ func parsePingResponse(cli *Client, resp serverResponse) (types.Ping, error) {
err := cli.checkResponseErr(resp)
return ping, errdefs.FromStatusCode(err, resp.statusCode)
}
ping.APIVersion = resp.header.Get("API-Version")
ping.OSType = resp.header.Get("OSType")
ping.APIVersion = resp.header.Get("Api-Version")
ping.OSType = resp.header.Get("Ostype")
if resp.header.Get("Docker-Experimental") == "true" {
ping.Experimental = true
}

View File

@@ -24,7 +24,7 @@ func TestPingFail(t *testing.T) {
resp := &http.Response{StatusCode: http.StatusInternalServerError}
if withHeader {
resp.Header = http.Header{}
resp.Header.Set("API-Version", "awesome")
resp.Header.Set("Api-Version", "awesome")
resp.Header.Set("Docker-Experimental", "true")
resp.Header.Set("Swarm", "inactive")
}
@@ -72,7 +72,7 @@ func TestPingSuccess(t *testing.T) {
client: newMockClient(func(req *http.Request) (*http.Response, error) {
resp := &http.Response{StatusCode: http.StatusOK}
resp.Header = http.Header{}
resp.Header.Set("API-Version", "awesome")
resp.Header.Set("Api-Version", "awesome")
resp.Header.Set("Docker-Experimental", "true")
resp.Header.Set("Swarm", "active/manager")
resp.Body = io.NopCloser(strings.NewReader("OK"))
@@ -122,7 +122,7 @@ func TestPingHeadFallback(t *testing.T) {
resp.StatusCode = tc.status
}
resp.Header = http.Header{}
resp.Header.Add("API-Version", strings.Join(reqs, ", "))
resp.Header.Add("Api-Version", strings.Join(reqs, ", "))
return resp, nil
}),
}

View File

@@ -38,7 +38,7 @@ func TestPingGet(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, string(b), "OK")
assert.Equal(t, res.StatusCode, http.StatusOK)
assert.Check(t, hdr(res, "API-Version") != "")
assert.Check(t, hdr(res, "Api-Version") != "")
}
func TestPingHead(t *testing.T) {
@@ -51,7 +51,7 @@ func TestPingHead(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, 0, len(b))
assert.Equal(t, res.StatusCode, http.StatusOK)
assert.Check(t, hdr(res, "API-Version") != "")
assert.Check(t, hdr(res, "Api-Version") != "")
}
func TestPingSwarmHeader(t *testing.T) {